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';
|
2024-02-29 17:22:39 +01:00
|
|
|
import { downloadRequest, getKey } from '$lib/utils';
|
2023-12-01 21:58:24 +01:00
|
|
|
import {
|
2024-02-13 23:07:37 +01:00
|
|
|
addAssetsToAlbum as addAssets,
|
2024-02-29 17:22:39 +01:00
|
|
|
defaults,
|
2024-02-14 14:09:49 +01:00
|
|
|
getDownloadInfo,
|
2024-01-20 19:47:41 +01:00
|
|
|
type AssetResponseDto,
|
2024-02-13 23:07:37 +01:00
|
|
|
type AssetTypeEnum,
|
|
|
|
type BulkIdResponseDto,
|
2024-01-20 19:47:41 +01:00
|
|
|
type DownloadInfoDto,
|
2024-02-13 23:07:37 +01:00
|
|
|
type DownloadResponseDto,
|
2024-01-20 19:47:41 +01:00
|
|
|
type UserResponseDto,
|
2024-02-13 23:07:37 +01:00
|
|
|
} from '@immich/sdk';
|
2024-01-25 18:14:02 +01:00
|
|
|
import { DateTime } from 'luxon';
|
2024-02-13 23:07:37 +01: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[]> =>
|
2024-02-13 23:07:37 +01:00
|
|
|
addAssets({
|
|
|
|
id: albumId,
|
|
|
|
bulkIdsDto: { ids: assetIds },
|
2024-02-14 14:09:49 +01:00
|
|
|
key: getKey(),
|
2024-02-13 23:07:37 +01:00
|
|
|
}).then((results) => {
|
|
|
|
const count = results.filter(({ success }) => success).length;
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: `Added ${count} asset${count === 1 ? '' : 's'}`,
|
2023-08-25 06:03:28 +02:00
|
|
|
});
|
2023-01-09 21:16:08 +01:00
|
|
|
|
2024-02-13 23:07:37 +01:00
|
|
|
return results;
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
document.body.append(anchor);
|
2023-07-01 06:50:47 +02:00
|
|
|
anchor.click();
|
2024-02-02 04:18:00 +01:00
|
|
|
anchor.remove();
|
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 {
|
2024-02-14 14:09:49 +01:00
|
|
|
downloadInfo = await getDownloadInfo({ downloadInfoDto: options, key: getKey() });
|
2023-07-01 06:50:47 +02:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Unable to download files');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: prompt for big download
|
|
|
|
// const total = downloadInfo.totalSize;
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
for (let index = 0; index < downloadInfo.archives.length; index++) {
|
|
|
|
const archive = downloadInfo.archives[index];
|
2024-03-05 13:56:12 +01:00
|
|
|
const suffix = downloadInfo.archives.length > 1 ? `+${index + 1}` : '';
|
|
|
|
const archiveName = fileName.replace('.zip', `${suffix}-${DateTime.now().toFormat('yyyyLLdd_HHmmss')}.zip`);
|
2024-02-29 17:22:39 +01:00
|
|
|
const key = getKey();
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-07-15 03:25:13 +02:00
|
|
|
let downloadKey = `${archiveName} `;
|
2023-07-01 06:50:47 +02:00
|
|
|
if (downloadInfo.archives.length > 1) {
|
2024-02-02 04:18:00 +01:00
|
|
|
downloadKey = `${archiveName} (${index + 1}/${downloadInfo.archives.length})`;
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-02-29 17:22:39 +01:00
|
|
|
// TODO use sdk once it supports progress events
|
|
|
|
const { data } = await downloadRequest({
|
|
|
|
method: 'POST',
|
|
|
|
url: defaults.baseUrl + '/download/archive' + (key ? `?key=${key}` : ''),
|
|
|
|
data: { assetIds: archive.assetIds },
|
|
|
|
signal: abort.signal,
|
|
|
|
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded),
|
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
downloadBlob(data, archiveName);
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error, '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 {
|
2024-02-02 04:18:00 +01:00
|
|
|
setTimeout(() => downloadManager.clear(downloadKey), 5000);
|
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-09-20 13:16:33 +02:00
|
|
|
if (asset.isOffline) {
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: `Asset ${asset.originalFileName} is offline`,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
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);
|
2024-02-29 17:22:39 +01:00
|
|
|
const key = getKey();
|
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}`,
|
|
|
|
});
|
|
|
|
|
2024-02-29 17:22:39 +01:00
|
|
|
// TODO use sdk once it supports progress events
|
|
|
|
const { data } = await downloadRequest({
|
|
|
|
method: 'POST',
|
|
|
|
url: defaults.baseUrl + `/download/asset/${id}` + (key ? `?key=${key}` : ''),
|
|
|
|
signal: abort.signal,
|
|
|
|
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded, event.total),
|
|
|
|
});
|
|
|
|
|
2023-07-15 03:25:13 +02:00
|
|
|
downloadBlob(data, filename);
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error, `Error downloading ${filename}`);
|
2023-07-15 03:25:13 +02:00
|
|
|
downloadManager.clear(downloadKey);
|
2023-07-01 06:50:47 +02:00
|
|
|
} finally {
|
2024-02-02 04:18:00 +01:00
|
|
|
setTimeout(() => downloadManager.clear(downloadKey), 5000);
|
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('.'));
|
2024-02-02 04:18:00 +01:00
|
|
|
const startIndex = (lastIndex || Number.POSITIVE_INFINITY) + 1;
|
2023-07-01 06:50:47 +02:00
|
|
|
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-09-18 15:06:03 +02:00
|
|
|
return orientation === 5 || orientation === 6 || orientation === 90;
|
2023-06-29 15:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isRotated270CW(orientation: number) {
|
2023-09-18 15:06:03 +02:00
|
|
|
return orientation === 7 || 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);
|
2024-02-02 04:18:00 +01:00
|
|
|
if (orientation && (isRotated90CW(orientation) || isRotated270CW(orientation))) {
|
|
|
|
[width, height] = [height, width];
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2023-11-01 04:13:34 +01:00
|
|
|
|
|
|
|
export const getAssetType = (type: AssetTypeEnum) => {
|
|
|
|
switch (type) {
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'IMAGE': {
|
2023-11-01 04:13:34 +01:00
|
|
|
return 'Photo';
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
case 'VIDEO': {
|
2023-11-01 04:13:34 +01:00
|
|
|
return 'Video';
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
default: {
|
2023-11-01 04:13:34 +01:00
|
|
|
return 'Asset';
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-11-01 04:13:34 +01:00
|
|
|
}
|
|
|
|
};
|
2023-12-01 21:58:24 +01:00
|
|
|
|
|
|
|
export const getSelectedAssets = (assets: Set<AssetResponseDto>, user: UserResponseDto | null): string[] => {
|
2024-02-02 04:18:00 +01:00
|
|
|
const ids = [...assets].filter((a) => !a.isExternal && user && a.ownerId === user.id).map((a) => a.id);
|
2023-12-08 00:21:51 +01:00
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
const numberOfIssues = [...assets].filter((a) => a.isExternal || (user && a.ownerId !== user.id)).length;
|
2023-12-01 21:58:24 +01:00
|
|
|
if (numberOfIssues > 0) {
|
|
|
|
notificationController.show({
|
|
|
|
message: `Can't change metadata of ${numberOfIssues} asset${numberOfIssues > 1 ? 's' : ''}`,
|
|
|
|
type: NotificationType.Warning,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
};
|
2024-02-18 20:18:40 +01:00
|
|
|
|
|
|
|
export const delay = async (ms: number) => {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
};
|