mirror of
https://github.com/immich-app/immich.git
synced 2026-03-26 11:50:53 +03:00
fix(web): stop in-progress uploads on logout (#27021)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { eventManager } from '$lib/managers/event-manager.svelte';
|
import { eventManager } from '$lib/managers/event-manager.svelte';
|
||||||
import { uploadAssetsStore } from '$lib/stores/upload';
|
import { uploadAssetsStore } from '$lib/stores/upload';
|
||||||
|
import { cancelUploadRequests } from '$lib/utils';
|
||||||
import { getSupportedMediaTypes, type ServerMediaTypesResponseDto } from '@immich/sdk';
|
import { getSupportedMediaTypes, type ServerMediaTypesResponseDto } from '@immich/sdk';
|
||||||
|
|
||||||
class UploadManager {
|
class UploadManager {
|
||||||
@@ -13,6 +14,7 @@ class UploadManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
|
cancelUploadRequests();
|
||||||
uploadAssetsStore.reset();
|
uploadAssetsStore.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,17 +78,40 @@ export const sleep = (ms: number) => {
|
|||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let unsubscribeId = 0;
|
||||||
|
const uploads: Record<number, () => void> = {};
|
||||||
|
|
||||||
|
const trackUpload = (unsubscribe: () => void) => {
|
||||||
|
const id = unsubscribeId++;
|
||||||
|
uploads[id] = unsubscribe;
|
||||||
|
return () => {
|
||||||
|
delete uploads[id];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const cancelUploadRequests = () => {
|
||||||
|
for (const unsubscribe of Object.values(uploads)) {
|
||||||
|
unsubscribe();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const uploadRequest = async <T>(options: UploadRequestOptions): Promise<{ data: T; status: number }> => {
|
export const uploadRequest = async <T>(options: UploadRequestOptions): Promise<{ data: T; status: number }> => {
|
||||||
const { onUploadProgress: onProgress, data, url } = options;
|
const { onUploadProgress: onProgress, data, url } = options;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
const unsubscribe = trackUpload(() => xhr.abort());
|
||||||
|
|
||||||
|
xhr.addEventListener('error', (error) => {
|
||||||
|
unsubscribe();
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
xhr.addEventListener('error', (error) => reject(error));
|
|
||||||
xhr.addEventListener('load', () => {
|
xhr.addEventListener('load', () => {
|
||||||
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) {
|
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) {
|
||||||
|
unsubscribe();
|
||||||
resolve({ data: xhr.response as T, status: xhr.status });
|
resolve({ data: xhr.response as T, status: xhr.status });
|
||||||
} else {
|
} else {
|
||||||
|
unsubscribe();
|
||||||
reject(new ApiError(xhr.statusText, xhr.status, xhr.response));
|
reject(new ApiError(xhr.statusText, xhr.status, xhr.response));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user