2023-05-14 04:52:29 +02:00
|
|
|
import { api, AddAssetsResponseDto, AssetResponseDto } from '@api';
|
2022-12-30 03:07:18 +01:00
|
|
|
import {
|
|
|
|
notificationController,
|
|
|
|
NotificationType
|
|
|
|
} from '$lib/components/shared-components/notification/notification';
|
2023-01-09 21:16:08 +01:00
|
|
|
import { downloadAssets } from '$lib/stores/download';
|
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
|
|
|
|
|
|
|
export async function bulkDownload(
|
|
|
|
fileName: string,
|
|
|
|
assets: AssetResponseDto[],
|
2023-05-16 16:13:20 +02:00
|
|
|
onDone?: () => void,
|
2023-01-09 21:16:08 +01:00
|
|
|
key?: string
|
|
|
|
) {
|
|
|
|
const assetIds = assets.map((asset) => asset.id);
|
|
|
|
|
|
|
|
try {
|
2023-01-09 22:32:58 +01:00
|
|
|
// let skip = 0;
|
2023-01-09 21:16:08 +01:00
|
|
|
let count = 0;
|
|
|
|
let done = false;
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
count++;
|
|
|
|
|
|
|
|
const downloadFileName = fileName + `${count === 1 ? '' : count}.zip`;
|
|
|
|
downloadAssets.set({ [downloadFileName]: 0 });
|
|
|
|
|
|
|
|
let total = 0;
|
|
|
|
|
2023-05-28 03:52:22 +02:00
|
|
|
const { data, status, headers } = await api.assetApi.downloadFiles(
|
|
|
|
{ downloadFilesDto: { assetIds }, key },
|
|
|
|
{
|
|
|
|
responseType: 'blob',
|
|
|
|
onDownloadProgress: function (progressEvent) {
|
|
|
|
const request = this as XMLHttpRequest;
|
|
|
|
if (!total) {
|
|
|
|
total = Number(request.getResponseHeader('X-Immich-Content-Length-Hint')) || 0;
|
|
|
|
}
|
2023-02-24 17:01:10 +01:00
|
|
|
|
2023-05-28 03:52:22 +02:00
|
|
|
if (total) {
|
|
|
|
const current = progressEvent.loaded;
|
|
|
|
downloadAssets.set({ [downloadFileName]: Math.floor((current / total) * 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
|
|
|
|
|
|
|
const isNotComplete = headers['x-immich-archive-complete'] === 'false';
|
|
|
|
const fileCount = Number(headers['x-immich-archive-file-count']) || 0;
|
|
|
|
if (isNotComplete && fileCount > 0) {
|
2023-01-09 22:32:58 +01:00
|
|
|
// skip += fileCount;
|
2023-01-09 21:16:08 +01:00
|
|
|
} else {
|
2023-05-16 16:13:20 +02:00
|
|
|
onDone?.();
|
2023-01-09 21:16:08 +01:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(data instanceof Blob)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status === 201) {
|
|
|
|
const fileUrl = URL.createObjectURL(data);
|
|
|
|
const anchor = document.createElement('a');
|
|
|
|
anchor.href = fileUrl;
|
|
|
|
anchor.download = downloadFileName;
|
|
|
|
|
|
|
|
document.body.appendChild(anchor);
|
|
|
|
anchor.click();
|
|
|
|
document.body.removeChild(anchor);
|
|
|
|
|
|
|
|
URL.revokeObjectURL(fileUrl);
|
|
|
|
|
|
|
|
// Remove item from download list
|
|
|
|
setTimeout(() => {
|
|
|
|
downloadAssets.set({});
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error downloading file ', e);
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Error,
|
|
|
|
message: 'Error downloading file, check console for more details.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
const lastIndex = filename.lastIndexOf('.');
|
|
|
|
return filename.slice(lastIndex + 1).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the MIME type of the file and an empty string when not found.
|
|
|
|
*/
|
|
|
|
export function getFileMimeType(file: File): string {
|
|
|
|
if (file.type !== '') {
|
|
|
|
// Return the MIME type determined by the browser.
|
|
|
|
return file.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return MIME type based on the file extension.
|
|
|
|
switch (getFilenameExtension(file.name)) {
|
|
|
|
case 'heic':
|
|
|
|
return 'image/heic';
|
|
|
|
case 'heif':
|
|
|
|
return 'image/heif';
|
|
|
|
case 'dng':
|
|
|
|
return 'image/dng';
|
|
|
|
case '3gp':
|
|
|
|
return 'video/3gpp';
|
|
|
|
case 'nef':
|
2023-02-14 20:54:28 +01:00
|
|
|
return 'image/x-nikon-nef';
|
|
|
|
case 'raf':
|
|
|
|
return 'image/x-fuji-raf';
|
|
|
|
case 'srw':
|
|
|
|
return 'image/x-samsung-srw';
|
2023-02-09 17:08:19 +01:00
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|