fix Photo model

This commit is contained in:
Philipp Holzer 2020-01-06 22:07:23 +01:00
parent d6fab6b06b
commit 1bce3fd0f1
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90

View file

@ -173,26 +173,24 @@ class Photo
*/ */
public static function getImageForPhoto(array $photo) public static function getImageForPhoto(array $photo)
{ {
$data = ""; if (empty($photo['backend-class'])) {
if ($photo["backend-class"] == "") {
// legacy data storage in "data" column // legacy data storage in "data" column
$i = self::selectFirst(["data"], ["id" => $photo["id"]]); $i = self::selectFirst(['data'], ['id' => $photo['id']]);
if ($i === false) { if ($i === false) {
return null; return null;
} }
$data = $i["data"]; $data = $i['data'];
} else { } else {
$backendClass = $photo["backend-class"]; $backendClass = DI::facStorage()->getByName($photo['backend-class'] ?? '');
$backendRef = $photo["backend-ref"]; $backendRef = $photo['backend-ref'] ?? '';
$data = $backendClass::get($backendRef); $data = $backendClass->get($backendRef);
} }
if ($data === "") { if (empty($data)) {
return null; return null;
} }
return new Image($data, $photo["type"]); return new Image($data, $photo['type']);
} }
/** /**
@ -224,10 +222,10 @@ class Photo
$values = array_fill(0, count($fields), ""); $values = array_fill(0, count($fields), "");
$photo = array_combine($fields, $values); $photo = array_combine($fields, $values);
$photo["backend-class"] = SystemResource::NAME; $photo['backend-class'] = SystemResource::NAME;
$photo["backend-ref"] = $filename; $photo['backend-ref'] = $filename;
$photo["type"] = $mimetype; $photo['type'] = $mimetype;
$photo["cacheable"] = false; $photo['cacheable'] = false;
return $photo; return $photo;
} }
@ -340,10 +338,9 @@ class Photo
$photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions); $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
foreach($photos as $photo) { foreach($photos as $photo) {
/** @var IStorage $backend_class */ $backend_class = DI::facStorage()->getByName($photo['backend-class'] ?? '');
$backend_class = (string)$photo["backend-class"]; if ($backend_class !== null) {
if ($backend_class !== "") { $backend_class->delete($photo["backend-ref"] ?? '');
$backend_class::delete($photo["backend-ref"]);
} }
} }
@ -370,10 +367,9 @@ class Photo
$photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions); $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
foreach($photos as $photo) { foreach($photos as $photo) {
/** @var IStorage $backend_class */ $backend_class = DI::facStorage()->getByName($photo['backend-class'] ?? '');
$backend_class = (string)$photo["backend-class"]; if ($backend_class !== null) {
if ($backend_class !== "") { $fields["backend-ref"] = $backend_class->put($img->asString(), $photo['backend-ref']);
$fields["backend-ref"] = $backend_class::put($img->asString(), $photo["backend-ref"]);
} else { } else {
$fields["data"] = $img->asString(); $fields["data"] = $img->asString();
} }