2022-07-11 04:41:45 +02:00
|
|
|
/* @vite-ignore */
|
2022-06-19 15:16:35 +02:00
|
|
|
import * as exifr from 'exifr';
|
|
|
|
import { serverEndpoint } from '../constants';
|
|
|
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
|
|
|
import type { UploadAsset } from '../models/upload-asset';
|
2022-07-11 04:41:45 +02:00
|
|
|
import { api } from '@api';
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2022-07-26 19:28:07 +02:00
|
|
|
export async function fileUploader(asset: File) {
|
2022-06-19 15:16:35 +02:00
|
|
|
const assetType = asset.type.split('/')[0].toUpperCase();
|
|
|
|
const temp = asset.name.split('.');
|
|
|
|
const fileExtension = temp[temp.length - 1];
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
try {
|
|
|
|
let exifData = null;
|
|
|
|
|
|
|
|
if (assetType !== 'VIDEO') {
|
|
|
|
exifData = await exifr.parse(asset);
|
|
|
|
}
|
|
|
|
|
|
|
|
const createdAt =
|
|
|
|
exifData && exifData.DateTimeOriginal != null
|
|
|
|
? new Date(exifData.DateTimeOriginal).toISOString()
|
|
|
|
: new Date(asset.lastModified).toISOString();
|
|
|
|
|
|
|
|
const deviceAssetId = 'web' + '-' + asset.name + '-' + asset.lastModified;
|
|
|
|
|
|
|
|
// Create and add Unique ID of asset on the device
|
|
|
|
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
|
|
|
|
formData.append('createdAt', createdAt);
|
|
|
|
|
|
|
|
// Get Asset Modified At
|
|
|
|
formData.append('modifiedAt', new Date(asset.lastModified).toISOString());
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Get asset binary data.
|
|
|
|
formData.append('assetData', asset);
|
|
|
|
|
|
|
|
// Check if asset upload on server before performing upload
|
|
|
|
|
2022-07-11 04:41:45 +02:00
|
|
|
const { data, status } = await api.assetApi.checkDuplicateAsset({
|
|
|
|
deviceAssetId: String(deviceAssetId),
|
2022-07-26 19:28:07 +02:00
|
|
|
deviceId: 'WEB'
|
2022-07-11 04:41:45 +02:00
|
|
|
});
|
2022-06-19 15:16:35 +02:00
|
|
|
|
2022-07-11 04:41:45 +02:00
|
|
|
if (status === 200) {
|
|
|
|
if (data.isExist) {
|
2022-06-19 15:16:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
request.upload.onloadstart = () => {
|
|
|
|
const newUploadAsset: UploadAsset = {
|
|
|
|
id: deviceAssetId,
|
|
|
|
file: asset,
|
|
|
|
progress: 0,
|
2022-07-26 19:28:07 +02:00
|
|
|
fileExtension: fileExtension
|
2022-06-19 15:16:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
uploadAssetsStore.addNewUploadAsset(newUploadAsset);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.upload.onload = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
2022-07-01 19:00:12 +02:00
|
|
|
}, 1000);
|
2022-06-19 15:16:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// listen for `error` event
|
|
|
|
request.upload.onerror = () => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
};
|
|
|
|
|
|
|
|
// listen for `abort` event
|
|
|
|
request.upload.onabort = () => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
|
|
|
};
|
|
|
|
|
|
|
|
// listen for `progress` event
|
|
|
|
request.upload.onprogress = (event) => {
|
|
|
|
const percentComplete = Math.floor((event.loaded / event.total) * 100);
|
|
|
|
uploadAssetsStore.updateProgress(deviceAssetId, percentComplete);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.open('POST', `${serverEndpoint}/asset/upload`);
|
|
|
|
|
|
|
|
request.send(formData);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('error uploading file ', e);
|
|
|
|
}
|
|
|
|
}
|