Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 151 additions & 21 deletions apps/web/src/components/uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,73 @@ import { getImportSummary, type ImportSummary } from "@/lib/api";
import { bulkUpload, isArchive, type BulkProgress } from "@/lib/bulk-upload";
import { useAuth } from "@/lib/auth";

const ACCEPT = ".txt,.zip,.rar,.7z,.tar,.gz,.tgz,.bz2,.xz";
const ARCHIVE_ACCEPT = ".zip,.rar,.7z,.tar,.gz,.tgz,.bz2,.xz";

/** Folder / files / archive upload with live import report. Calls onComplete when done. */
// readEntries yields a directory in batches; keep calling until it returns an empty batch.
async function readAllEntries(reader: FileSystemDirectoryReader): Promise<FileSystemEntry[]> {
const out: FileSystemEntry[] = [];
for (;;) {
const batch = await new Promise<FileSystemEntry[]>((res, rej) => reader.readEntries(res, rej));
if (batch.length === 0) break;
out.push(...batch);
}
return out;
}

// Resolve file entries in bounded batches: firing thousands of FileSystemFileEntry.file() calls at
// once (a folder of 10k+ files) overwhelms Chromium and the whole drop silently fails.
const FILE_RESOLVE_BATCH = 64;

// Collect dropped folders and archives only — loose individual files are intentionally ignored
// (uploading is folder- or archive-based). Folder entries are walked breadth-first with bounded
// concurrency; an archive is taken via getAsFile(). We decide archive-vs-file from entry.name
// (sync, no file resolution) so dropping a huge loose-file selection never touches the files.
// `onCount` reports progress on large folder drops.
async function filesFromDrop(dt: DataTransfer, onCount?: (n: number) => void): Promise<File[]> {
const items = Array.from(dt.items).filter((i) => i.kind === "file");
// Snapshot the entries synchronously (they must be read before the event yields).
const pairs = items.map((item) => ({ item, entry: item.webkitGetAsEntry?.() ?? null }));

// Browser without the entry API: keep only archives from the flat list.
if (pairs.every((p) => p.entry == null)) {
const archives = Array.from(dt.files).filter((f) => isArchive(f.name));
onCount?.(archives.length);
return archives;
}

const files: File[] = [];
const dirQueue: FileSystemDirectoryEntry[] = [];
for (const p of pairs) {
if (p.entry?.isDirectory) {
dirQueue.push(p.entry as FileSystemDirectoryEntry);
} else if (p.entry && isArchive(p.entry.name)) {
const f = p.item.getAsFile(); // archive — grab it now, while the item is valid
if (f) files.push(f);
}
// loose non-archive files are ignored
}
onCount?.(files.length);

while (dirQueue.length > 0) {
const children = await readAllEntries(dirQueue.shift()!.createReader());
const fileEntries: FileSystemFileEntry[] = [];
for (const c of children) {
if (c.isDirectory) dirQueue.push(c as FileSystemDirectoryEntry);
else fileEntries.push(c as FileSystemFileEntry);
}
for (let i = 0; i < fileEntries.length; i += FILE_RESOLVE_BATCH) {
const slice = fileEntries.slice(i, i + FILE_RESOLVE_BATCH);
const got = await Promise.all(
slice.map((fe) => new Promise<File>((res, rej) => fe.file(res, rej))),
);
files.push(...got);
onCount?.(files.length);
}
}
return files;
}

/** Folder / files / archive upload (button or drag-and-drop) with live import report. */
export function Uploader({ onComplete }: { onComplete?: () => void }) {
const { session } = useAuth();
const token = session?.access_token ?? "";
Expand All @@ -22,12 +86,45 @@ export function Uploader({ onComplete }: { onComplete?: () => void }) {
const [progress, setProgress] = React.useState<BulkProgress | null>(null);
const [report, setReport] = React.useState<ImportSummary | null>(null);
const [error, setError] = React.useState<string | null>(null);
const [dragOver, setDragOver] = React.useState(false);
const [scanCount, setScanCount] = React.useState<number | null>(null); // reading a dropped folder
const dragDepth = React.useRef(0); // ignore dragleave from child elements (enter/leave flicker)

function pick(list: FileList | null) {
if (!list) return;
setFiles(Array.from(list).filter((f) => f.name.endsWith(".txt") || isArchive(f.name)));
function pickFiles(list: File[]) {
setFiles(list.filter((f) => f.name.endsWith(".txt") || isArchive(f.name)));
setReport(null);
setProgress(null);
setError(null);
}

function pick(list: FileList | null) {
if (list) pickFiles(Array.from(list));
}

async function onDrop(e: React.DragEvent) {
e.preventDefault();
dragDepth.current = 0;
setDragOver(false);
if (busy) return;
setScanCount(0);
try {
pickFiles(await filesFromDrop(e.dataTransfer, setScanCount));
} catch (err) {
setError(`Could not read the dropped items: ${String(err)}`);
} finally {
setScanCount(null);
}
}

function onDragEnter(e: React.DragEvent) {
e.preventDefault();
dragDepth.current += 1;
if (!busy) setDragOver(true);
}

function onDragLeave() {
dragDepth.current -= 1;
if (dragDepth.current <= 0) setDragOver(false);
}

async function start() {
Expand All @@ -43,6 +140,7 @@ export function Uploader({ onComplete }: { onComplete?: () => void }) {
if (s.complete) break;
await new Promise((r) => setTimeout(r, 1500));
}
setFiles([]); // consumed — require a fresh selection before Upload is enabled again
onComplete?.();
} catch (e) {
setError(String(e));
Expand All @@ -52,23 +150,60 @@ export function Uploader({ onComplete }: { onComplete?: () => void }) {
}

const pct =
progress && progress.filesTotal > 0
? Math.round((progress.filesRead / progress.filesTotal) * 100)
progress && progress.bytesTotal > 0
? Math.min(100, Math.round((progress.bytesUploaded / progress.bytesTotal) * 100))
: 0;

return (
<Card>
<CardHeader>
<CardTitle>Upload hand history — folder, files, or an archive (.zip/.rar/.7z/…)</CardTitle>
<CardTitle>Upload hand history — a folder or an archive (.zip/.rar/.7z/…)</CardTitle>
</CardHeader>
<CardContent className="flex flex-col gap-4">
<div className="flex flex-wrap gap-3">
<Button variant="outline" onClick={() => folderRef.current?.click()} disabled={busy}>
<FolderUp className="h-4 w-4" /> Choose folder
</Button>
<Button variant="outline" onClick={() => filesRef.current?.click()} disabled={busy}>
<UploadCloud className="h-4 w-4" /> Choose files / archive
</Button>
<div
onDrop={onDrop}
onDragOver={(e) => e.preventDefault()}
onDragEnter={onDragEnter}
onDragLeave={onDragLeave}
className={`flex flex-col items-center gap-3 rounded-lg border-2 border-dashed p-6 text-center transition-colors ${
dragOver ? "border-primary bg-primary/5" : "border-border"
}`}
>
<UploadCloud className="h-7 w-7 text-muted-foreground" />
<p className="text-sm text-muted-foreground">
Drag &amp; drop a folder or an archive (.zip/.rar/.7z/…) here — or
</p>
<div className="flex flex-wrap justify-center gap-3">
<Button
variant="outline"
onClick={() => folderRef.current?.click()}
disabled={busy || scanCount !== null}
>
<FolderUp className="h-4 w-4" /> Choose folder
</Button>
<Button
variant="outline"
onClick={() => filesRef.current?.click()}
disabled={busy || scanCount !== null}
>
<UploadCloud className="h-4 w-4" /> Choose archive
</Button>
<Button onClick={start} disabled={busy || scanCount !== null || files.length === 0}>
{busy ? "Uploading…" : "Upload"}
</Button>
</div>
{scanCount !== null ? (
<p className="text-xs text-muted-foreground">
Reading {scanCount.toLocaleString("en-US")} files…
</p>
) : (
files.length > 0 &&
!busy && (
<p className="text-xs text-muted-foreground">
{files.length.toLocaleString("en-US")} file{files.length === 1 ? "" : "s"} ready to upload
</p>
)
)}
<input
ref={folderRef}
type="file"
Expand All @@ -82,15 +217,10 @@ export function Uploader({ onComplete }: { onComplete?: () => void }) {
ref={filesRef}
type="file"
multiple
accept={ACCEPT}
accept={ARCHIVE_ACCEPT}
className="hidden"
onChange={(e) => pick(e.target.files)}
/>
{files.length > 0 && (
<Button onClick={start} disabled={busy}>
{busy ? "Uploading…" : `Upload ${files.length.toLocaleString("en-US")}`}
</Button>
)}
</div>

{progress && (
Expand Down
38 changes: 25 additions & 13 deletions apps/web/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,36 @@ export async function uploadBulk(
return gzipped.byteLength;
}

/** Upload one archive (zip/rar/7z/…) for server-side extraction; returns its byte count. */
export async function uploadArchive(
/**
* Upload one archive (zip/rar/7z/…) for server-side extraction; returns its byte count.
* Uses XHR so the caller can track real upload progress via `onUploadProgress(loadedBytes)` —
* fetch() does not expose request-body upload progress.
*/
export function uploadArchive(
token: string,
file: File,
sessionId: string,
onUploadProgress?: (loaded: number) => void,
): Promise<number> {
const res = await fetch(`${API_URL}/ingest/archive`, {
method: "POST",
body: file,
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/octet-stream",
"X-Upload-Session": sessionId,
"X-Filename": encodeURIComponent(file.name),
},
return new Promise<number>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("POST", `${API_URL}/ingest/archive`);
xhr.setRequestHeader("Authorization", `Bearer ${token}`);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("X-Upload-Session", sessionId);
xhr.setRequestHeader("X-Filename", encodeURIComponent(file.name));
if (onUploadProgress) {
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) onUploadProgress(e.loaded);
};
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) resolve(file.size);
else reject(new Error(`API ${xhr.status}: ${xhr.responseText}`));
};
xhr.onerror = () => reject(new Error("Network error during archive upload"));
xhr.send(file);
});
if (!res.ok) throw new Error(`API ${res.status}: ${await res.text()}`);
return file.size;
}

/** Aggregated added/skipped report for one upload session. */
Expand Down
Loading
Loading