[AVATAR] Temporary ImageFile wasn't

This commit is contained in:
Diogo Cordeiro 2020-06-21 00:09:32 +01:00 committed by Diogo Peralta Cordeiro
parent 44ad0d3a85
commit 02055dee49
3 changed files with 34 additions and 7 deletions

View File

@ -359,7 +359,7 @@ class AvatarsettingsAction extends SettingsAction
'x' => $dest_x, 'y' => $dest_y,
'w' => $dest_w, 'h' => $dest_h,];
$imagefile = new ImageFile(null, $filedata['filepath']);
$imagefile = new ImageFile(-1, $filedata['filepath']);
$filename = Avatar::filename(
$this->scoped->getID(),
image_type_to_extension($imagefile->preferredType()),

View File

@ -55,7 +55,21 @@ class ImageFile extends MediaFile
public $animated; // Animated image? (has more than 1 frame). null means untested
public $mimetype; // The _ImageFile_ mimetype, _not_ the originating File object
public function __construct($id, string $filepath)
/**
* ImageFile constructor.
*
* @param int|null $id The DB id of the file. Int if known, null if not.
* If null, it searches for it. If -1, it skips all DB
* interactions (useful for temporary objects)
* @param string $filepath The path of the file this media refers to. Required
* @param string|null $filehash The hash of the file, if known. Optional
*
* @throws ClientException
* @throws NoResultException
* @throws ServerException
* @throws UnsupportedMediaException
*/
public function __construct(?int $id = null, string $filepath, ?string $filehash = null)
{
$old_limit = ini_set('memory_limit', common_config('attachments', 'memory_limit'));
@ -74,7 +88,14 @@ class ImageFile extends MediaFile
}
return false;
};
if (!(($cmp($this, IMAGETYPE_GIF) && function_exists('imagecreatefromgif')) || ($cmp($this, IMAGETYPE_JPEG) && function_exists('imagecreatefromjpeg')) || ($cmp($this, IMAGETYPE_BMP) && function_exists('imagecreatefrombmp')) || ($cmp($this, IMAGETYPE_WBMP) && function_exists('imagecreatefromwbmp')) || ($cmp($this, IMAGETYPE_XBM) && function_exists('imagecreatefromxbm')) || ($cmp($this, IMAGETYPE_PNG) && function_exists('imagecreatefrompng')))) {
if (!(($cmp($this, IMAGETYPE_GIF) && function_exists('imagecreatefromgif')) ||
($cmp($this, IMAGETYPE_JPEG) && function_exists('imagecreatefromjpeg')) ||
($cmp($this, IMAGETYPE_BMP) && function_exists('imagecreatefrombmp')) ||
($cmp($this, IMAGETYPE_WBMP) && function_exists('imagecreatefromwbmp')) ||
($cmp($this, IMAGETYPE_XBM) && function_exists('imagecreatefromxbm')) ||
($cmp($this, IMAGETYPE_PNG) && function_exists('imagecreatefrompng'))
)
) {
common_debug("Mimetype '{$this->mimetype}' was not recognized as a supported format");
// TRANS: Exception thrown when trying to upload an unsupported image file format.
throw new UnsupportedMediaException(_m('Unsupported image format.'), $this->filepath);
@ -86,7 +107,7 @@ class ImageFile extends MediaFile
parent::__construct(
$filepath,
$this->mimetype,
null /* filehash, MediaFile will calculate it */,
$filehash,
$id
);
@ -123,7 +144,11 @@ class ImageFile extends MediaFile
/**
* Create a thumbnail from a file object
*
* @param File $file
* @return ImageFile
* @throws FileNotFoundException
* @throws UnsupportedMediaException
* @throws UseFileAsThumbnailException
*/
public static function fromFileObject(File $file)
{

View File

@ -44,10 +44,12 @@ class MediaFile
public $mimetype;
/**
* MediaFile constructor.
*
* @param string $filepath The path of the file this media refers to. Required
* @param string $mimetype The mimetype of the file. Required
* @param string $filehash The hash of the file, if known. Optional
* @param null|int $id The DB id of the file. Int if known, null if not.
* @param string|null $filehash The hash of the file, if known. Optional
* @param int|null $id The DB id of the file. Int if known, null if not.
* If null, it searches for it. If -1, it skips all DB
* interactions (useful for temporary objects)
*
@ -55,7 +57,7 @@ class MediaFile
* @throws NoResultException
* @throws ServerException
*/
public function __construct(string $filepath, string $mimetype, $filehash = null, $id = null)
public function __construct(string $filepath, string $mimetype, ?string $filehash = null, ?int $id = null)
{
$this->filepath = $filepath;
$this->filename = basename($this->filepath);