2023-07-01 06:50:47 +02:00
|
|
|
import { notificationController, NotificationType } from '$lib/components/shared-components/notification/notification';
|
2023-07-15 03:25:13 +02:00
|
|
|
import { downloadManager } from '$lib/stores/download';
|
2023-08-15 17:49:32 +02:00
|
|
|
import { api, BulkIdResponseDto, AssetResponseDto, DownloadResponseDto, DownloadInfoDto } from '@api';
|
2023-06-30 18:24:28 +02:00
|
|
|
import { handleError } from './handle-error';
|
2022-12-30 03:07:18 +01:00
|
|
|
|
2023-08-25 06:03:28 +02:00
|
|
|
export const addAssetsToAlbum = async (albumId: string, assetIds: Array<string>): Promise<BulkIdResponseDto[]> =>
|
|
|
|
api.albumApi
|
|
|
|
.addAssetsToAlbum({
|
|
|
|
id: albumId,
|
|
|
|
bulkIdsDto: { ids: assetIds },
|
|
|
|
key: api.getKey(),
|
|
|
|
})
|
|
|
|
.then(({ data: results }) => {
|
|
|
|
const count = results.filter(({ success }) => success).length;
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: `Added ${count} asset${count === 1 ? '' : 's'}`,
|
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-08-25 06:03:28 +02:00
|
|
|
return results;
|
|
|
|
});
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-08-25 19:44:52 +02:00
|
|
|
export const downloadBlob = (data: Blob, filename: string) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const url = URL.createObjectURL(data);
|
2023-06-30 18:24:28 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
const anchor = document.createElement('a');
|
|
|
|
anchor.href = url;
|
|
|
|
anchor.download = filename;
|
2023-06-30 18:24:28 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
document.body.appendChild(anchor);
|
|
|
|
anchor.click();
|
|
|
|
document.body.removeChild(anchor);
|
2023-06-30 18:24:28 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
URL.revokeObjectURL(url);
|
2023-06-30 18:24:28 +02:00
|
|
|
};
|
|
|
|
|
2023-08-25 06:03:28 +02:00
|
|
|
export const downloadArchive = async (fileName: string, options: DownloadInfoDto) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
let downloadInfo: DownloadResponseDto | null = null;
|
|
|
|
|
|
|
|
try {
|
2023-08-25 06:03:28 +02:00
|
|
|
const { data } = await api.assetApi.getDownloadInfo({ downloadInfoDto: options, key: api.getKey() });
|
2023-07-01 06:50:47 +02:00
|
|
|
downloadInfo = data;
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Unable to download files');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: prompt for big download
|
|
|
|
// const total = downloadInfo.totalSize;
|
|
|
|
|
|
|
|
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-07-15 03:25:13 +02:00
|
|
|
let downloadKey = `${archiveName} `;
|
2023-07-01 06:50:47 +02:00
|
|
|
if (downloadInfo.archives.length > 1) {
|
|
|
|
downloadKey = `${archiveName} (${i + 1}/${downloadInfo.archives.length})`;
|
|
|
|
}
|
|
|
|
|
2023-07-15 03:25:13 +02:00
|
|
|
const abort = new AbortController();
|
|
|
|
downloadManager.add(downloadKey, archive.size, abort);
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const { data } = await api.assetApi.downloadArchive(
|
2023-08-25 06:03:28 +02:00
|
|
|
{ assetIdsDto: { assetIds: archive.assetIds }, key: api.getKey() },
|
2023-07-01 06:50:47 +02:00
|
|
|
{
|
|
|
|
responseType: 'blob',
|
2023-07-15 03:25:13 +02:00
|
|
|
signal: abort.signal,
|
|
|
|
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded),
|
2023-07-01 06:50:47 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
downloadBlob(data, archiveName);
|
|
|
|
} catch (e) {
|
|
|
|
handleError(e, 'Unable to download files');
|
2023-07-15 03:25:13 +02:00
|
|
|
downloadManager.clear(downloadKey);
|
2023-07-01 06:50:47 +02:00
|
|
|
return;
|
|
|
|
} finally {
|
2023-07-15 03:25:13 +02:00
|
|
|
setTimeout(() => downloadManager.clear(downloadKey), 5_000);
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-30 18:24:28 +02:00
|
|
|
};
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2023-08-25 06:03:28 +02:00
|
|
|
export const downloadFile = async (asset: AssetResponseDto) => {
|
2023-07-15 03:25:13 +02:00
|
|
|
const assets = [
|
|
|
|
{
|
|
|
|
filename: `${asset.originalFileName}.${getFilenameExtension(asset.originalPath)}`,
|
|
|
|
id: asset.id,
|
|
|
|
size: asset.exifInfo?.fileSizeInByte || 0,
|
|
|
|
},
|
|
|
|
];
|
2023-07-01 06:50:47 +02:00
|
|
|
if (asset.livePhotoVideoId) {
|
2023-07-13 16:00:46 +02:00
|
|
|
assets.push({
|
|
|
|
filename: `${asset.originalFileName}.mov`,
|
|
|
|
id: asset.livePhotoVideoId,
|
2023-07-15 03:25:13 +02:00
|
|
|
size: 0,
|
2023-07-13 16:00:46 +02:00
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-15 03:25:13 +02:00
|
|
|
for (const { filename, id, size } of assets) {
|
|
|
|
const downloadKey = filename;
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
try {
|
2023-07-15 03:25:13 +02:00
|
|
|
const abort = new AbortController();
|
|
|
|
downloadManager.add(downloadKey, size, abort);
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
const { data } = await api.assetApi.downloadFile(
|
2023-08-25 06:03:28 +02:00
|
|
|
{ id, key: api.getKey() },
|
2023-07-01 06:50:47 +02:00
|
|
|
{
|
|
|
|
responseType: 'blob',
|
|
|
|
onDownloadProgress: (event: ProgressEvent) => {
|
|
|
|
if (event.lengthComputable) {
|
2023-07-15 03:25:13 +02:00
|
|
|
downloadManager.update(downloadKey, event.loaded, event.total);
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
},
|
2023-07-15 03:25:13 +02:00
|
|
|
signal: abort.signal,
|
2023-07-01 06:50:47 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-07-17 05:16:14 +02:00
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: `Downloading asset ${asset.originalFileName}`,
|
|
|
|
});
|
|
|
|
|
2023-07-15 03:25:13 +02:00
|
|
|
downloadBlob(data, filename);
|
2023-07-01 06:50:47 +02:00
|
|
|
} catch (e) {
|
2023-07-15 03:25:13 +02:00
|
|
|
handleError(e, `Error downloading ${filename}`);
|
|
|
|
downloadManager.clear(downloadKey);
|
2023-07-01 06:50:47 +02:00
|
|
|
} finally {
|
2023-07-15 03:25:13 +02:00
|
|
|
setTimeout(() => downloadManager.clear(downloadKey), 5_000);
|
2023-07-01 06:50:47 +02: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-07-01 06:50:47 +02:00
|
|
|
const lastIndex = Math.max(0, filename.lastIndexOf('.'));
|
|
|
|
const startIndex = (lastIndex || Infinity) + 1;
|
|
|
|
return filename.slice(startIndex).toLowerCase();
|
2023-05-28 03:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the filename of an asset including file extension
|
|
|
|
*/
|
|
|
|
export function getAssetFilename(asset: AssetResponseDto): string {
|
2023-07-01 06:50:47 +02:00
|
|
|
const fileExtension = getFilenameExtension(asset.originalPath);
|
|
|
|
return `${asset.originalFileName}.${fileExtension}`;
|
2023-02-09 17:08:19 +01:00
|
|
|
}
|
|
|
|
|
2023-06-29 15:11:00 +02:00
|
|
|
function isRotated90CW(orientation: number) {
|
2023-07-01 06:50:47 +02:00
|
|
|
return orientation == 6 || orientation == 90;
|
2023-06-29 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isRotated270CW(orientation: number) {
|
2023-07-01 06:50:47 +02:00
|
|
|
return orientation == 8 || orientation == -90;
|
2023-06-29 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 17:22:45 +02:00
|
|
|
/**
|
|
|
|
* Returns aspect ratio for the asset
|
|
|
|
*/
|
|
|
|
export function getAssetRatio(asset: AssetResponseDto) {
|
2023-07-01 06:50:47 +02:00
|
|
|
let height = asset.exifInfo?.exifImageHeight || 235;
|
|
|
|
let width = asset.exifInfo?.exifImageWidth || 235;
|
|
|
|
const orientation = Number(asset.exifInfo?.orientation);
|
|
|
|
if (orientation) {
|
|
|
|
if (isRotated90CW(orientation) || isRotated270CW(orientation)) {
|
|
|
|
[width, height] = [height, width];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { width, height };
|
2023-06-08 17:22:45 +02:00
|
|
|
}
|
2023-09-12 17:42:41 +02:00
|
|
|
|
|
|
|
// list of supported image extensions from https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types excluding svg
|
|
|
|
const supportedImageExtensions = new Set(['apng', 'avif', 'gif', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', 'webp']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the asset is an image supported by web browsers, false otherwise
|
|
|
|
*/
|
|
|
|
export function isWebCompatibleImage(asset: AssetResponseDto): boolean {
|
|
|
|
const imgExtension = getFilenameExtension(asset.originalPath);
|
|
|
|
|
|
|
|
return supportedImageExtensions.has(imgExtension);
|
|
|
|
}
|