From 4c92cc967bd128683634a94377389fe2ea4ff3fc Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Wed, 5 Aug 2020 17:51:43 +0100 Subject: [PATCH] [MEDIA] fromUrl now supports using original file name --- lib/media/imagefile.php | 10 ++++++++-- lib/media/mediafile.php | 13 ++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/media/imagefile.php b/lib/media/imagefile.php index 1818c76900..cf425cbae8 100644 --- a/lib/media/imagefile.php +++ b/lib/media/imagefile.php @@ -256,13 +256,19 @@ class ImageFile extends MediaFile * * @param string $url Remote image URL * @param Profile|null $scoped + * @param string|null $name * @return ImageFile * @throws ClientException + * @throws FileNotFoundException + * @throws InvalidFilenameException + * @throws NoResultException * @throws ServerException + * @throws UnsupportedMediaException + * @throws UseFileAsThumbnailException */ - public static function fromUrl(string $url, ?Profile $scoped = null): self + public static function fromUrl(string $url, ?Profile $scoped = null, ?string $name = null): self { - $mediafile = parent::fromUrl($url, $scoped); + $mediafile = parent::fromUrl($url, $scoped, $name); if ($mediafile instanceof self) { return $mediafile; } else { diff --git a/lib/media/mediafile.php b/lib/media/mediafile.php index e0ece91d72..31179db08f 100644 --- a/lib/media/mediafile.php +++ b/lib/media/mediafile.php @@ -445,10 +445,17 @@ class MediaFile * * @param string $url Remote media URL * @param Profile|null $scoped + * @param string|null $name * @return ImageFile|MediaFile + * @throws ClientException + * @throws FileNotFoundException + * @throws InvalidFilenameException + * @throws NoResultException * @throws ServerException + * @throws UnsupportedMediaException + * @throws UseFileAsThumbnailException */ - public static function fromUrl(string $url, ?Profile $scoped = null) + public static function fromUrl(string $url, ?Profile $scoped = null, ?string $name = null) { if (!common_valid_http_url($url)) { // TRANS: Server exception. %s is a URL. @@ -478,10 +485,10 @@ class MediaFile File::respectsQuota($scoped, filesize($temp_filename)); } - $mimetype = self::getUploadedMimeType($temp_filename); + $mimetype = self::getUploadedMimeType($temp_filename, $name ?? false); $media = common_get_mime_media($mimetype); - $basename = basename($temp_filename); + $basename = basename($name ?? $temp_filename); if ($media == 'image') { // Use -1 for the id to avoid adding this temporary file to the DB