refactoring des caches

This commit is contained in:
2025-10-25 17:14:24 +02:00
parent 180241a315
commit ecb362ae48
21 changed files with 964 additions and 245 deletions

View File

@@ -141,6 +141,7 @@ import {
purgeCache,
consolidateCache,
getImageUrl,
waitForDownload,
} from "../services/coverCache";
// --- États ---
@@ -190,11 +191,16 @@ async function handleAddImage() {
isAdding.value = true; addError.value=""; addSuccess.value="";
try {
const result = await addImage(newImageUrl.value);
addSuccess.value = `Image downloading... PK: ${result.pk}`;
// Attendre que le téléchargement et la transformation soient terminés
await waitForDownload(result.pk);
addSuccess.value = `Image added! PK: ${result.pk}`;
newImageUrl.value = "";
await refreshImages();
} catch(e:any) { addError.value = e.message ?? "Failed to add image"; }
finally { isAdding.value=false; setTimeout(()=>addSuccess.value="",1500); }
finally { isAdding.value=false; setTimeout(()=>addSuccess.value="",3000); }
}
async function handleDeleteImage(pk:string){

View File

@@ -24,6 +24,14 @@ export interface ApiError {
message: string;
}
export interface DownloadStatus {
pk: string;
finished: boolean;
current_size?: number;
expected_size?: number;
transformed_size?: number;
}
/**
* Liste toutes les images en cache
*/
@@ -109,6 +117,54 @@ export async function consolidateCache(): Promise<void> {
}
}
/**
* Récupère le statut du téléchargement d'une image
*/
export async function getDownloadStatus(pk: string): Promise<DownloadStatus> {
const response = await fetch(`/api/covers/${pk}/status`);
if (!response.ok) {
const error: ApiError = await response.json();
throw new Error(error.message || "Failed to get download status");
}
return response.json();
}
/**
* Attend que le téléchargement d'une image soit terminé
*
* @param pk - Clé primaire de l'image
* @param maxWaitMs - Temps maximum d'attente en millisecondes (défaut: 30000)
* @param pollIntervalMs - Intervalle entre les vérifications en millisecondes (défaut: 500)
*/
export async function waitForDownload(
pk: string,
maxWaitMs: number = 30000,
pollIntervalMs: number = 500
): Promise<void> {
const startTime = Date.now();
while (Date.now() - startTime < maxWaitMs) {
try {
const status = await getDownloadStatus(pk);
if (status.finished) {
return; // Téléchargement terminé
}
} catch (error) {
// Si l'API retourne une erreur, on continue d'attendre
console.warn(`Error checking download status for ${pk}:`, error);
}
// Attendre avant la prochaine vérification
await new Promise(resolve => setTimeout(resolve, pollIntervalMs));
}
// Timeout atteint, on lance une dernière vérification
const finalStatus = await getDownloadStatus(pk);
if (!finalStatus.finished) {
console.warn(`Download timeout for ${pk}, but continuing anyway`);
}
}
/**
* Génère l'URL pour afficher une image
*/