Images can be uploaded with alt text

This means associating images to notes via a pivot table, we also finish
off the migration of medai from S3.
This commit is contained in:
Jonny Barnes 2026-01-04 10:07:25 +00:00
commit 9e788e0fe8
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
9 changed files with 200 additions and 19 deletions

View file

@ -51,8 +51,11 @@ class NoteService
// $note->instagram_url = $this->getInstagramUrl($request);
foreach ($this->getMedia($data) as $media) {
$note->media()->save($media);
foreach ($this->getMedia($data) as $index => $media) {
$note->media()->attach($media['value']->id, [
'alt_text' => $media['alt_text'],
'order' => $index,
]);
}
$note->save();
@ -195,16 +198,37 @@ class NoteService
if (isset($photos)) {
foreach ((array) $photos as $photo) {
// $photo can be a string of the URL opf the photo
// or it can be an object with a `value` and `alt`
$photoUrl = null;
$photoAlt = null;
if (is_string($photo)) {
$photoUrl = $photo;
} elseif (is_array($photo)) {
$photoUrl = $photo['value'];
$photoAlt = $photo['alt'];
}
if (empty($photoUrl)) {
continue;
}
// check the media was uploaded to my endpoint, and use path
if (Str::startsWith($photo, config('filesystems.disks.public.url'))) {
$path = substr($photo, strlen(config('filesystems.disks.public.url')));
$media[] = Media::where('path', ltrim($path, '/'))->firstOrFail();
if (Str::startsWith($photoUrl, config('filesystems.disks.public.url'))) {
$path = substr($photoUrl, strlen(config('filesystems.disks.public.url')));
$media[] = [
'value' => Media::where('path', ltrim($path, '/'))->firstOrFail(),
'alt' => $photoAlt,
];
} else {
$newMedia = Media::firstOrNew(['path' => $photo]);
$newMedia = Media::firstOrNew(['path' => $photoUrl]);
// currently assuming this is a photo from Swarm or OwnYourGram
$newMedia->type = 'image';
$newMedia->save();
$media[] = $newMedia;
$media[] = [
'value' => $newMedia,
'alt' => $photoAlt,
];
}
}
}