Guess the mimetype in advance

This commit is contained in:
Michael 2021-06-30 04:28:03 +00:00
parent 4308cace68
commit ec49afa116
2 changed files with 15 additions and 6 deletions

View file

@ -245,13 +245,17 @@ class Photo
* Construct a photo array for a system resource image * Construct a photo array for a system resource image
* *
* @param string $filename Image file name relative to code root * @param string $filename Image file name relative to code root
* @param string $mimetype Image mime type. Defaults to "image/jpeg" * @param string $mimetype Image mime type. Is guessed by file name when empty.
* *
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg") public static function createPhotoForSystemResource($filename, $mimetype = '')
{ {
if (empty($mimetype)) {
$mimetype = Images::guessTypeByExtension($filename);
}
$fields = self::getFields(); $fields = self::getFields();
$values = array_fill(0, count($fields), ""); $values = array_fill(0, count($fields), "");
@ -269,13 +273,17 @@ class Photo
* *
* @param string $url Image URL * @param string $url Image URL
* @param int $uid User ID of the requesting person * @param int $uid User ID of the requesting person
* @param string $mimetype Image mime type. Defaults to "image/jpeg" * @param string $mimetype Image mime type. Is guessed by file name when empty.
* *
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = "image/jpeg") public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = '')
{ {
if (empty($mimetype)) {
$mimetype = Images::guessTypeByExtension($url);
}
$fields = self::getFields(); $fields = self::getFields();
$values = array_fill(0, count($fields), ""); $values = array_fill(0, count($fields), "");

View file

@ -29,6 +29,7 @@ use Friendica\Model\Contact;
use Friendica\Model\Photo as MPhoto; use Friendica\Model\Photo as MPhoto;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\Storage\ExternalResource; use Friendica\Model\Storage\ExternalResource;
use Friendica\Model\Storage\SystemResource;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\Images; use Friendica\Util\Images;
@ -102,8 +103,8 @@ class Photo extends BaseModule
$stamp = microtime(true); $stamp = microtime(true);
$imgdata = MPhoto::getImageDataForPhoto($photo); $imgdata = MPhoto::getImageDataForPhoto($photo);
// The mimetype for an external resource can only be known after it had been fetched // The mimetype for an external or system resource can only be known reliably after it had been fetched
if ($photo['backend-class'] == ExternalResource::NAME) { if (in_array($photo['backend-class'], [ExternalResource::NAME, SystemResource::NAME])) {
$mimetype = Images::getMimeTypeByData($imgdata); $mimetype = Images::getMimeTypeByData($imgdata);
if (!empty($mimetype)) { if (!empty($mimetype)) {
$photo['type'] = $mimetype; $photo['type'] = $mimetype;