refactor(web): drop axios (#7490)

* 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
37 changed files with 217 additions and 20671 deletions

View File

@@ -1,10 +1,9 @@
import { api } from '$lib/api';
import { UploadState } from '$lib/models/upload-asset';
import { uploadAssetsStore } from '$lib/stores/upload';
import { getKey } from '$lib/utils';
import { getKey, uploadRequest } from '$lib/utils';
import { addAssetsToAlbum } from '$lib/utils/asset-utils';
import { ExecutorQueue } from '$lib/utils/executor-queue';
import { getSupportedMediaTypes, type AssetFileUploadResponseDto } from '@immich/sdk';
import { defaults, getSupportedMediaTypes, type AssetFileUploadResponseDto } from '@immich/sdk';
import { getServerErrorMessage, handleError } from './handle-error';
let _extensions: string[];
@@ -72,26 +71,28 @@ async function fileUploader(asset: File, albumId: string | undefined = undefined
const deviceAssetId = getDeviceAssetId(asset);
return new Promise((resolve) => resolve(uploadAssetsStore.markStarted(deviceAssetId)))
.then(() =>
api.assetApi.uploadFile(
{
deviceAssetId,
deviceId: 'WEB',
fileCreatedAt,
fileModifiedAt: new Date(asset.lastModified).toISOString(),
isFavorite: false,
duration: '0:00:00.000000',
assetData: new File([asset], asset.name),
key: getKey(),
},
{
onUploadProgress: ({ event }) => {
const { loaded, total } = event;
uploadAssetsStore.updateProgress(deviceAssetId, loaded, total);
},
},
),
)
.then(() => {
const formData = new FormData();
for (const [key, value] of Object.entries({
deviceAssetId,
deviceId: 'WEB',
fileCreatedAt,
fileModifiedAt: new Date(asset.lastModified).toISOString(),
isFavorite: 'false',
duration: '0:00:00.000000',
assetData: new File([asset], asset.name),
})) {
formData.append(key, value);
}
const key = getKey();
return uploadRequest<AssetFileUploadResponseDto>({
url: defaults.baseUrl + '/asset/upload' + (key ? `?key=${key}` : ''),
data: formData,
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
});
})
.then(async (response) => {
if (response.status == 200 || response.status == 201) {
const res: AssetFileUploadResponseDto = response.data;
@@ -118,9 +119,9 @@ async function fileUploader(asset: File, albumId: string | undefined = undefined
return res.id;
}
})
.catch(async (error) => {
.catch((error) => {
handleError(error, 'Unable to upload file');
const reason = (await getServerErrorMessage(error)) || error;
const reason = getServerErrorMessage(error) || error;
uploadAssetsStore.updateAsset(deviceAssetId, { state: UploadState.ERROR, error: reason });
return undefined;
});