fix: prevent broken link upon thumbnail in variety list when having uploaded a PNG file
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Repositories\Core;
|
namespace App\Repositories\Core;
|
||||||
|
|
||||||
|
use Spatie\MediaLibrary\MediaCollections\Models\Media as MediaModel;
|
||||||
|
|
||||||
class Medias
|
class Medias
|
||||||
{
|
{
|
||||||
public static function getImage($model, $conversion = 'normal', $collection = 'images')
|
public static function getImage($model, $conversion = 'normal', $collection = 'images')
|
||||||
@@ -79,13 +81,9 @@ class Medias
|
|||||||
|
|
||||||
public static function getImageSrc($image)
|
public static function getImageSrc($image)
|
||||||
{
|
{
|
||||||
if (! $image) {
|
$media = self::resolveMedia($image);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$id = $image['id'];
|
|
||||||
$filename = self::getFilename($image);
|
|
||||||
|
|
||||||
return "/storage/{$id}/{$filename}";
|
return $media ? $media->getUrl() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getThumbSrc($image)
|
public static function getThumbSrc($image)
|
||||||
@@ -110,6 +108,12 @@ class Medias
|
|||||||
|
|
||||||
public static function getSrcByType($image, $type)
|
public static function getSrcByType($image, $type)
|
||||||
{
|
{
|
||||||
|
$media = self::resolveMedia($image);
|
||||||
|
|
||||||
|
if ($media) {
|
||||||
|
return $type ? $media->getUrl($type) : $media->getUrl();
|
||||||
|
}
|
||||||
|
|
||||||
return $image ? '/storage/'.$image['id'].'/conversions/'.self::getFilename($image, $type) : false;
|
return $image ? '/storage/'.$image['id'].'/conversions/'.self::getFilename($image, $type) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,4 +128,48 @@ class Medias
|
|||||||
{
|
{
|
||||||
return str_replace(['#', '/', '\\', ' '], '-', $name);
|
return str_replace(['#', '/', '\\', ' '], '-', $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function resolveMedia($image): ?MediaModel
|
||||||
|
{
|
||||||
|
if ($image instanceof MediaModel) {
|
||||||
|
return $image;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_null($image)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($image)) {
|
||||||
|
return self::hydrateMedia($image);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_object($image)) {
|
||||||
|
if ($image instanceof \ArrayAccess) {
|
||||||
|
return self::hydrateMedia((array) $image);
|
||||||
|
}
|
||||||
|
|
||||||
|
$array = method_exists($image, 'toArray') ? $image->toArray() : (array) $image;
|
||||||
|
|
||||||
|
return self::hydrateMedia($array);
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = data_get($image, 'id');
|
||||||
|
|
||||||
|
return $id ? MediaModel::query()->withoutGlobalScopes()->find($id) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function hydrateMedia(array $attributes): ?MediaModel
|
||||||
|
{
|
||||||
|
$id = data_get($attributes, 'id');
|
||||||
|
|
||||||
|
if (! $id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$media = new MediaModel();
|
||||||
|
$media->forceFill($attributes);
|
||||||
|
$media->exists = true;
|
||||||
|
|
||||||
|
return $media;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user