refactor(web): drop axios ()

* refactor: downloadApi

* refactor: assetApi

* chore: drop axios

* chore: tidy up

* chore: fix exports

* fix: show notification when download starts
This commit is contained in:
Jason Rasmussen 2024-02-29 11:22:39 -05:00 committed by GitHub
parent bb3d81bfc5
commit 09a7291527
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 217 additions and 20671 deletions
web/src/lib/utils

View file

@ -1,8 +1,9 @@
import { api } from '$lib/api';
import { notificationController, NotificationType } from '$lib/components/shared-components/notification/notification';
import { downloadManager } from '$lib/stores/download';
import { downloadRequest, getKey } from '$lib/utils';
import {
addAssetsToAlbum as addAssets,
defaults,
getDownloadInfo,
type AssetResponseDto,
type AssetTypeEnum,
@ -12,7 +13,6 @@ import {
type UserResponseDto,
} from '@immich/sdk';
import { DateTime } from 'luxon';
import { getKey } from '../utils';
import { handleError } from './handle-error';
export const addAssetsToAlbum = async (albumId: string, assetIds: Array<string>): Promise<BulkIdResponseDto[]> =>
@ -61,6 +61,7 @@ export const downloadArchive = async (fileName: string, options: DownloadInfoDto
const archive = downloadInfo.archives[index];
const suffix = downloadInfo.archives.length === 1 ? '' : `+${index + 1}`;
const archiveName = fileName.replace('.zip', `${suffix}-${DateTime.now().toFormat('yyyy-LL-dd-HH-mm-ss')}.zip`);
const key = getKey();
let downloadKey = `${archiveName} `;
if (downloadInfo.archives.length > 1) {
@ -71,14 +72,14 @@ export const downloadArchive = async (fileName: string, options: DownloadInfoDto
downloadManager.add(downloadKey, archive.size, abort);
try {
const { data } = await api.downloadApi.downloadArchive(
{ assetIdsDto: { assetIds: archive.assetIds }, key: getKey() },
{
responseType: 'blob',
signal: abort.signal,
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded),
},
);
// 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),
});
downloadBlob(data, archiveName);
} catch (error) {
@ -120,25 +121,21 @@ export const downloadFile = async (asset: AssetResponseDto) => {
try {
const abort = new AbortController();
downloadManager.add(downloadKey, size, abort);
const { data } = await api.downloadApi.downloadFile(
{ id, key: getKey() },
{
responseType: 'blob',
onDownloadProgress: ({ event }) => {
if (event.lengthComputable) {
downloadManager.update(downloadKey, event.loaded, event.total);
}
},
signal: abort.signal,
},
);
const key = getKey();
notificationController.show({
type: NotificationType.Info,
message: `Downloading asset ${asset.originalFileName}`,
});
// 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),
});
downloadBlob(data, filename);
} catch (error) {
handleError(error, `Error downloading ${filename}`);