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.
This commit is contained in:
SENOO, Ken 2022-11-23 22:55:41 +09:00
parent bbb4b1eb60
commit e28db58c66
3 changed files with 19 additions and 16 deletions

View File

@ -37,6 +37,7 @@
· · · ·
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
require_once INSTALLDIR . '/lib/util/tempfile.php';
if (!defined('GNUSOCIAL')) { if (!defined('GNUSOCIAL')) {
exit(1); exit(1);
@ -93,12 +94,13 @@ class ApiAccountUpdateProfileBannerAction extends ApiAuthAction
$this->img = str_replace(' ', '+', $this->img); $this->img = str_replace(' ', '+', $this->img);
$this->img = base64_decode($this->img, true); $this->img = base64_decode($this->img, true);
$fh = tmpfile(); $fh = new TemporaryFile('gs-mediaupload');
fwrite($fh, $this->img); fwrite($fh->getResource(), $this->img);
unset($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 // maybe resize

View File

@ -37,6 +37,7 @@
· · · ·
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
require_once INSTALLDIR . '/lib/util/tempfile.php';
if (!defined('GNUSOCIAL')) { if (!defined('GNUSOCIAL')) {
exit(1); exit(1);
@ -90,15 +91,14 @@ class ApiUpdateAvatarAction extends ApiAuthAction
$imagefile = null; $imagefile = null;
// write the image to a temporary file // write the image to a temporary file
$fh = tmpfile(); $fh = new TemporaryFile('gs-mediaupload');
fwrite($fh, $this->img); fwrite($fh->getResource(), $this->img);
unset($this->img); // no need to keep it in memory 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 // read the temporary file as an uploaded image, will store File object
$mediafile = MediaFile::fromFilehandle($fh, $this->scoped); fflush($fh->getResource());
// Deletes the temporary file, if it was needed we stored it in fromFilehandle $mediafile = MediaFile::fromFileInfo($fh, $this->scoped);
fclose($fh); // 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 // Now try to get it as an ImageFile since it has some handy functions
$imagefile = ImageFile::fromFileObject($mediafile->fileRecord); $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);

View File

@ -37,6 +37,7 @@
· · · ·
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
require_once INSTALLDIR . '/lib/util/tempfile.php';
if (!defined('GNUSOCIAL')) { if (!defined('GNUSOCIAL')) {
exit(1); exit(1);
@ -91,15 +92,15 @@ class ApiUpdateBackgroundImageAction extends ApiAuthAction
$imagefile = null; $imagefile = null;
// put the image data in a temporary file // put the image data in a temporary file
$fh = tmpfile(); $fh = new TemporaryFile('gs-mediaupload')
fwrite($fh, $this->img); fwrite($fh->getResource(), $this->img);
unset($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 // 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 // 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); $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
unset($mediafile); // No need to keep the MediaFile around anymore, everything we need is in ImageFile unset($mediafile); // No need to keep the MediaFile around anymore, everything we need is in ImageFile