2024-02-13 23:07:37 +01:00
|
|
|
import { UploadState } from '$lib/models/upload-asset';
|
2023-06-02 15:55:08 +02:00
|
|
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
2024-02-29 17:22:39 +01:00
|
|
|
import { getKey, uploadRequest } from '$lib/utils';
|
2023-07-17 18:22:29 +02:00
|
|
|
import { addAssetsToAlbum } from '$lib/utils/asset-utils';
|
2023-09-01 18:00:51 +02:00
|
|
|
import { ExecutorQueue } from '$lib/utils/executor-queue';
|
2024-02-29 17:22:39 +01:00
|
|
|
import { defaults, getSupportedMediaTypes, type AssetFileUploadResponseDto } from '@immich/sdk';
|
2024-01-13 01:43:36 +01:00
|
|
|
import { getServerErrorMessage, handleError } from './handle-error';
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-08-15 03:22:33 +02:00
|
|
|
let _extensions: string[];
|
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
export const uploadExecutionQueue = new ExecutorQueue({ concurrency: 2 });
|
|
|
|
|
2023-08-15 03:22:33 +02:00
|
|
|
const getExtensions = async () => {
|
|
|
|
if (!_extensions) {
|
2024-02-13 23:07:37 +01:00
|
|
|
const { image, video } = await getSupportedMediaTypes();
|
|
|
|
_extensions = [...image, ...video];
|
2023-08-15 03:22:33 +02:00
|
|
|
}
|
|
|
|
return _extensions;
|
|
|
|
};
|
2023-07-12 05:56:30 +02:00
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
export const openFileUploadDialog = async (albumId?: string | undefined) => {
|
2023-08-15 03:22:33 +02:00
|
|
|
const extensions = await getExtensions();
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
return new Promise<(string | undefined)[]>((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const fileSelector = document.createElement('input');
|
|
|
|
|
|
|
|
fileSelector.type = 'file';
|
|
|
|
fileSelector.multiple = true;
|
2023-07-12 05:56:30 +02:00
|
|
|
fileSelector.accept = extensions.join(',');
|
2024-02-27 17:37:37 +01:00
|
|
|
fileSelector.addEventListener('change', (e: Event) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const target = e.target as HTMLInputElement;
|
|
|
|
if (!target.files) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-17 18:22:29 +02:00
|
|
|
const files = Array.from(target.files);
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-08-25 06:03:28 +02:00
|
|
|
resolve(fileUploadHandler(files, albumId));
|
2024-02-02 04:18:00 +01:00
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
fileSelector.click();
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.log('Error selecting file', error);
|
|
|
|
reject(error);
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
});
|
2022-07-27 03:53:25 +02:00
|
|
|
};
|
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
export const fileUploadHandler = async (files: File[], albumId: string | undefined = undefined): Promise<string[]> => {
|
2023-08-15 03:22:33 +02:00
|
|
|
const extensions = await getExtensions();
|
2023-09-01 18:00:51 +02:00
|
|
|
const promises = [];
|
|
|
|
for (const file of files) {
|
|
|
|
const name = file.name.toLowerCase();
|
2024-02-02 04:18:00 +01:00
|
|
|
if (extensions.some((extension) => name.endsWith(extension))) {
|
2023-09-01 18:00:51 +02:00
|
|
|
uploadAssetsStore.addNewUploadAsset({ id: getDeviceAssetId(file), file, albumId });
|
|
|
|
promises.push(uploadExecutionQueue.addTask(() => fileUploader(file, albumId)));
|
|
|
|
}
|
|
|
|
}
|
2023-07-17 18:22:29 +02:00
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
const results = await Promise.all(promises);
|
|
|
|
return results.filter((result): result is string => !!result);
|
2023-07-17 18:22:29 +02:00
|
|
|
};
|
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
function getDeviceAssetId(asset: File) {
|
|
|
|
return 'web' + '-' + asset.name + '-' + asset.lastModified;
|
|
|
|
}
|
2022-12-30 03:07:18 +01:00
|
|
|
|
2023-07-17 18:22:29 +02:00
|
|
|
// TODO: should probably use the @api SDK
|
2023-08-25 06:03:28 +02:00
|
|
|
async function fileUploader(asset: File, albumId: string | undefined = undefined): Promise<string | undefined> {
|
2023-07-01 06:50:47 +02:00
|
|
|
const fileCreatedAt = new Date(asset.lastModified).toISOString();
|
2023-09-01 18:00:51 +02:00
|
|
|
const deviceAssetId = getDeviceAssetId(asset);
|
|
|
|
|
|
|
|
return new Promise((resolve) => resolve(uploadAssetsStore.markStarted(deviceAssetId)))
|
2024-02-29 17:22:39 +01:00
|
|
|
.then(() => {
|
|
|
|
const formData = new FormData();
|
|
|
|
for (const [key, value] of Object.entries({
|
|
|
|
deviceAssetId,
|
|
|
|
deviceId: 'WEB',
|
|
|
|
fileCreatedAt,
|
|
|
|
fileModifiedAt: new Date(asset.lastModified).toISOString(),
|
|
|
|
isFavorite: 'false',
|
|
|
|
duration: '0:00:00.000000',
|
|
|
|
assetData: new File([asset], asset.name),
|
|
|
|
})) {
|
|
|
|
formData.append(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = getKey();
|
|
|
|
|
|
|
|
return uploadRequest<AssetFileUploadResponseDto>({
|
|
|
|
url: defaults.baseUrl + '/asset/upload' + (key ? `?key=${key}` : ''),
|
|
|
|
data: formData,
|
|
|
|
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
|
|
|
|
});
|
|
|
|
})
|
2023-09-01 18:00:51 +02:00
|
|
|
.then(async (response) => {
|
|
|
|
if (response.status == 200 || response.status == 201) {
|
|
|
|
const res: AssetFileUploadResponseDto = response.data;
|
|
|
|
|
|
|
|
if (res.duplicate) {
|
|
|
|
uploadAssetsStore.duplicateCounter.update((count) => count + 1);
|
2024-03-13 02:37:56 +01:00
|
|
|
} else {
|
|
|
|
uploadAssetsStore.successCounter.update((c) => c + 1);
|
2023-09-01 18:00:51 +02:00
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
if (albumId && res.id) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Adding to album...' });
|
|
|
|
await addAssetsToAlbum(albumId, [res.id]);
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Added to album' });
|
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, {
|
|
|
|
state: res.duplicate ? UploadState.DUPLICATED : UploadState.DONE,
|
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
}, 1000);
|
2023-08-10 03:11:26 +02:00
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
return res.id;
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
2023-09-01 18:00:51 +02:00
|
|
|
})
|
2024-02-29 17:22:39 +01:00
|
|
|
.catch((error) => {
|
2024-02-27 17:37:37 +01:00
|
|
|
handleError(error, 'Unable to upload file');
|
2024-02-29 17:22:39 +01:00
|
|
|
const reason = getServerErrorMessage(error) || error;
|
2023-09-01 18:00:51 +02:00
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { state: UploadState.ERROR, error: reason });
|
|
|
|
return undefined;
|
|
|
|
});
|
2022-06-19 15:16:35 +02:00
|
|
|
}
|