2023-06-02 15:55:08 +02:00
|
|
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
2023-07-12 05:56:30 +02:00
|
|
|
import { addAssetsToAlbum, getFilenameExtension } from '$lib/utils/asset-utils';
|
2023-06-02 15:55:08 +02:00
|
|
|
import type { AssetFileUploadResponseDto } from '@api';
|
|
|
|
import axios from 'axios';
|
|
|
|
import { combineLatestAll, filter, firstValueFrom, from, mergeMap, of } from 'rxjs';
|
2023-07-01 06:50:47 +02:00
|
|
|
import { notificationController, NotificationType } from './../components/shared-components/notification/notification';
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-07-12 05:56:30 +02:00
|
|
|
const extensions = [
|
|
|
|
'.3fr',
|
|
|
|
'.3gp',
|
|
|
|
'.ari',
|
|
|
|
'.arw',
|
|
|
|
'.avi',
|
|
|
|
'.avif',
|
|
|
|
'.cap',
|
|
|
|
'.cin',
|
|
|
|
'.cr2',
|
|
|
|
'.cr3',
|
|
|
|
'.crw',
|
|
|
|
'.dcr',
|
|
|
|
'.dng',
|
|
|
|
'.erf',
|
|
|
|
'.fff',
|
|
|
|
'.flv',
|
|
|
|
'.gif',
|
|
|
|
'.heic',
|
|
|
|
'.heif',
|
|
|
|
'.iiq',
|
|
|
|
'.jpeg',
|
|
|
|
'.jpg',
|
|
|
|
'.k25',
|
|
|
|
'.kdc',
|
|
|
|
'.mkv',
|
|
|
|
'.mov',
|
|
|
|
'.mp2t',
|
|
|
|
'.mp4',
|
|
|
|
'.mpeg',
|
|
|
|
'.mrw',
|
|
|
|
'.nef',
|
|
|
|
'.orf',
|
|
|
|
'.ori',
|
|
|
|
'.pef',
|
|
|
|
'.png',
|
|
|
|
'.raf',
|
|
|
|
'.raw',
|
|
|
|
'.rwl',
|
|
|
|
'.sr2',
|
|
|
|
'.srf',
|
|
|
|
'.srw',
|
|
|
|
'.tiff',
|
|
|
|
'.webm',
|
|
|
|
'.webp',
|
|
|
|
'.wmv',
|
|
|
|
'.x3f',
|
|
|
|
];
|
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
export const openFileUploadDialog = async (
|
2023-07-01 06:50:47 +02:00
|
|
|
albumId: string | undefined = undefined,
|
|
|
|
sharedKey: string | undefined = undefined,
|
2022-12-30 03:07:18 +01:00
|
|
|
) => {
|
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;
|
|
|
|
}
|
|
|
|
const files = Array.from<File>(target.files);
|
|
|
|
|
|
|
|
resolve(await fileUploadHandler(files, albumId, sharedKey));
|
|
|
|
};
|
|
|
|
|
|
|
|
fileSelector.click();
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Error selecting file', e);
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
2022-07-27 03:53:25 +02:00
|
|
|
};
|
|
|
|
|
2023-01-09 21:16:08 +01:00
|
|
|
export const fileUploadHandler = async (
|
2023-07-01 06:50:47 +02:00
|
|
|
files: File[],
|
|
|
|
albumId: string | undefined = undefined,
|
|
|
|
sharedKey: string | undefined = undefined,
|
2023-01-09 21:16:08 +01:00
|
|
|
) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
return firstValueFrom(
|
|
|
|
from(files).pipe(
|
2023-07-12 05:56:30 +02:00
|
|
|
filter((file) => extensions.includes('.' + getFilenameExtension(file.name))),
|
2023-07-01 06:50:47 +02:00
|
|
|
mergeMap(async (file) => of(await fileUploader(file, albumId, sharedKey)), 2),
|
|
|
|
combineLatestAll(),
|
|
|
|
),
|
|
|
|
);
|
2022-12-30 03:07:18 +01:00
|
|
|
};
|
|
|
|
|
2022-09-08 17:30:49 +02:00
|
|
|
//TODO: should probably use the @api SDK
|
2023-01-09 21:16:08 +01:00
|
|
|
async function fileUploader(
|
2023-07-01 06:50:47 +02:00
|
|
|
asset: File,
|
|
|
|
albumId: string | undefined = undefined,
|
|
|
|
sharedKey: string | undefined = undefined,
|
2023-02-15 22:21:22 +01:00
|
|
|
): Promise<string | undefined> {
|
2023-07-01 06:50:47 +02:00
|
|
|
const formData = new FormData();
|
|
|
|
const fileCreatedAt = new Date(asset.lastModified).toISOString();
|
|
|
|
const deviceAssetId = 'web' + '-' + asset.name + '-' + asset.lastModified;
|
|
|
|
|
|
|
|
try {
|
|
|
|
formData.append('deviceAssetId', deviceAssetId);
|
|
|
|
formData.append('deviceId', 'WEB');
|
|
|
|
formData.append('fileCreatedAt', fileCreatedAt);
|
|
|
|
formData.append('fileModifiedAt', new Date(asset.lastModified).toISOString());
|
|
|
|
formData.append('isFavorite', 'false');
|
|
|
|
formData.append('duration', '0:00:00.000000');
|
2023-07-12 05:56:30 +02:00
|
|
|
formData.append('assetData', new File([asset], asset.name));
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-07-12 05:56:30 +02:00
|
|
|
uploadAssetsStore.addNewUploadAsset({
|
2023-07-01 06:50:47 +02:00
|
|
|
id: deviceAssetId,
|
|
|
|
file: asset,
|
|
|
|
progress: 0,
|
2023-07-12 05:56:30 +02:00
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
const response = await axios.post(`/api/asset/upload`, formData, {
|
|
|
|
params: {
|
|
|
|
key: sharedKey,
|
|
|
|
},
|
|
|
|
onUploadProgress: (event) => {
|
|
|
|
const percentComplete = Math.floor((event.loaded / event.total) * 100);
|
|
|
|
uploadAssetsStore.updateProgress(deviceAssetId, percentComplete);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.status == 200 || response.status == 201) {
|
|
|
|
const res: AssetFileUploadResponseDto = response.data;
|
|
|
|
|
|
|
|
if (albumId && res.id) {
|
|
|
|
await addAssetsToAlbum(albumId, [res.id], sharedKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
return res.id;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log('error uploading file ', e);
|
|
|
|
handleUploadError(asset, JSON.stringify(e));
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
}
|
2022-06-19 15:16:35 +02:00
|
|
|
}
|
2022-12-30 03:07:18 +01:00
|
|
|
|
|
|
|
function handleUploadError(asset: File, respBody = '{}', extraMessage?: string) {
|
2023-07-01 06:50:47 +02:00
|
|
|
try {
|
|
|
|
const res = JSON.parse(respBody);
|
|
|
|
|
|
|
|
const extraMsg = res ? ' ' + res?.message : '';
|
|
|
|
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Error,
|
|
|
|
message: `Cannot upload file ${asset.name} ${extraMsg}${extraMessage}`,
|
|
|
|
timeout: 5000,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error('ERROR parsing data JSON in handleUploadError');
|
|
|
|
}
|
2022-08-31 15:12:31 +02:00
|
|
|
}
|