mirror of
https://github.com/immich-app/immich.git
synced 2025-01-10 13:56:47 +01:00
ce5966c23d
* feat: activity * regenerate api * fix: make asset owner unable to delete comment * fix: merge * fix: tests * feat: use textarea instead of input * fix: do actions only if the album is shared * fix: placeholder opacity * fix(web): improve messages UI * fix(web): improve input message UI * pr feedback * fix: tests * pr feedback * pr feedback * pr feedback * fix permissions * regenerate api * pr feedback * pr feedback * multiple improvements on web * fix: ui colors * WIP * chore: open api * pr feedback * fix: add comment * chore: clean up * pr feedback * refactor: endpoints * chore: open api * fix: filter by type * fix: e2e * feat: e2e remove own comment * fix: web tests * remove console.log * chore: cleanup * fix: ui tweaks * pr feedback * fix web test * fix: unit tests * chore: remove unused code * revert useless changes * fix: grouping messages * fix: remove nullable on updatedAt * fix: text overflow * styling --------- Co-authored-by: Jason Rasmussen <jrasm91@gmail.com> Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
205 lines
6.2 KiB
TypeScript
205 lines
6.2 KiB
TypeScript
import { notificationController, NotificationType } from '$lib/components/shared-components/notification/notification';
|
|
import { downloadManager } from '$lib/stores/download';
|
|
import { api, BulkIdResponseDto, AssetResponseDto, DownloadResponseDto, DownloadInfoDto, AssetTypeEnum } from '@api';
|
|
import { handleError } from './handle-error';
|
|
|
|
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'}`,
|
|
});
|
|
|
|
return results;
|
|
});
|
|
|
|
export 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 (fileName: string, options: DownloadInfoDto) => {
|
|
let downloadInfo: DownloadResponseDto | null = null;
|
|
|
|
try {
|
|
const { data } = await api.assetApi.getDownloadInfo({ downloadInfoDto: options, key: api.getKey() });
|
|
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`);
|
|
|
|
let downloadKey = `${archiveName} `;
|
|
if (downloadInfo.archives.length > 1) {
|
|
downloadKey = `${archiveName} (${i + 1}/${downloadInfo.archives.length})`;
|
|
}
|
|
|
|
const abort = new AbortController();
|
|
downloadManager.add(downloadKey, archive.size, abort);
|
|
|
|
try {
|
|
const { data } = await api.assetApi.downloadArchive(
|
|
{ assetIdsDto: { assetIds: archive.assetIds }, key: api.getKey() },
|
|
{
|
|
responseType: 'blob',
|
|
signal: abort.signal,
|
|
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded),
|
|
},
|
|
);
|
|
|
|
downloadBlob(data, archiveName);
|
|
} catch (e) {
|
|
handleError(e, 'Unable to download files');
|
|
downloadManager.clear(downloadKey);
|
|
return;
|
|
} finally {
|
|
setTimeout(() => downloadManager.clear(downloadKey), 5_000);
|
|
}
|
|
}
|
|
};
|
|
|
|
export const downloadFile = async (asset: AssetResponseDto) => {
|
|
if (asset.isOffline) {
|
|
notificationController.show({
|
|
type: NotificationType.Info,
|
|
message: `Asset ${asset.originalFileName} is offline`,
|
|
});
|
|
return;
|
|
}
|
|
const assets = [
|
|
{
|
|
filename: `${asset.originalFileName}.${getFilenameExtension(asset.originalPath)}`,
|
|
id: asset.id,
|
|
size: asset.exifInfo?.fileSizeInByte || 0,
|
|
},
|
|
];
|
|
if (asset.livePhotoVideoId) {
|
|
assets.push({
|
|
filename: `${asset.originalFileName}.mov`,
|
|
id: asset.livePhotoVideoId,
|
|
size: 0,
|
|
});
|
|
}
|
|
|
|
for (const { filename, id, size } of assets) {
|
|
const downloadKey = filename;
|
|
|
|
try {
|
|
const abort = new AbortController();
|
|
downloadManager.add(downloadKey, size, abort);
|
|
|
|
const { data } = await api.assetApi.downloadFile(
|
|
{ id, key: api.getKey() },
|
|
{
|
|
responseType: 'blob',
|
|
onDownloadProgress: (event: ProgressEvent) => {
|
|
if (event.lengthComputable) {
|
|
downloadManager.update(downloadKey, event.loaded, event.total);
|
|
}
|
|
},
|
|
signal: abort.signal,
|
|
},
|
|
);
|
|
|
|
notificationController.show({
|
|
type: NotificationType.Info,
|
|
message: `Downloading asset ${asset.originalFileName}`,
|
|
});
|
|
|
|
downloadBlob(data, filename);
|
|
} catch (e) {
|
|
handleError(e, `Error downloading ${filename}`);
|
|
downloadManager.clear(downloadKey);
|
|
} finally {
|
|
setTimeout(() => downloadManager.clear(downloadKey), 5_000);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Returns the lowercase filename extension without a dot (.) and
|
|
* an empty string when not found.
|
|
*/
|
|
export function getFilenameExtension(filename: string): string {
|
|
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}`;
|
|
}
|
|
|
|
function isRotated90CW(orientation: number) {
|
|
return orientation === 5 || orientation === 6 || orientation === 90;
|
|
}
|
|
|
|
function isRotated270CW(orientation: number) {
|
|
return orientation === 7 || orientation === 8 || orientation === -90;
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
if (isRotated90CW(orientation) || isRotated270CW(orientation)) {
|
|
[width, height] = [height, width];
|
|
}
|
|
}
|
|
return { width, height };
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
export const getAssetType = (type: AssetTypeEnum) => {
|
|
switch (type) {
|
|
case 'IMAGE':
|
|
return 'Photo';
|
|
case 'VIDEO':
|
|
return 'Video';
|
|
default:
|
|
return 'Asset';
|
|
}
|
|
};
|