2022-08-26 18:36:54 +02:00
|
|
|
import {
|
|
|
|
notificationController,
|
|
|
|
NotificationType
|
|
|
|
} from './../components/shared-components/notification/notification';
|
2022-06-19 15:16:35 +02:00
|
|
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
|
|
|
import type { UploadAsset } from '../models/upload-asset';
|
2023-05-24 23:08:21 +02:00
|
|
|
import { AssetFileUploadResponseDto } from '@api';
|
2023-02-09 17:08:19 +01:00
|
|
|
import { addAssetsToAlbum, getFileMimeType, getFilenameExtension } from '$lib/utils/asset-utils';
|
2023-02-15 22:21:22 +01:00
|
|
|
import { mergeMap, filter, firstValueFrom, from, of, combineLatestAll } from 'rxjs';
|
|
|
|
import axios from 'axios';
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
export const openFileUploadDialog = async (
|
2022-12-30 03:07:18 +01:00
|
|
|
albumId: string | undefined = undefined,
|
2023-02-15 22:21:22 +01:00
|
|
|
sharedKey: string | undefined = undefined
|
2022-12-30 03:07:18 +01:00
|
|
|
) => {
|
2023-02-15 22:21:22 +01:00
|
|
|
return new Promise<(string | undefined)[]>((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const fileSelector = document.createElement('input');
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
fileSelector.type = 'file';
|
|
|
|
fileSelector.multiple = true;
|
2023-02-09 17:08:19 +01:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
// When adding a content type that is unsupported by browsers, make sure
|
|
|
|
// to also add it to getFileMimeType() otherwise the upload will fail.
|
|
|
|
fileSelector.accept = 'image/*,video/*,.heic,.heif,.dng,.3gp,.nef,.srw,.raf';
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
fileSelector.onchange = async (e: Event) => {
|
|
|
|
const target = e.target as HTMLInputElement;
|
|
|
|
if (!target.files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const files = Array.from<File>(target.files);
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
resolve(await fileUploadHandler(files, albumId, sharedKey));
|
|
|
|
};
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
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 (
|
|
|
|
files: File[],
|
|
|
|
albumId: string | undefined = undefined,
|
2023-02-15 22:21:22 +01:00
|
|
|
sharedKey: string | undefined = undefined
|
2023-01-09 21:16:08 +01:00
|
|
|
) => {
|
2023-02-15 22:21:22 +01:00
|
|
|
return firstValueFrom(
|
|
|
|
from(files).pipe(
|
|
|
|
filter((file) => {
|
|
|
|
const assetType = getFileMimeType(file).split('/')[0];
|
|
|
|
return assetType === 'video' || assetType === 'image';
|
|
|
|
}),
|
|
|
|
mergeMap(async (file) => of(await fileUploader(file, albumId, sharedKey)), 2),
|
|
|
|
combineLatestAll()
|
2023-02-13 20:18:11 +01:00
|
|
|
)
|
2023-02-15 22:21:22 +01:00
|
|
|
);
|
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(
|
|
|
|
asset: File,
|
|
|
|
albumId: string | undefined = undefined,
|
2023-02-15 22:21:22 +01:00
|
|
|
sharedKey: string | undefined = undefined
|
|
|
|
): Promise<string | undefined> {
|
2023-02-09 17:08:19 +01:00
|
|
|
const mimeType = getFileMimeType(asset);
|
|
|
|
const assetType = mimeType.split('/')[0].toUpperCase();
|
|
|
|
const fileExtension = getFilenameExtension(asset.name);
|
2022-06-19 15:16:35 +02:00
|
|
|
const formData = new FormData();
|
2023-02-19 17:44:53 +01:00
|
|
|
const fileCreatedAt = new Date(asset.lastModified).toISOString();
|
2023-02-13 20:18:11 +01:00
|
|
|
const deviceAssetId = 'web' + '-' + asset.name + '-' + asset.lastModified;
|
2022-06-19 15:16:35 +02:00
|
|
|
|
|
|
|
try {
|
2023-05-24 23:08:21 +02:00
|
|
|
// Create and add pseudo-unique ID of asset on the device
|
2022-06-19 15:16:35 +02:00
|
|
|
formData.append('deviceAssetId', deviceAssetId);
|
|
|
|
|
|
|
|
// Get device id - for web -> use WEB
|
|
|
|
formData.append('deviceId', 'WEB');
|
|
|
|
|
|
|
|
// Get asset type
|
|
|
|
formData.append('assetType', assetType);
|
|
|
|
|
|
|
|
// Get Asset Created Date
|
2023-02-19 17:44:53 +01:00
|
|
|
formData.append('fileCreatedAt', fileCreatedAt);
|
2022-06-19 15:16:35 +02:00
|
|
|
|
|
|
|
// Get Asset Modified At
|
2023-02-19 17:44:53 +01:00
|
|
|
formData.append('fileModifiedAt', new Date(asset.lastModified).toISOString());
|
2022-06-19 15:16:35 +02:00
|
|
|
|
|
|
|
// Set Asset is Favorite to false
|
|
|
|
formData.append('isFavorite', 'false');
|
|
|
|
|
|
|
|
// Get asset duration
|
|
|
|
formData.append('duration', '0:00:00.000000');
|
|
|
|
|
|
|
|
// Get asset file extension
|
|
|
|
formData.append('fileExtension', '.' + fileExtension);
|
|
|
|
|
2023-02-09 17:08:19 +01:00
|
|
|
// Get asset binary data with a custom MIME type, because browsers will
|
|
|
|
// use application/octet-stream for unsupported MIME types, leading to
|
|
|
|
// failed uploads.
|
|
|
|
formData.append('assetData', new File([asset], asset.name, { type: mimeType }));
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
const newUploadAsset: UploadAsset = {
|
|
|
|
id: deviceAssetId,
|
|
|
|
file: asset,
|
|
|
|
progress: 0,
|
|
|
|
fileExtension: fileExtension
|
2022-06-19 15:16:35 +02:00
|
|
|
};
|
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
uploadAssetsStore.addNewUploadAsset(newUploadAsset);
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2023-02-15 22:21:22 +01: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);
|
|
|
|
}
|
|
|
|
});
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
if (response.status == 200 || response.status == 201) {
|
|
|
|
const res: AssetFileUploadResponseDto = response.data;
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
if (albumId && res.id) {
|
|
|
|
await addAssetsToAlbum(albumId, [res.id], sharedKey);
|
|
|
|
}
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
}, 1000);
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2023-02-15 22:21:22 +01:00
|
|
|
return res.id;
|
|
|
|
}
|
2022-06-19 15:16:35 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log('error uploading file ', e);
|
2023-02-15 22:21:22 +01:00
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|