From 735df366226d3a107156915d6e73f45dc0637c93 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Thu, 4 Apr 2024 17:37:15 -0400 Subject: [PATCH] Figure out where to hold onto files before upload --- .../components/forms/DocumentUpload.svelte | 81 ++++++++++--------- src/lib/components/inputs/File.svelte | 2 + src/lib/utils/validateFiles.ts | 7 ++ src/routes/app/upload/+page.server.ts | 2 +- src/routes/app/upload/+page.svelte | 20 +---- 5 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/lib/components/forms/DocumentUpload.svelte b/src/lib/components/forms/DocumentUpload.svelte index ae45d92a0..00eacff73 100644 --- a/src/lib/components/forms/DocumentUpload.svelte +++ b/src/lib/components/forms/DocumentUpload.svelte @@ -18,22 +18,18 @@ import Text from "../inputs/Text.svelte"; import { DOCUMENT_TYPES } from "@/config/config.js"; - import { removeUnsupportedTypes } from "@/lib/utils/validateFiles"; - - interface UploadFile { - index: number; - name: string; - type: string; - size: number; - file: File; - } + import { isSupported } from "@/lib/utils/validateFiles"; + import { afterUpdate } from "svelte"; + + export let files: File[] = []; - export let files: UploadFile[] = []; + let uploader: HTMLInputElement; let form: HTMLFormElement; let fileDropActive: boolean; + // todo: fetch projects in load function const projectOptions = [ { value: "1", label: "FBI Files" }, { value: "2", label: "1033 Program" }, @@ -55,29 +51,15 @@ let ocrEngine = ocrEngineOptions[0]; export function values() { - return Object.fromEntries(new FormData(form)); - } - - function createUploadFile(file: File, index: number): UploadFile { - return { - index, - name: file.name, - type: file.type, - size: file.size, - file, - }; + return Array.from(new FormData(form)); } function addFiles(filesToAdd: FileList) { - console.log(...filesToAdd); - // const supportedFiles = removeUnsupportedTypes([...filesToAdd]); - // files = files.concat(supportedFiles.map(createUploadFile)); - const newFiles = Array.from(filesToAdd).map(createUploadFile); - files = [...files, ...newFiles]; + files = files.concat(Array.from(filesToAdd).filter(isSupported)); } - function removeFile(fileToRemove: UploadFile) { - files = files.filter((file) => file.index !== fileToRemove.index); + function removeFile(index: number) { + files = files.filter((f, i) => i !== index); } function formatFileType(filetype: string) { @@ -87,7 +69,19 @@ return "unknown"; } - $: console.log(files); + $: total = files.reduce((t, file) => { + return t + file.size; + }, 0); + + afterUpdate(() => { + const dt = new DataTransfer(); + + files.forEach((file) => { + dt.items.add(file); + }); + + uploader.files = dt.files; + });
- {#each files as file} + {#each files as file, index}

{formatFileType(file.type)} / {filesize(file.size)}

+
-
{:else} - Get started by selecting, pasting, or dragging-and-dropping files + Get started by selecting, pasting or dragging-and-dropping files below {/each} + +
+

Total upload size: {filesize(total)}

+
@@ -193,6 +186,14 @@ + +
@@ -312,4 +313,8 @@ border: none; border-top: 1px solid var(--gray-2, #d8dee2); } + + input[name="uploads"] { + display: none; + } diff --git a/src/lib/components/inputs/File.svelte b/src/lib/components/inputs/File.svelte index 754a5b620..67e6f8ca7 100644 --- a/src/lib/components/inputs/File.svelte +++ b/src/lib/components/inputs/File.svelte @@ -12,6 +12,7 @@ export let onFileSelect: (files: FileList) => void; export let multiple = false; export let buttonMode: ComponentProps