Files
EventSnap/frontend/src/lib/upload-queue.ts
MechaCat02 6efd2fd3b5 feat: auto-retry uploads when rate limited
Backend: extend rate limiter with check_with_retry() that returns the
seconds until the next slot opens. Upload 429 responses now include
retry_after_secs in the JSON body and a Retry-After header.

Frontend: the upload queue catches 429s as RateLimitError, resets the
affected item to pending, schedules processQueue() to resume after the
server-reported wait, and shows a live countdown banner in UploadQueue.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-03 18:34:01 +02:00

273 lines
6.8 KiB
TypeScript

import { openDB, type IDBPDatabase } from 'idb';
import { writable, get } from 'svelte/store';
import { getToken } from './auth';
export interface QueueItem {
id: string;
fileName: string;
fileSize: number;
mimeType: string;
caption: string;
hashtags: string;
status: 'pending' | 'uploading' | 'done' | 'error';
progress: number;
error?: string;
}
// Store does NOT hold file blobs — those stay in IndexedDB only
export const queueItems = writable<QueueItem[]>([]);
export const isProcessing = writable(false);
/** Set to the timestamp (ms) at which the rate-limit lifts, or null when clear. */
export const rateLimitRetryAt = writable<number | null>(null);
const DB_NAME = 'eventsnap-uploads';
const STORE_NAME = 'queue';
let db: IDBPDatabase | null = null;
async function getDb(): Promise<IDBPDatabase> {
if (db) return db;
db = await openDB(DB_NAME, 1, {
upgrade(database) {
if (!database.objectStoreNames.contains(STORE_NAME)) {
database.createObjectStore(STORE_NAME, { keyPath: 'id' });
}
}
});
return db;
}
class RateLimitError extends Error {
retryAfterSecs: number;
constructor(secs: number) {
super('rate_limited');
this.retryAfterSecs = secs;
}
}
export async function loadQueue(): Promise<void> {
const database = await getDb();
const all = await database.getAll(STORE_NAME);
const items: QueueItem[] = all.map((entry) => ({
id: entry.id,
fileName: entry.fileName,
fileSize: entry.fileSize,
mimeType: entry.mimeType,
caption: entry.caption ?? '',
hashtags: entry.hashtags ?? '',
status: entry.status === 'uploading' ? 'pending' : entry.status,
progress: entry.status === 'done' ? 100 : 0,
error: entry.error
}));
queueItems.set(items);
}
export async function addToQueue(
file: File,
caption: string,
hashtags: string
): Promise<void> {
const database = await getDb();
const id = crypto.randomUUID();
const entry = {
id,
fileName: file.name,
fileSize: file.size,
mimeType: file.type,
caption,
hashtags,
status: 'pending',
blob: file
};
await database.put(STORE_NAME, entry);
queueItems.update((items) => [
...items,
{
id,
fileName: file.name,
fileSize: file.size,
mimeType: file.type,
caption,
hashtags,
status: 'pending',
progress: 0
}
]);
processQueue();
}
export async function retryItem(id: string): Promise<void> {
const database = await getDb();
const entry = await database.get(STORE_NAME, id);
if (!entry) return;
entry.status = 'pending';
entry.error = undefined;
await database.put(STORE_NAME, entry);
queueItems.update((items) =>
items.map((item) =>
item.id === id ? { ...item, status: 'pending' as const, progress: 0, error: undefined } : item
)
);
processQueue();
}
export async function removeItem(id: string): Promise<void> {
const database = await getDb();
await database.delete(STORE_NAME, id);
queueItems.update((items) => items.filter((item) => item.id !== id));
}
export async function clearCompleted(): Promise<void> {
const database = await getDb();
const items = get(queueItems);
for (const item of items) {
if (item.status === 'done') {
await database.delete(STORE_NAME, item.id);
}
}
queueItems.update((items) => items.filter((item) => item.status !== 'done'));
}
let processing = false;
async function processQueue(): Promise<void> {
if (processing) return;
processing = true;
isProcessing.set(true);
try {
while (true) {
const items = get(queueItems);
const next = items.find((item) => item.status === 'pending');
if (!next) break;
try {
await uploadItem(next.id);
} catch (e) {
if (e instanceof RateLimitError) {
// Keep all pending items as-is; schedule queue resume when limit lifts
const retryAt = Date.now() + e.retryAfterSecs * 1000;
rateLimitRetryAt.set(retryAt);
setTimeout(() => {
rateLimitRetryAt.set(null);
processQueue();
}, e.retryAfterSecs * 1000);
break;
}
// Other errors are already handled inside uploadItem (marked as 'error')
}
}
} finally {
processing = false;
isProcessing.set(false);
}
}
async function uploadItem(id: string): Promise<void> {
const database = await getDb();
const entry = await database.get(STORE_NAME, id);
if (!entry || !entry.blob) {
updateItemStatus(id, 'error', 'Datei nicht gefunden.');
return;
}
updateItemStatus(id, 'uploading');
const token = getToken();
if (!token) {
updateItemStatus(id, 'error', 'Nicht angemeldet.');
return;
}
try {
const formData = new FormData();
formData.append('file', entry.blob, entry.fileName);
if (entry.caption) formData.append('caption', entry.caption);
if (entry.hashtags) formData.append('hashtags', entry.hashtags);
await new Promise<void>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', '/api/v1/upload');
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
const pct = Math.round((e.loaded / e.total) * 100);
queueItems.update((items) =>
items.map((item) => (item.id === id ? { ...item, progress: pct } : item))
);
}
});
xhr.addEventListener('load', () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve();
} else if (xhr.status === 429) {
try {
const body = JSON.parse(xhr.responseText);
const secs = typeof body.retry_after_secs === 'number' ? body.retry_after_secs : 60;
reject(new RateLimitError(secs));
} catch {
reject(new RateLimitError(60));
}
} else {
try {
const body = JSON.parse(xhr.responseText);
reject(new Error(body.message || `HTTP ${xhr.status}`));
} catch {
reject(new Error(`HTTP ${xhr.status}`));
}
}
});
xhr.addEventListener('error', () => reject(new Error('Netzwerkfehler')));
xhr.addEventListener('abort', () => reject(new Error('Abgebrochen')));
xhr.send(formData);
});
// Success — remove blob from IndexedDB, mark done
entry.status = 'done';
delete entry.blob;
await database.put(STORE_NAME, entry);
updateItemStatus(id, 'done');
} catch (e) {
if (e instanceof RateLimitError) {
// Reset to pending so it will be retried when the queue resumes
entry.status = 'pending';
await database.put(STORE_NAME, entry);
updateItemStatus(id, 'pending');
throw e; // Propagate to processQueue for scheduling
}
const msg = e instanceof Error ? e.message : 'Upload fehlgeschlagen.';
entry.status = 'error';
entry.error = msg;
await database.put(STORE_NAME, entry);
updateItemStatus(id, 'error', msg);
}
}
function updateItemStatus(
id: string,
status: QueueItem['status'],
error?: string
): void {
queueItems.update((items) =>
items.map((item) =>
item.id === id
? {
...item,
status,
progress: status === 'done' ? 100 : status === 'pending' ? 0 : item.progress,
error
}
: item
)
);
}