[MEDIA] fromUrl now supports using original file name

This commit is contained in:
Diogo Cordeiro 2020-08-05 17:51:43 +01:00
parent 1428ac2cb0
commit 7a2bb38331
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 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 {

View File

@ -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