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-05-02 23:26:13 +02:00
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
checkBulkUpload,
|
|
|
|
defaults,
|
|
|
|
getSupportedMediaTypes,
|
|
|
|
type AssetFileUploadResponseDto,
|
|
|
|
} from '@immich/sdk';
|
|
|
|
import { tick } from 'svelte';
|
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);
|
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
uploadAssetsStore.markStarted(deviceAssetId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
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);
|
|
|
|
}
|
2024-02-29 17:22:39 +01:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
let responseData: AssetFileUploadResponseDto | undefined;
|
|
|
|
const key = getKey();
|
|
|
|
if (crypto?.subtle?.digest && !key) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Hashing...' });
|
|
|
|
await tick();
|
|
|
|
try {
|
|
|
|
const bytes = await asset.arrayBuffer();
|
|
|
|
const hash = await crypto.subtle.digest('SHA-1', bytes);
|
|
|
|
const checksum = Array.from(new Uint8Array(hash))
|
|
|
|
.map((b) => b.toString(16).padStart(2, '0'))
|
|
|
|
.join('');
|
|
|
|
|
|
|
|
const {
|
|
|
|
results: [checkUploadResult],
|
|
|
|
} = await checkBulkUpload({ assetBulkUploadCheckDto: { assets: [{ id: asset.name, checksum }] } });
|
|
|
|
if (checkUploadResult.action === Action.Reject && checkUploadResult.assetId) {
|
|
|
|
responseData = { duplicate: true, id: checkUploadResult.assetId };
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error calculating sha1 file=${asset.name})`, error);
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 17:22:39 +01:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
if (!responseData) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Uploading...' });
|
|
|
|
const response = await uploadRequest<AssetFileUploadResponseDto>({
|
2024-02-29 17:22:39 +01:00
|
|
|
url: defaults.baseUrl + '/asset/upload' + (key ? `?key=${key}` : ''),
|
|
|
|
data: formData,
|
|
|
|
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
|
|
|
|
});
|
2024-05-02 23:26:13 +02:00
|
|
|
if (![200, 201].includes(response.status)) {
|
|
|
|
throw new Error('Failed to upload file');
|
|
|
|
}
|
|
|
|
responseData = response.data;
|
|
|
|
}
|
|
|
|
const { duplicate, id: assetId } = responseData;
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
if (duplicate) {
|
|
|
|
uploadAssetsStore.duplicateCounter.update((count) => count + 1);
|
|
|
|
} else {
|
|
|
|
uploadAssetsStore.successCounter.update((c) => c + 1);
|
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
if (albumId && assetId) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Adding to album...' });
|
|
|
|
await addAssetsToAlbum(albumId, [assetId]);
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Added to album' });
|
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { state: duplicate ? UploadState.DUPLICATED : UploadState.DONE });
|
2023-08-10 03:11:26 +02:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
return assetId;
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Unable to upload file');
|
|
|
|
const reason = getServerErrorMessage(error) || error;
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { state: UploadState.ERROR, error: reason });
|
|
|
|
return;
|
|
|
|
}
|
2022-06-19 15:16:35 +02:00
|
|
|
}
|