1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-22 22:39:36 +02:00

feat: enhance Immich integration with local copy option and validation for image handling

This commit is contained in:
Sean Morley 2025-06-01 19:55:12 -04:00
parent f95afdc35c
commit 06787bccf6
12 changed files with 214 additions and 37 deletions

View file

@ -22,7 +22,7 @@
const dispatch = createEventDispatcher();
let images: { id: string; image: string; is_primary: boolean }[] = [];
let images: { id: string; image: string; is_primary: boolean; immich_id: string | null }[] = [];
let warningMessage: string = '';
let constrainDates: boolean = false;
@ -82,6 +82,7 @@
let fileInput: HTMLInputElement;
let immichIntegration: boolean = false;
let copyImmichLocally: boolean = false;
import ActivityComplete from './ActivityComplete.svelte';
import CategoryDropdown from './CategoryDropdown.svelte';
@ -161,13 +162,19 @@
addToast('error', $t('adventures.category_fetch_error'));
}
// Check for Immich Integration
let res = await fetch('/api/integrations');
if (!res.ok) {
let res = await fetch('/api/integrations/immich/');
// If the response is not ok, we assume Immich integration is not available
if (!res.ok && res.status !== 404) {
addToast('error', $t('immich.integration_fetch_error'));
} else {
let data = await res.json();
if (data.immich) {
if (data.error) {
immichIntegration = false;
} else if (data.id) {
immichIntegration = true;
copyImmichLocally = data.copy_locally || false;
} else {
immichIntegration = false;
}
}
});
@ -330,7 +337,12 @@
});
if (res.ok) {
let newData = deserialize(await res.text()) as { data: { id: string; image: string } };
let newImage = { id: newData.data.id, image: newData.data.image, is_primary: false };
let newImage = {
id: newData.data.id,
image: newData.data.image,
is_primary: false,
immich_id: null
};
images = [...images, newImage];
adventure.images = images;
addToast('success', $t('adventures.image_upload_success'));
@ -381,7 +393,12 @@
});
if (res2.ok) {
let newData = deserialize(await res2.text()) as { data: { id: string; image: string } };
let newImage = { id: newData.data.id, image: newData.data.image, is_primary: false };
let newImage = {
id: newData.data.id,
image: newData.data.image,
is_primary: false,
immich_id: null
};
images = [...images, newImage];
adventure.images = images;
addToast('success', $t('adventures.image_upload_success'));
@ -817,6 +834,18 @@
url = e.detail;
fetchImage();
}}
{copyImmichLocally}
on:remoteImmichSaved={(e) => {
const newImage = {
id: e.detail.id,
image: e.detail.image,
is_primary: e.detail.is_primary,
immich_id: e.detail.immich_id
};
images = [...images, newImage];
adventure.images = images;
addToast('success', $t('adventures.image_upload_success'));
}}
/>
{/if}

View file

@ -13,6 +13,7 @@
let loading = false;
export let adventure: Adventure | null = null;
export let copyImmichLocally: boolean = false;
const dispatch = createEventDispatcher();
@ -45,6 +46,36 @@
return fetchAssets(immichNextURL, true);
}
async function saveImmichRemoteUrl(imageId: string) {
if (!adventure) {
console.error('No adventure provided to save the image URL');
return;
}
let res = await fetch('/api/images', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
immich_id: imageId,
adventure: adventure.id
})
});
if (res.ok) {
let data = await res.json();
if (!data.image) {
console.error('No image data returned from the server');
immichError = $t('immich.error_saving_image');
return;
}
dispatch('remoteImmichSaved', data);
} else {
let errorData = await res.json();
console.error('Error saving image URL:', errorData);
immichError = $t(errorData.message || 'immich.error_saving_image');
}
}
async function fetchAssets(url: string, usingNext = false) {
loading = true;
try {
@ -191,7 +222,11 @@
on:click={() => {
let currentDomain = window.location.origin;
let fullUrl = `${currentDomain}/immich/${image.id}`;
dispatch('fetchImage', fullUrl);
if (copyImmichLocally) {
dispatch('fetchImage', fullUrl);
} else {
saveImmichRemoteUrl(image.id);
}
}}
>
{$t('adventures.upload_image')}