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,
|
2024-05-24 02:26:22 +02:00
|
|
|
AssetMediaStatus,
|
2024-05-02 23:26:13 +02:00
|
|
|
checkBulkUpload,
|
2024-05-31 19:44:04 +02:00
|
|
|
getAssetOriginalPath,
|
2024-05-17 22:48:29 +02:00
|
|
|
getBaseUrl,
|
2024-05-02 23:26:13 +02:00
|
|
|
getSupportedMediaTypes,
|
2024-05-24 02:26:22 +02:00
|
|
|
type AssetMediaResponseDto,
|
2024-05-02 23:26:13 +02:00
|
|
|
} 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-05-24 02:26:22 +02:00
|
|
|
type FileUploadParam = { multiple?: boolean } & (
|
|
|
|
| { albumId?: string; assetId?: never }
|
|
|
|
| { albumId?: never; assetId?: string }
|
|
|
|
);
|
2024-06-12 12:57:11 +02:00
|
|
|
export const openFileUploadDialog = async (options: FileUploadParam = {}) => {
|
|
|
|
const { albumId, multiple = true, assetId } = options;
|
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';
|
2024-06-12 12:57:11 +02:00
|
|
|
fileSelector.multiple = multiple;
|
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
|
|
|
|
2024-05-24 02:26:22 +02:00
|
|
|
resolve(fileUploadHandler(files, albumId, assetId));
|
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
|
|
|
};
|
|
|
|
|
2024-05-24 02:26:22 +02:00
|
|
|
export const fileUploadHandler = async (files: File[], albumId?: string, assetId?: string): 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))) {
|
2024-05-24 02:26:22 +02:00
|
|
|
uploadAssetsStore.addNewUploadAsset({ id: getDeviceAssetId(file), file, albumId, assetId });
|
|
|
|
promises.push(uploadExecutionQueue.addTask(() => fileUploader(file, albumId, assetId)));
|
2023-09-01 18:00:51 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
2024-05-24 02:26:22 +02:00
|
|
|
async function fileUploader(assetFile: File, albumId?: string, replaceAssetId?: string): Promise<string | undefined> {
|
|
|
|
const fileCreatedAt = new Date(assetFile.lastModified).toISOString();
|
|
|
|
const deviceAssetId = getDeviceAssetId(assetFile);
|
2023-09-01 18:00:51 +02:00
|
|
|
|
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,
|
2024-05-24 02:26:22 +02:00
|
|
|
fileModifiedAt: new Date(assetFile.lastModified).toISOString(),
|
2024-05-02 23:26:13 +02:00
|
|
|
isFavorite: 'false',
|
|
|
|
duration: '0:00:00.000000',
|
2024-05-24 02:26:22 +02:00
|
|
|
assetData: new File([assetFile], assetFile.name),
|
2024-05-02 23:26:13 +02:00
|
|
|
})) {
|
|
|
|
formData.append(key, value);
|
|
|
|
}
|
2024-02-29 17:22:39 +01:00
|
|
|
|
2024-05-24 02:26:22 +02:00
|
|
|
let responseData: AssetMediaResponseDto | undefined;
|
2024-05-02 23:26:13 +02:00
|
|
|
const key = getKey();
|
|
|
|
if (crypto?.subtle?.digest && !key) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Hashing...' });
|
|
|
|
await tick();
|
|
|
|
try {
|
2024-05-24 02:26:22 +02:00
|
|
|
const bytes = await assetFile.arrayBuffer();
|
2024-05-02 23:26:13 +02:00
|
|
|
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],
|
2024-05-24 02:26:22 +02:00
|
|
|
} = await checkBulkUpload({ assetBulkUploadCheckDto: { assets: [{ id: assetFile.name, checksum }] } });
|
2024-05-02 23:26:13 +02:00
|
|
|
if (checkUploadResult.action === Action.Reject && checkUploadResult.assetId) {
|
2024-05-24 02:26:22 +02:00
|
|
|
responseData = { status: AssetMediaStatus.Duplicate, id: checkUploadResult.assetId };
|
2024-05-02 23:26:13 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-05-24 02:26:22 +02:00
|
|
|
console.error(`Error calculating sha1 file=${assetFile.name})`, error);
|
2024-05-02 23:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-29 17:22:39 +01:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
if (!responseData) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Uploading...' });
|
2024-05-24 02:26:22 +02:00
|
|
|
if (replaceAssetId) {
|
|
|
|
const response = await uploadRequest<AssetMediaResponseDto>({
|
2024-05-31 19:44:04 +02:00
|
|
|
url: getBaseUrl() + getAssetOriginalPath(replaceAssetId) + (key ? `?key=${key}` : ''),
|
2024-05-24 02:26:22 +02:00
|
|
|
method: 'PUT',
|
|
|
|
data: formData,
|
|
|
|
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
|
|
|
|
});
|
2024-06-05 15:29:52 +02:00
|
|
|
responseData = response.data;
|
2024-05-24 02:26:22 +02:00
|
|
|
} else {
|
2024-05-31 19:44:04 +02:00
|
|
|
const response = await uploadRequest<AssetMediaResponseDto>({
|
|
|
|
url: getBaseUrl() + '/assets' + (key ? `?key=${key}` : ''),
|
2024-05-24 02:26:22 +02:00
|
|
|
data: formData,
|
|
|
|
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
|
|
|
|
});
|
2024-05-31 19:44:04 +02:00
|
|
|
|
2024-05-24 02:26:22 +02:00
|
|
|
if (![200, 201].includes(response.status)) {
|
|
|
|
throw new Error('Failed to upload file');
|
|
|
|
}
|
2024-05-31 19:44:04 +02:00
|
|
|
|
2024-06-05 15:29:52 +02:00
|
|
|
responseData = response.data;
|
2024-05-02 23:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2024-06-05 15:29:52 +02:00
|
|
|
if (responseData.status === AssetMediaStatus.Duplicate) {
|
2024-05-02 23:26:13 +02:00
|
|
|
uploadAssetsStore.duplicateCounter.update((count) => count + 1);
|
|
|
|
} else {
|
|
|
|
uploadAssetsStore.successCounter.update((c) => c + 1);
|
2024-06-05 15:29:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (albumId) {
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Adding to album...' });
|
|
|
|
await addAssetsToAlbum(albumId, [responseData.id]);
|
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Added to album' });
|
2024-05-02 23:26:13 +02:00
|
|
|
}
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2024-05-24 02:26:22 +02:00
|
|
|
uploadAssetsStore.updateAsset(deviceAssetId, {
|
2024-06-05 15:29:52 +02:00
|
|
|
state: responseData.status === AssetMediaStatus.Duplicate ? UploadState.DUPLICATED : UploadState.DONE,
|
2024-05-24 02:26:22 +02:00
|
|
|
});
|
2023-08-10 03:11:26 +02:00
|
|
|
|
2024-05-02 23:26:13 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
}, 1000);
|
|
|
|
|
2024-06-05 15:29:52 +02:00
|
|
|
return responseData.id;
|
2024-05-02 23:26:13 +02:00
|
|
|
} 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
|
|
|
}
|