From 17fe3cf264807c0661918538efb9e95584b5ff13 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Thu, 11 Apr 2024 15:40:25 -0400 Subject: [PATCH] Everything in the browser --- .../components/forms/DocumentUpload.svelte | 134 ++++++++++++++++-- src/routes/app/upload/+page.server.ts | 76 ++-------- src/routes/app/upload/+page.svelte | 11 +- 3 files changed, 140 insertions(+), 81 deletions(-) diff --git a/src/lib/components/forms/DocumentUpload.svelte b/src/lib/components/forms/DocumentUpload.svelte index 203dfdadf..66802eefa 100644 --- a/src/lib/components/forms/DocumentUpload.svelte +++ b/src/lib/components/forms/DocumentUpload.svelte @@ -1,11 +1,109 @@ - + +
diff --git a/src/routes/app/upload/+page.server.ts b/src/routes/app/upload/+page.server.ts index 25bdddf19..d6133e224 100644 --- a/src/routes/app/upload/+page.server.ts +++ b/src/routes/app/upload/+page.server.ts @@ -10,79 +10,19 @@ import type { import { CSRF_COOKIE_NAME, DEFAULT_LANGUAGE } from "@/config/config.js"; import * as documents from "$lib/api/documents"; import { unwrap } from "$lib/components/inputs/Select.svelte"; +import { upload } from "$lib/components/forms/DocumentUpload.svelte"; + +export function load({ cookies }) { + const csrf_token = cookies.get(CSRF_COOKIE_NAME); + + return { csrf_token }; +} export const actions = { default: async ({ request, cookies, fetch }) => { const csrf_token = cookies.get(CSRF_COOKIE_NAME); const form = await request.formData(); - // one per file - const files = Array.from(form.getAll("uploads")) as File[]; - const titles = form.getAll("title") as string[]; - const filenames = form.getAll("filename") as string[]; - - // one per batch - const access = form.get("access") as Access; - - // value is a JSON string - const ocr_engine: OCREngine = unwrap(form.get("ocr_engine") as string); - const force_ocr = Boolean(form.get("force_ocr")); - const revision_control = Boolean(form.get("revision_control")); - const projects = unwrap(form.get("projects") as string, []); - const language = unwrap(form.get("language") as string, DEFAULT_LANGUAGE); - - // put things together - const docs: DocumentUpload[] = titles.map((title, i) => { - return { - title, - access, - language, - projects: projects.map((p: Project) => p.id), - revision_control, - }; - }); - - let created: Document[]; - try { - created = await documents.create(docs, csrf_token, fetch); - } catch (err) { - return { - success: false, - error: err, - }; - } - - // upload - const uploads = created.map((d, i) => ({ - id: d.id, - presigned_url: new URL(d.presigned_url), - file: files[i], - })); - - // todo: handle retries and errors - const upload_responses = await documents.upload(uploads, fetch); - - console.log(upload_responses.map((r) => r.status)); - - // process - const process_response = await documents.process( - created.map((d) => ({ - id: d.id, - force_ocr, - ocr_engine: ocr_engine.value, - })), - csrf_token, - fetch, - ); - - // todo: i18n - const message = process_response.ok - ? `Uploaded ${created.length} documents` - : process_response.statusText; - - return { - success: true, - message, - }; + return upload(form, csrf_token, fetch); }, } satisfies Actions; diff --git a/src/routes/app/upload/+page.svelte b/src/routes/app/upload/+page.svelte index 3bf06681a..7a26fba48 100644 --- a/src/routes/app/upload/+page.svelte +++ b/src/routes/app/upload/+page.svelte @@ -1,8 +1,9 @@ @@ -13,9 +14,13 @@

Upload documents

- {#if form?.success} + {#if form?.type === "success"}

- {form.message} + {form.data?.message} +

+ {:else if form?.type === "error"} +

+ {form?.error}

{:else}