2022-08-26 18:36:54 +02:00
|
|
|
import {
|
|
|
|
notificationController,
|
|
|
|
NotificationType
|
|
|
|
} from './../components/shared-components/notification/notification';
|
2022-07-11 04:41:45 +02:00
|
|
|
/* @vite-ignore */
|
2022-06-19 15:16:35 +02:00
|
|
|
import * as exifr from 'exifr';
|
|
|
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
|
|
|
import type { UploadAsset } from '../models/upload-asset';
|
2022-07-27 03:53:25 +02:00
|
|
|
import { api, AssetFileUploadResponseDto } from '@api';
|
|
|
|
import { albumUploadAssetStore } from '$lib/stores/album-upload-asset';
|
|
|
|
/**
|
|
|
|
* Determine if the upload is for album or for the user general backup
|
|
|
|
* @variant GENERAL - Upload assets to the server for general backup
|
|
|
|
* @variant ALBUM - Upload assets to the server for backup and add to the album
|
|
|
|
*/
|
|
|
|
export enum UploadType {
|
|
|
|
/**
|
|
|
|
* Upload assets to the server
|
|
|
|
*/
|
|
|
|
GENERAL = 'GENERAL',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload assets to the server and add to album
|
|
|
|
*/
|
|
|
|
ALBUM = 'ALBUM'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const openFileUploadDialog = (uploadType: UploadType) => {
|
|
|
|
try {
|
2022-09-08 17:30:49 +02:00
|
|
|
const fileSelector = document.createElement('input');
|
2022-07-27 03:53:25 +02:00
|
|
|
|
|
|
|
fileSelector.type = 'file';
|
|
|
|
fileSelector.multiple = true;
|
2022-09-24 02:09:45 +02:00
|
|
|
fileSelector.accept = 'image/*,video/*,.heic,.heif,.dng,.3gp,.nef';
|
2022-07-27 03:53:25 +02:00
|
|
|
|
2022-09-08 17:30:49 +02: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
|
|
|
|
2022-08-26 18:36:54 +02:00
|
|
|
if (files.length > 50) {
|
|
|
|
notificationController.show({
|
2022-08-26 19:01:47 +02:00
|
|
|
type: NotificationType.Error,
|
2022-08-26 18:42:48 +02:00
|
|
|
message: `Cannot upload more than 50 files at a time - you are uploading ${files.length} files.
|
2022-11-17 06:11:15 +01:00
|
|
|
Please check out <u>the bulk upload documentation</u> if you need to upload more than 50 files.`,
|
|
|
|
timeout: 10000,
|
2022-12-21 22:01:50 +01:00
|
|
|
action: { type: 'link', target: 'https://immich.app/docs/features/bulk-upload' }
|
2022-08-26 18:36:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-27 03:53:25 +02:00
|
|
|
const acceptedFile = files.filter(
|
|
|
|
(e) => e.type.split('/')[0] === 'video' || e.type.split('/')[0] === 'image'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (uploadType === UploadType.ALBUM) {
|
|
|
|
albumUploadAssetStore.asset.set([]);
|
|
|
|
albumUploadAssetStore.count.set(acceptedFile.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const asset of acceptedFile) {
|
|
|
|
await fileUploader(asset, uploadType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fileSelector.click();
|
|
|
|
} catch (e) {
|
2022-08-31 15:12:31 +02:00
|
|
|
console.log('Error selecting file', e);
|
2022-07-27 03:53:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-08 17:30:49 +02:00
|
|
|
//TODO: should probably use the @api SDK
|
2022-07-27 03:53:25 +02:00
|
|
|
async function fileUploader(asset: File, uploadType: UploadType) {
|
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') {
|
2022-08-13 04:52:30 +02:00
|
|
|
exifData = await exifr.parse(asset).catch((e) => console.log('error parsing exif', e));
|
2022-06-19 15:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-08 17:30:49 +02:00
|
|
|
const dataId = data.id;
|
|
|
|
if (uploadType === UploadType.ALBUM && dataId) {
|
2022-07-27 03:53:25 +02:00
|
|
|
albumUploadAssetStore.asset.update((a) => {
|
2022-09-08 17:30:49 +02:00
|
|
|
return [...a, dataId];
|
2022-07-27 03:53:25 +02:00
|
|
|
});
|
|
|
|
}
|
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);
|
|
|
|
};
|
|
|
|
|
2022-09-08 17:30:49 +02:00
|
|
|
request.upload.onload = () => {
|
2022-06-19 15:16:35 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
uploadAssetsStore.removeUploadAsset(deviceAssetId);
|
2022-07-01 19:00:12 +02:00
|
|
|
}, 1000);
|
2022-06-19 15:16:35 +02:00
|
|
|
};
|
|
|
|
|
2022-07-27 03:53:25 +02:00
|
|
|
request.onreadystatechange = () => {
|
|
|
|
try {
|
|
|
|
if (request.readyState === 4 && uploadType === UploadType.ALBUM) {
|
2022-08-31 15:12:31 +02:00
|
|
|
const res: AssetFileUploadResponseDto = JSON.parse(request.response || '{}');
|
2022-07-27 03:53:25 +02:00
|
|
|
|
|
|
|
albumUploadAssetStore.asset.update((assets) => {
|
2022-08-31 15:12:31 +02:00
|
|
|
return [...assets, res?.id || ''];
|
2022-07-27 03:53:25 +02:00
|
|
|
});
|
2022-08-31 15:12:31 +02:00
|
|
|
|
|
|
|
if (request.status !== 201) {
|
|
|
|
handleUploadError(asset, res);
|
|
|
|
}
|
2022-07-27 03:53:25 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error('ERROR parsing data JSON in upload onreadystatechange');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-19 15:16:35 +02:00
|
|
|
// listen for `error` event
|
2022-09-08 17:30:49 +02:00
|
|
|
request.upload.onerror = () => {
|
2022-06-19 15:16:35 +02:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2022-08-07 15:12:31 +02:00
|
|
|
request.open('POST', `/api/asset/upload`);
|
2022-06-19 15:16:35 +02:00
|
|
|
|
|
|
|
request.send(formData);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('error uploading file ', e);
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 17:30:49 +02:00
|
|
|
// TODO: This should have a proper type
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-10-25 16:51:03 +02:00
|
|
|
function handleUploadError(asset: File, respBody: any, extraMessage?: string) {
|
|
|
|
const extraMsg = respBody ? ' ' + respBody?.message : '';
|
2022-08-31 15:12:31 +02:00
|
|
|
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Error,
|
2022-10-25 16:51:03 +02:00
|
|
|
message: `Cannot upload file ${asset.name} ${extraMsg}${extraMessage}`,
|
2022-08-31 15:12:31 +02:00
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
}
|