From e28db58c6604de55c5b45e714edca4a8b5560ea9 Mon Sep 17 00:00:00 2001 From: "SENOO, Ken" Date: Wed, 23 Nov 2022 22:55:41 +0900 Subject: [PATCH] Support v2 media uploading method In GNU social v2, following commits are new emdia uploading method. - d2c7d70f49bd01b08977360998616c252bbfa9ea - 7fa4d56f05fb8420505398c13a3d180e3524f4b6 So support these new method for qvitter. --- actions/apiaccountupdateprofilebanner.php | 10 ++++++---- actions/apiupdateavatar.php | 14 +++++++------- actions/apiupdatebackgroundimage.php | 11 ++++++----- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/actions/apiaccountupdateprofilebanner.php b/actions/apiaccountupdateprofilebanner.php index aa7a606..280c961 100644 --- a/actions/apiaccountupdateprofilebanner.php +++ b/actions/apiaccountupdateprofilebanner.php @@ -37,6 +37,7 @@ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ +require_once INSTALLDIR . '/lib/util/tempfile.php'; if (!defined('GNUSOCIAL')) { exit(1); @@ -93,12 +94,13 @@ class ApiAccountUpdateProfileBannerAction extends ApiAuthAction $this->img = str_replace(' ', '+', $this->img); $this->img = base64_decode($this->img, true); - $fh = tmpfile(); - fwrite($fh, $this->img); + $fh = new TemporaryFile('gs-mediaupload'); + fwrite($fh->getResource(), $this->img); unset($this->img); - fseek($fh, 0); - $mediafile = MediaFile::fromFilehandle($fh, $this->scoped); + fflush($fh->getResouce()); + $mediafile = MediaFile::fromFileInfo($fh, $this->scoped); + unset($fh); } // maybe resize diff --git a/actions/apiupdateavatar.php b/actions/apiupdateavatar.php index b234b55..fdb54bb 100644 --- a/actions/apiupdateavatar.php +++ b/actions/apiupdateavatar.php @@ -37,6 +37,7 @@ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ +require_once INSTALLDIR . '/lib/util/tempfile.php'; if (!defined('GNUSOCIAL')) { exit(1); @@ -90,15 +91,14 @@ class ApiUpdateAvatarAction extends ApiAuthAction $imagefile = null; // write the image to a temporary file - $fh = tmpfile(); - fwrite($fh, $this->img); + $fh = new TemporaryFile('gs-mediaupload'); + fwrite($fh->getResource(), $this->img); unset($this->img); // no need to keep it in memory - // seek back to position 0, so we don't read EOF directly - fseek($fh, 0); // read the temporary file as an uploaded image, will store File object - $mediafile = MediaFile::fromFilehandle($fh, $this->scoped); - // Deletes the temporary file, if it was needed we stored it in fromFilehandle - fclose($fh); + fflush($fh->getResource()); + $mediafile = MediaFile::fromFileInfo($fh, $this->scoped); + // Deletes the temporary file, if it was needed we stored it in fromFileInfo + unset($fh); // Now try to get it as an ImageFile since it has some handy functions $imagefile = ImageFile::fromFileObject($mediafile->fileRecord); diff --git a/actions/apiupdatebackgroundimage.php b/actions/apiupdatebackgroundimage.php index e71f24e..cad44e0 100644 --- a/actions/apiupdatebackgroundimage.php +++ b/actions/apiupdatebackgroundimage.php @@ -37,6 +37,7 @@ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ +require_once INSTALLDIR . '/lib/util/tempfile.php'; if (!defined('GNUSOCIAL')) { exit(1); @@ -91,15 +92,15 @@ class ApiUpdateBackgroundImageAction extends ApiAuthAction $imagefile = null; // put the image data in a temporary file - $fh = tmpfile(); - fwrite($fh, $this->img); + $fh = new TemporaryFile('gs-mediaupload') + fwrite($fh->getResource(), $this->img); unset($this->img); - fseek($fh, 0); // go to beginning just to be sure the content is read properly // We get a MediaFile with a File object using the filehandle - $mediafile = MediaFile::fromFilehandle($fh, $this->scoped); + fflush($fh->getResource()); + $mediafile = MediaFile::fromFileInfo($fh, $this->scoped); // and can dispose of the temporary filehandle since we're certain we have a File on disk now - fclose($fh); + unset($fh); $imagefile = ImageFile::fromFileObject($mediafile->fileRecord); unset($mediafile); // No need to keep the MediaFile around anymore, everything we need is in ImageFile