[MEDIA] fromUrl now supports using original file name

This commit is contained in:
Diogo Cordeiro 2020-08-05 17:51:43 +01:00 committed by Diogo Peralta Cordeiro
parent 7e559f0cd0
commit 4c92cc967b
2 changed files with 18 additions and 5 deletions

View File

@ -256,13 +256,19 @@ class ImageFile extends MediaFile
* *
* @param string $url Remote image URL * @param string $url Remote image URL
* @param Profile|null $scoped * @param Profile|null $scoped
* @param string|null $name
* @return ImageFile * @return ImageFile
* @throws ClientException * @throws ClientException
* @throws FileNotFoundException
* @throws InvalidFilenameException
* @throws NoResultException
* @throws ServerException * @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) { if ($mediafile instanceof self) {
return $mediafile; return $mediafile;
} else { } else {

View File

@ -445,10 +445,17 @@ class MediaFile
* *
* @param string $url Remote media URL * @param string $url Remote media URL
* @param Profile|null $scoped * @param Profile|null $scoped
* @param string|null $name
* @return ImageFile|MediaFile * @return ImageFile|MediaFile
* @throws ClientException
* @throws FileNotFoundException
* @throws InvalidFilenameException
* @throws NoResultException
* @throws ServerException * @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)) { if (!common_valid_http_url($url)) {
// TRANS: Server exception. %s is a URL. // TRANS: Server exception. %s is a URL.
@ -478,10 +485,10 @@ class MediaFile
File::respectsQuota($scoped, filesize($temp_filename)); File::respectsQuota($scoped, filesize($temp_filename));
} }
$mimetype = self::getUploadedMimeType($temp_filename); $mimetype = self::getUploadedMimeType($temp_filename, $name ?? false);
$media = common_get_mime_media($mimetype); $media = common_get_mime_media($mimetype);
$basename = basename($temp_filename); $basename = basename($name ?? $temp_filename);
if ($media == 'image') { if ($media == 'image') {
// Use -1 for the id to avoid adding this temporary file to the DB // Use -1 for the id to avoid adding this temporary file to the DB