This commit is contained in:
m
2026-08-25 17:04:17 +02:00
parent 7e7d669d63
commit a791df26bf
8 changed files with 130 additions and 38 deletions
+7
View File
@@ -286,6 +286,13 @@ export const fileStore = {
return rowToRecord(row, getTagsForFile(row.id));
},
getByLocalUri(localUri: string): FileRecord | null {
const d = getDb();
const row = d.select().from(files).where(eq(files.localUri, localUri)).get() as FileRow | undefined;
if (!row) return null;
return rowToRecord(row, getTagsForFile(row.id));
},
getRootFolders(): FileRecord[] {
const d = getDb();
const rows = d.select().from(files)
+20
View File
@@ -3,6 +3,7 @@ import { createMMKV } from 'react-native-mmkv';
import { apiClient } from '../api/client';
import { API_BASE_URL, ENDPOINTS } from '../constants/api';
import { ApiError, UploadError } from '../types';
import { fileStore } from './fileStore';
export type UploadFile = { uri: string; type: string; name: string };
export type UploadResult = { name: string; id: string };
@@ -10,6 +11,8 @@ export type UploadResult = { name: string; id: string };
export const UPLOAD_MAX_RETRIES = 3;
const BASE_RETRY_DELAY_MS = 1000;
export const activeUploadUris = new Set<string>();
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error';
export type UploadTask = {
@@ -222,6 +225,7 @@ class UploadQueue {
private async runTask(task: UploadTask) {
let willRetry = false;
activeUploadUris.add(task.file.uri);
try {
const fsFile = new File(task.file.uri);
const headers: Record<string, string> = {};
@@ -263,6 +267,7 @@ class UploadQueue {
task.progress = 100;
task.result = item as UploadResult;
task.updatedAt = Date.now();
this.linkResultToStore(task);
this.persist();
this.notify();
this.scheduleCleanup();
@@ -293,6 +298,7 @@ class UploadQueue {
}
} finally {
this.active--;
activeUploadUris.delete(task.file.uri);
this.notify();
if (!willRetry) {
this.processNext();
@@ -300,6 +306,20 @@ class UploadQueue {
}
}
private linkResultToStore(task: UploadTask) {
try {
const backendId = task.result?.id;
if (!backendId) return;
const entry = fileStore.getByLocalUri(task.file.uri);
if (!entry || entry.backendId) return;
fileStore.updatePartial(entry.id, {
backendId,
syncStatus: 'synced',
source: 'synced',
});
} catch {}
}
private scheduleCleanup() {
if (this.cleanupTimer) return;
this.cleanupTimer = setTimeout(() => {