Add check that photos key exists before trying to do something with it

This commit is contained in:
Jonny Barnes 2017-05-19 13:17:27 +01:00
parent 2d8678b037
commit 67782e30e8

View file

@ -76,18 +76,20 @@ class NoteService
} }
*/ */
//add support for media uploaded as URLs //add support for media uploaded as URLs
foreach ($data['photo'] as $photo) { if (array_key_exists('photo', $data)) {
// check the media was uploaded to my endpoint, and use path foreach ($data['photo'] as $photo) {
if (starts_with($photo, config('filesystems.disks.s3.url'))) { // check the media was uploaded to my endpoint, and use path
$path = substr($photo, strlen(config('filesystems.disks.s3.url'))); if (starts_with($photo, config('filesystems.disks.s3.url'))) {
$media = Media::where('path', ltrim($path, '/'))->firstOrFail(); $path = substr($photo, strlen(config('filesystems.disks.s3.url')));
} else { $media = Media::where('path', ltrim($path, '/'))->firstOrFail();
$media = Media::firstOrNew(['path' => $photo]); } else {
// currently assuming this is a photo from Swarm $media = Media::firstOrNew(['path' => $photo]);
$media->type = 'image'; // currently assuming this is a photo from Swarm
$media->save(); $media->type = 'image';
$media->save();
}
$note->media()->save($media);
} }
$note->media()->save($media);
} }
$note->save(); $note->save();