2024-03-27 20:47:42 +01:00
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
import { NotificationType, notificationController } from '$lib/components/shared-components/notification/notification';
|
|
|
|
import { AppRoute } from '$lib/constants';
|
2024-03-21 13:14:13 +01:00
|
|
|
import type { AssetInteractionStore } from '$lib/stores/asset-interaction.store';
|
2024-04-17 13:55:07 +02:00
|
|
|
import { assetViewingStore } from '$lib/stores/asset-viewing.store';
|
2024-03-21 13:14:13 +01:00
|
|
|
import { BucketPosition, isSelectingAllAssets, type AssetStore } from '$lib/stores/assets.store';
|
2023-07-15 03:25:13 +02:00
|
|
|
import { downloadManager } from '$lib/stores/download';
|
2024-05-22 15:33:37 +02:00
|
|
|
import { downloadRequest, getKey, s } from '$lib/utils';
|
2024-04-05 21:19:26 +02:00
|
|
|
import { createAlbum } from '$lib/utils/album-utils';
|
2024-05-23 19:57:25 +02:00
|
|
|
import { asByteUnitString } from '$lib/utils/byte-units';
|
2024-03-27 20:47:42 +01:00
|
|
|
import { encodeHTMLSpecialChars } from '$lib/utils/string-utils';
|
2023-12-01 21:58:24 +01:00
|
|
|
import {
|
2024-02-13 23:07:37 +01:00
|
|
|
addAssetsToAlbum as addAssets,
|
2024-05-31 19:44:04 +02:00
|
|
|
getAssetInfo,
|
2024-05-17 22:48:29 +02:00
|
|
|
getBaseUrl,
|
2024-02-14 14:09:49 +01:00
|
|
|
getDownloadInfo,
|
2024-06-07 01:23:49 +02:00
|
|
|
updateAsset,
|
2024-04-02 17:04:52 +02:00
|
|
|
updateAssets,
|
2024-04-05 21:19:26 +02:00
|
|
|
type AlbumResponseDto,
|
2024-01-20 19:47:41 +01:00
|
|
|
type AssetResponseDto,
|
2024-02-13 23:07:37 +01:00
|
|
|
type AssetTypeEnum,
|
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-06-07 01:23:49 +02:00
|
|
|
import { t as translate } from 'svelte-i18n';
|
2024-03-21 13:14:13 +01:00
|
|
|
import { get } from 'svelte/store';
|
2024-02-13 23:07:37 +01:00
|
|
|
import { handleError } from './handle-error';
|
2022-12-30 03:07:18 +01:00
|
|
|
|
2024-03-27 20:47:42 +01:00
|
|
|
export const addAssetsToAlbum = async (albumId: string, assetIds: string[]) => {
|
|
|
|
const result = await addAssets({
|
2024-02-13 23:07:37 +01:00
|
|
|
id: albumId,
|
2024-03-27 20:47:42 +01:00
|
|
|
bulkIdsDto: {
|
|
|
|
ids: assetIds,
|
|
|
|
},
|
2024-02-14 14:09:49 +01:00
|
|
|
key: getKey(),
|
2024-03-27 20:47:42 +01:00
|
|
|
});
|
|
|
|
const count = result.filter(({ success }) => success).length;
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
timeout: 5000,
|
|
|
|
message:
|
|
|
|
count > 0
|
2024-05-22 15:33:37 +02:00
|
|
|
? `Added ${count} asset${s(count)} to the album`
|
2024-03-27 20:47:42 +01:00
|
|
|
: `Asset${assetIds.length === 1 ? ' was' : 's were'} already part of the album`,
|
|
|
|
button: {
|
|
|
|
text: 'View Album',
|
|
|
|
onClick() {
|
|
|
|
return goto(`${AppRoute.ALBUMS}/${albumId}`);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const addAssetsToNewAlbum = async (albumName: string, assetIds: string[]) => {
|
2024-04-05 21:19:26 +02:00
|
|
|
const album = await createAlbum(albumName, assetIds);
|
|
|
|
if (!album) {
|
|
|
|
return;
|
2024-03-27 20:47:42 +01:00
|
|
|
}
|
2024-04-05 21:19:26 +02:00
|
|
|
const displayName = albumName ? `<b>${encodeHTMLSpecialChars(albumName)}</b>` : 'new album';
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
timeout: 5000,
|
2024-05-22 15:33:37 +02:00
|
|
|
message: `Added ${assetIds.length} asset${s(assetIds.length)} to ${displayName}`,
|
2024-04-05 21:19:26 +02:00
|
|
|
html: true,
|
|
|
|
button: {
|
|
|
|
text: 'View Album',
|
|
|
|
onClick() {
|
|
|
|
return goto(`${AppRoute.ALBUMS}/${album.id}`);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return album;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const downloadAlbum = async (album: AlbumResponseDto) => {
|
|
|
|
await downloadArchive(`${album.albumName}.zip`, {
|
|
|
|
albumId: album.id,
|
|
|
|
});
|
2024-03-27 20:47:42 +01:00
|
|
|
};
|
2024-02-13 23:07:37 +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
|
|
|
|
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',
|
2024-05-17 22:48:29 +02:00
|
|
|
url: getBaseUrl() + '/download/archive' + (key ? `?key=${key}` : ''),
|
2024-02-29 17:22:39 +01:00
|
|
|
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 = [
|
|
|
|
{
|
2024-03-07 03:34:55 +01:00
|
|
|
filename: asset.originalFileName,
|
2023-07-15 03:25:13 +02:00
|
|
|
id: asset.id,
|
|
|
|
size: asset.exifInfo?.fileSizeInByte || 0,
|
|
|
|
},
|
|
|
|
];
|
2024-05-31 19:44:04 +02:00
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
if (asset.livePhotoVideoId) {
|
2024-05-31 19:44:04 +02:00
|
|
|
const motionAsset = await getAssetInfo({ id: asset.livePhotoVideoId, key: getKey() });
|
2023-07-13 16:00:46 +02:00
|
|
|
assets.push({
|
2024-05-31 19:44:04 +02:00
|
|
|
filename: motionAsset.originalFileName,
|
2023-07-13 16:00:46 +02:00
|
|
|
id: asset.livePhotoVideoId,
|
2024-05-31 19:44:04 +02:00
|
|
|
size: motionAsset.exifInfo?.fileSizeInByte || 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({
|
2024-05-31 19:44:04 +02:00
|
|
|
method: 'GET',
|
|
|
|
url: getBaseUrl() + `/assets/${id}/original` + (key ? `?key=${key}` : ''),
|
2024-02-29 17:22:39 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-13 22:03:36 +02:00
|
|
|
export function isFlipped(orientation?: string | null) {
|
|
|
|
const value = Number(orientation);
|
|
|
|
return value && (isRotated270CW(value) || isRotated90CW(value));
|
|
|
|
}
|
|
|
|
|
2024-05-23 19:57:25 +02:00
|
|
|
export function getFileSize(asset: AssetResponseDto): string {
|
|
|
|
const size = asset.exifInfo?.fileSizeInByte || 0;
|
|
|
|
return size > 0 ? asByteUnitString(size, undefined, 4) : 'Invalid Data';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAssetResolution(asset: AssetResponseDto): string {
|
|
|
|
const { width, height } = getAssetRatio(asset);
|
|
|
|
|
|
|
|
if (width === 235 && height === 235) {
|
|
|
|
return 'Invalid Data';
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${width} x ${height}`;
|
|
|
|
}
|
|
|
|
|
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;
|
2024-05-13 22:03:36 +02:00
|
|
|
if (isFlipped(asset.exifInfo?.orientation)) {
|
2024-02-02 04:18:00 +01:00
|
|
|
[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 {
|
2024-06-11 17:23:48 +02:00
|
|
|
// originalPath is undefined when public shared link has metadata option turned off
|
|
|
|
if (!asset.originalPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:42:41 +02:00
|
|
|
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-05-03 21:34:57 +02:00
|
|
|
const ids = [...assets].filter((a) => user && a.ownerId === user.id).map((a) => a.id);
|
2023-12-08 00:21:51 +01:00
|
|
|
|
2024-05-03 21:34:57 +02:00
|
|
|
const numberOfIssues = [...assets].filter((a) => user && a.ownerId !== user.id).length;
|
2023-12-01 21:58:24 +01:00
|
|
|
if (numberOfIssues > 0) {
|
|
|
|
notificationController.show({
|
2024-05-22 15:33:37 +02:00
|
|
|
message: `Can't change metadata of ${numberOfIssues} asset${s(numberOfIssues)}`,
|
2023-12-01 21:58:24 +01:00
|
|
|
type: NotificationType.Warning,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
};
|
2024-02-18 20:18:40 +01:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
export const stackAssets = async (assets: AssetResponseDto[]) => {
|
|
|
|
if (assets.length < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
const parent = assets[0];
|
|
|
|
const children = assets.slice(1);
|
|
|
|
const ids = children.map(({ id }) => id);
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
try {
|
|
|
|
await updateAssets({
|
|
|
|
assetBulkUpdateDto: {
|
|
|
|
ids,
|
|
|
|
stackParentId: parent.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Failed to stack assets');
|
|
|
|
return false;
|
|
|
|
}
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
let grandChildren: AssetResponseDto[] = [];
|
|
|
|
for (const asset of children) {
|
|
|
|
asset.stackParentId = parent.id;
|
|
|
|
if (asset.stack) {
|
|
|
|
// Add grand-children to new parent
|
|
|
|
grandChildren = grandChildren.concat(asset.stack);
|
2024-04-02 17:04:52 +02:00
|
|
|
// Reset children stack info
|
|
|
|
asset.stackCount = null;
|
|
|
|
asset.stack = [];
|
|
|
|
}
|
2024-04-17 13:55:07 +02:00
|
|
|
}
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
parent.stack ??= [];
|
|
|
|
parent.stack = parent.stack.concat(children, grandChildren);
|
|
|
|
parent.stackCount = parent.stack.length + 1;
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
notificationController.show({
|
|
|
|
message: `Stacked ${parent.stackCount} assets`,
|
|
|
|
type: NotificationType.Info,
|
|
|
|
button: {
|
|
|
|
text: 'View Stack',
|
|
|
|
onClick() {
|
|
|
|
return assetViewingStore.setAssetId(parent.id);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-04-17 13:55:07 +02:00
|
|
|
return ids;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const unstackAssets = async (assets: AssetResponseDto[]) => {
|
|
|
|
const ids = assets.map(({ id }) => id);
|
|
|
|
try {
|
|
|
|
await updateAssets({
|
|
|
|
assetBulkUpdateDto: {
|
|
|
|
ids,
|
|
|
|
removeParent: true,
|
|
|
|
},
|
|
|
|
});
|
2024-04-02 17:04:52 +02:00
|
|
|
} catch (error) {
|
2024-04-17 13:55:07 +02:00
|
|
|
handleError(error, 'Failed to un-stack assets');
|
|
|
|
return;
|
2024-04-02 17:04:52 +02:00
|
|
|
}
|
2024-04-17 13:55:07 +02:00
|
|
|
for (const asset of assets) {
|
|
|
|
asset.stackParentId = null;
|
|
|
|
asset.stackCount = null;
|
|
|
|
asset.stack = [];
|
|
|
|
}
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: `Un-stacked ${assets.length} assets`,
|
|
|
|
});
|
|
|
|
return assets;
|
|
|
|
};
|
2024-04-02 17:04:52 +02:00
|
|
|
|
2024-03-21 13:14:13 +01:00
|
|
|
export const selectAllAssets = async (assetStore: AssetStore, assetInteractionStore: AssetInteractionStore) => {
|
|
|
|
if (get(isSelectingAllAssets)) {
|
|
|
|
// Selection is already ongoing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isSelectingAllAssets.set(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (const bucket of assetStore.buckets) {
|
|
|
|
await assetStore.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
|
|
|
|
|
|
|
|
if (!get(isSelectingAllAssets)) {
|
|
|
|
break; // Cancelled
|
|
|
|
}
|
|
|
|
assetInteractionStore.selectAssets(bucket.assets);
|
|
|
|
|
|
|
|
// We use setTimeout to allow the UI to update. Otherwise, this may
|
|
|
|
// cause a long delay between the start of 'select all' and the
|
|
|
|
// effective update of the UI, depending on the number of assets
|
|
|
|
// to select
|
|
|
|
await delay(0);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Error selecting all assets');
|
|
|
|
isSelectingAllAssets.set(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-07 01:23:49 +02:00
|
|
|
export const toggleArchive = async (asset: AssetResponseDto) => {
|
|
|
|
try {
|
|
|
|
const data = await updateAsset({
|
|
|
|
id: asset.id,
|
|
|
|
updateAssetDto: {
|
|
|
|
isArchived: !asset.isArchived,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
asset.isArchived = data.isArchived;
|
|
|
|
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: asset.isArchived ? `Added to archive` : `Removed from archive`,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, `Unable to ${asset.isArchived ? `remove asset from` : `add asset to`} archive`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return asset;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const archiveAssets = async (assets: AssetResponseDto[], archive: boolean) => {
|
|
|
|
const isArchived = archive;
|
|
|
|
const ids = assets.map(({ id }) => id);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (ids.length > 0) {
|
|
|
|
await updateAssets({ assetBulkUpdateDto: { ids, isArchived } });
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
asset.isArchived = isArchived;
|
|
|
|
}
|
|
|
|
|
|
|
|
const t = get(translate);
|
|
|
|
notificationController.show({
|
|
|
|
message: `${isArchived ? t('archived') : t('unarchived')} ${ids.length}`,
|
|
|
|
type: NotificationType.Info,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, `Unable to ${isArchived ? 'archive' : 'unarchive'}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
};
|
|
|
|
|
2024-02-18 20:18:40 +01:00
|
|
|
export const delay = async (ms: number) => {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
};
|