2022-12-30 03:07:18 +01:00
|
|
|
import {
|
|
|
|
notificationController,
|
|
|
|
NotificationType
|
|
|
|
} from '$lib/components/shared-components/notification/notification';
|
2023-06-30 18:24:28 +02:00
|
|
|
import { clearDownload, updateDownload } from '$lib/stores/download';
|
|
|
|
import {
|
|
|
|
AddAssetsResponseDto,
|
|
|
|
api,
|
|
|
|
AssetApiGetDownloadInfoRequest,
|
|
|
|
AssetResponseDto,
|
|
|
|
DownloadResponseDto
|
|
|
|
} from '@api';
|
|
|
|
import { handleError } from './handle-error';
|
2022-12-30 03:07:18 +01:00
|
|
|
|
|
|
|
export const addAssetsToAlbum = async (
|
|
|
|
albumId: string,
|
2023-01-09 21:16:08 +01:00
|
|
|
assetIds: Array<string>,
|
|
|
|
key: string | undefined = undefined
|
2022-12-30 03:07:18 +01:00
|
|
|
): Promise<AddAssetsResponseDto> =>
|
2023-05-28 03:52:22 +02:00
|
|
|
api.albumApi
|
|
|
|
.addAssetsToAlbum({ id: albumId, addAssetsDto: { assetIds }, key })
|
|
|
|
.then(({ data: dto }) => {
|
|
|
|
if (dto.successfullyAdded > 0) {
|
|
|
|
// This might be 0 if the user tries to add an asset that is already in the album
|
|
|
|
notificationController.show({
|
|
|
|
message: `Added ${dto.successfullyAdded} to ${dto.album?.albumName}`,
|
|
|
|
type: NotificationType.Info
|
|
|
|
});
|
|
|
|
}
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-05-28 03:52:22 +02:00
|
|
|
return dto;
|
|
|
|
});
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
const downloadBlob = (data: Blob, filename: string) => {
|
|
|
|
const url = URL.createObjectURL(data);
|
|
|
|
|
|
|
|
const anchor = document.createElement('a');
|
|
|
|
anchor.href = url;
|
|
|
|
anchor.download = filename;
|
|
|
|
|
|
|
|
document.body.appendChild(anchor);
|
|
|
|
anchor.click();
|
|
|
|
document.body.removeChild(anchor);
|
|
|
|
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const downloadArchive = async (
|
2023-01-09 21:16:08 +01:00
|
|
|
fileName: string,
|
2023-06-30 18:24:28 +02:00
|
|
|
options: Omit<AssetApiGetDownloadInfoRequest, 'key'>,
|
2023-05-16 16:13:20 +02:00
|
|
|
onDone?: () => void,
|
2023-01-09 21:16:08 +01:00
|
|
|
key?: string
|
2023-06-30 18:24:28 +02:00
|
|
|
) => {
|
|
|
|
let downloadInfo: DownloadResponseDto | null = null;
|
2023-01-09 21:16:08 +01:00
|
|
|
|
|
|
|
try {
|
2023-06-30 18:24:28 +02:00
|
|
|
const { data } = await api.assetApi.getDownloadInfo({ ...options, key });
|
|
|
|
downloadInfo = data;
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Unable to download files');
|
|
|
|
return;
|
|
|
|
}
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
// TODO: prompt for big download
|
|
|
|
// const total = downloadInfo.totalSize;
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
for (let i = 0; i < downloadInfo.archives.length; i++) {
|
|
|
|
const archive = downloadInfo.archives[i];
|
|
|
|
const suffix = downloadInfo.archives.length === 1 ? '' : `+${i + 1}`;
|
|
|
|
const archiveName = fileName.replace('.zip', `${suffix}.zip`);
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
let downloadKey = `${archiveName}`;
|
|
|
|
if (downloadInfo.archives.length > 1) {
|
|
|
|
downloadKey = `${archiveName} (${i + 1}/${downloadInfo.archives.length})`;
|
|
|
|
}
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
updateDownload(downloadKey, 0);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { data } = await api.assetApi.downloadArchive(
|
|
|
|
{ assetIdsDto: { assetIds: archive.assetIds }, key },
|
2023-05-28 03:52:22 +02:00
|
|
|
{
|
|
|
|
responseType: 'blob',
|
2023-06-30 18:24:28 +02:00
|
|
|
onDownloadProgress: (event) =>
|
|
|
|
updateDownload(downloadKey, Math.floor((event.loaded / archive.size) * 100))
|
2023-01-09 21:16:08 +01:00
|
|
|
}
|
2023-05-28 03:52:22 +02:00
|
|
|
);
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
downloadBlob(data, archiveName);
|
|
|
|
} catch (e) {
|
|
|
|
handleError(e, 'Unable to download files');
|
|
|
|
clearDownload(downloadKey);
|
|
|
|
return;
|
|
|
|
} finally {
|
|
|
|
setTimeout(() => clearDownload(downloadKey), 3_000);
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
onDone?.();
|
|
|
|
};
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
export const downloadFile = async (asset: AssetResponseDto, key?: string) => {
|
|
|
|
const filenames = [`${asset.originalFileName}.${getFilenameExtension(asset.originalPath)}`];
|
|
|
|
if (asset.livePhotoVideoId) {
|
|
|
|
filenames.push(`${asset.originalFileName}.mov`);
|
|
|
|
}
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
for (const filename of filenames) {
|
|
|
|
try {
|
|
|
|
updateDownload(filename, 0);
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
const { data } = await api.assetApi.downloadFile(
|
|
|
|
{ id: asset.id, key },
|
|
|
|
{
|
|
|
|
responseType: 'blob',
|
|
|
|
onDownloadProgress: (event: ProgressEvent) => {
|
|
|
|
if (event.lengthComputable) {
|
|
|
|
updateDownload(filename, Math.floor((event.loaded / event.total) * 100));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-06-30 18:24:28 +02:00
|
|
|
downloadBlob(data, filename);
|
|
|
|
} catch (e) {
|
|
|
|
handleError(e, `Error downloading ${filename}`);
|
|
|
|
} finally {
|
|
|
|
setTimeout(() => clearDownload(filename), 3_000);
|
2023-01-09 21:16:08 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-30 18:24:28 +02:00
|
|
|
};
|
2023-02-09 17:08:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the lowercase filename extension without a dot (.) and
|
|
|
|
* an empty string when not found.
|
|
|
|
*/
|
|
|
|
export function getFilenameExtension(filename: string): string {
|
2023-05-28 03:53:29 +02:00
|
|
|
const lastIndex = Math.max(0, filename.lastIndexOf('.'));
|
|
|
|
const startIndex = (lastIndex || Infinity) + 1;
|
|
|
|
return filename.slice(startIndex).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the filename of an asset including file extension
|
|
|
|
*/
|
|
|
|
export function getAssetFilename(asset: AssetResponseDto): string {
|
|
|
|
const fileExtension = getFilenameExtension(asset.originalPath);
|
|
|
|
return `${asset.originalFileName}.${fileExtension}`;
|
2023-02-09 17:08:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the MIME type of the file and an empty string when not found.
|
|
|
|
*/
|
|
|
|
export function getFileMimeType(file: File): string {
|
2023-06-15 04:34:03 +02:00
|
|
|
const mimeTypes: Record<string, string> = {
|
2023-06-21 14:29:02 +02:00
|
|
|
'3fr': 'image/x-hasselblad-3fr',
|
2023-06-15 04:34:03 +02:00
|
|
|
'3gp': 'video/3gpp',
|
2023-06-21 14:29:02 +02:00
|
|
|
ari: 'image/x-arriflex-ari',
|
2023-06-15 04:34:03 +02:00
|
|
|
arw: 'image/x-sony-arw',
|
2023-06-28 16:21:42 +02:00
|
|
|
avi: 'video/avi',
|
2023-06-21 14:29:02 +02:00
|
|
|
avif: 'image/avif',
|
|
|
|
cap: 'image/x-phaseone-cap',
|
|
|
|
cin: 'image/x-phantom-cin',
|
|
|
|
cr2: 'image/x-canon-cr2',
|
|
|
|
cr3: 'image/x-canon-cr3',
|
|
|
|
crw: 'image/x-canon-crw',
|
|
|
|
dcr: 'image/x-kodak-dcr',
|
2023-06-21 20:50:12 +02:00
|
|
|
dng: 'image/x-adobe-dng',
|
2023-06-21 14:29:02 +02:00
|
|
|
erf: 'image/x-epson-erf',
|
|
|
|
fff: 'image/x-hasselblad-fff',
|
2023-06-21 20:50:12 +02:00
|
|
|
flv: 'video/x-flv',
|
|
|
|
gif: 'image/gif',
|
2023-06-15 04:34:03 +02:00
|
|
|
heic: 'image/heic',
|
|
|
|
heif: 'image/heif',
|
2023-06-21 14:29:02 +02:00
|
|
|
iiq: 'image/x-phaseone-iiq',
|
2023-06-15 04:34:03 +02:00
|
|
|
insp: 'image/jpeg',
|
|
|
|
insv: 'video/mp4',
|
2023-06-21 20:50:12 +02:00
|
|
|
jpeg: 'image/jpeg',
|
|
|
|
jpg: 'image/jpeg',
|
2023-06-21 14:29:02 +02:00
|
|
|
jxl: 'image/jxl',
|
2023-06-20 04:10:29 +02:00
|
|
|
k25: 'image/x-kodak-k25',
|
|
|
|
kdc: 'image/x-kodak-kdc',
|
2023-06-21 20:50:12 +02:00
|
|
|
m2ts: 'video/mp2t',
|
|
|
|
mkv: 'video/x-matroska',
|
|
|
|
mov: 'video/quicktime',
|
|
|
|
mp4: 'video/mp4',
|
|
|
|
mpg: 'video/mpeg',
|
2023-06-20 04:10:29 +02:00
|
|
|
mrw: 'image/x-minolta-mrw',
|
2023-06-21 20:50:12 +02:00
|
|
|
mts: 'video/mp2t',
|
2023-06-21 14:29:02 +02:00
|
|
|
nef: 'image/x-nikon-nef',
|
2023-06-20 04:10:29 +02:00
|
|
|
orf: 'image/x-olympus-orf',
|
2023-06-21 14:29:02 +02:00
|
|
|
ori: 'image/x-olympus-ori',
|
2023-06-20 04:10:29 +02:00
|
|
|
pef: 'image/x-pentax-pef',
|
2023-06-21 20:50:12 +02:00
|
|
|
png: 'image/png',
|
2023-06-21 14:29:02 +02:00
|
|
|
raf: 'image/x-fuji-raf',
|
|
|
|
raw: 'image/x-panasonic-raw',
|
2023-06-20 04:10:29 +02:00
|
|
|
rwl: 'image/x-leica-rwl',
|
2023-06-21 14:29:02 +02:00
|
|
|
sr2: 'image/x-sony-sr2',
|
|
|
|
srf: 'image/x-sony-srf',
|
|
|
|
srw: 'image/x-samsung-srw',
|
2023-06-21 20:50:12 +02:00
|
|
|
tiff: 'image/tiff',
|
|
|
|
webm: 'video/webm',
|
|
|
|
webp: 'image/webp',
|
|
|
|
wmv: 'video/x-ms-wmv',
|
2023-06-21 14:29:02 +02:00
|
|
|
x3f: 'image/x-sigma-x3f'
|
2023-06-15 04:34:03 +02:00
|
|
|
};
|
|
|
|
// Return the MIME type determined by the browser or the MIME type based on the file extension.
|
|
|
|
return file.type || (mimeTypes[getFilenameExtension(file.name)] ?? '');
|
2023-02-09 17:08:19 +01:00
|
|
|
}
|
2023-06-08 17:22:45 +02:00
|
|
|
|
2023-06-29 15:11:00 +02:00
|
|
|
function isRotated90CW(orientation: number) {
|
|
|
|
return orientation == 6 || orientation == 90;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRotated270CW(orientation: number) {
|
|
|
|
return orientation == 8 || orientation == -90;
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:22:45 +02:00
|
|
|
/**
|
|
|
|
* Returns aspect ratio for the asset
|
|
|
|
*/
|
|
|
|
export function getAssetRatio(asset: AssetResponseDto) {
|
|
|
|
let height = asset.exifInfo?.exifImageHeight || 235;
|
|
|
|
let width = asset.exifInfo?.exifImageWidth || 235;
|
|
|
|
const orientation = Number(asset.exifInfo?.orientation);
|
|
|
|
if (orientation) {
|
2023-06-29 15:11:00 +02:00
|
|
|
if (isRotated90CW(orientation) || isRotated270CW(orientation)) {
|
2023-06-08 17:22:45 +02:00
|
|
|
[width, height] = [height, width];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { width, height };
|
|
|
|
}
|