2023-06-02 15:55:08 +02:00
|
|
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
2023-07-17 18:22:29 +02:00
|
|
|
import { addAssetsToAlbum } from '$lib/utils/asset-utils';
|
2024-01-20 19:47:41 +01:00
|
|
|
import { api, type AssetFileUploadResponseDto } from '@api';
|
2023-09-01 18:00:51 +02:00
|
|
|
import { UploadState } from '$lib/models/upload-asset';
|
|
|
|
import { ExecutorQueue } from '$lib/utils/executor-queue';
|
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) {
|
|
|
|
const { data } = await api.serverInfoApi.getSupportedMediaTypes();
|
|
|
|
_extensions = [...data.image, ...data.video];
|
|
|
|
}
|
|
|
|
return _extensions;
|
|
|
|
};
|
2023-07-12 05:56:30 +02:00
|
|
|
|
2023-08-25 06:03:28 +02:00
|
|
|
export const openFileUploadDialog = async (albumId: string | undefined = 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(',');
|
2023-07-01 06:50:47 +02:00
|
|
|
fileSelector.onchange = async (e: Event) => {
|
|
|
|
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));
|
2023-07-01 06:50:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
fileSelector.click();
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Error selecting file', e);
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
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();
|
|
|
|
if (extensions.some((ext) => name.endsWith(ext))) {
|
|
|
|
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)))
|
|
|
|
.then(() =>
|
|
|
|
api.assetApi.uploadFile(
|
|
|
|
{
|
|
|
|
deviceAssetId,
|
|
|
|
deviceId: 'WEB',
|
|
|
|
fileCreatedAt,
|
|
|
|
fileModifiedAt: new Date(asset.lastModified).toISOString(),
|
|
|
|
isFavorite: false,
|
|
|
|
duration: '0:00:00.000000',
|
|
|
|
assetData: new File([asset], asset.name),
|
|
|
|
key: api.getKey(),
|
|
|
|
},
|
|
|
|
{
|
2024-01-26 17:40:28 +01:00
|
|
|
onUploadProgress: ({ event }) => {
|
|
|
|
const { loaded, total } = event;
|
2023-09-01 18:00:51 +02:00
|
|
|
uploadAssetsStore.updateProgress(deviceAssetId, loaded, total);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.then(async (response) => {
|
|
|
|
if (response.status == 200 || response.status == 201) {
|
|
|
|
const res: AssetFileUploadResponseDto = response.data;
|
|
|
|
|
|
|
|
if (res.duplicate) {
|
|
|
|
uploadAssetsStore.duplicateCounter.update((count) => count + 1);
|
|
|
|
}
|
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,
|
|
|
|
});
|
|
|
|
uploadAssetsStore.successCounter.update((c) => c + 1);
|
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-01-13 01:43:36 +01:00
|
|
|
.catch(async (error) => {
|
|
|
|
await handleError(error, 'Unable to upload file');
|
|
|
|
const reason = (await 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
|
|
|
}
|