Skip to content

Commit

Permalink
Fix types on uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Nov 4, 2024
1 parent 7030f3c commit 6ac15c1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 43 deletions.
45 changes: 37 additions & 8 deletions src/lib/components/forms/DocumentUpload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ progress through the three-part upload process.
</script>

<script lang="ts">
import type { Access, DocumentUpload, Project } from "$lib/api/types";
import type {
Access,
DocumentUpload,
Maybe,
Nullable,
Project,
} from "$lib/api/types";
import { filesize } from "filesize";
import { onMount } from "svelte";
Expand Down Expand Up @@ -65,7 +71,7 @@ progress through the three-part upload process.
export let files: File[] = getFilesToUpload();
export let projects: Project[] = [];
let csrf_token: string;
let csrf_token: Maybe<string>;
let fileDropActive: boolean;
/**
Expand Down Expand Up @@ -99,7 +105,9 @@ progress through the three-part upload process.
];
let ocrEngine = ocrEngineOptions[0];
let add_to_projects: Project[] = [getProjectToUpload()].filter(Boolean);
let add_to_projects: Project[] = [getProjectToUpload()].filter(
Boolean,
) as Project[];
$: total = Object.values(STATUS).reduce((t, status) => {
return t + status.file.size;
Expand Down Expand Up @@ -171,7 +179,7 @@ progress through the three-part upload process.
upload(
id,
{
title: titles[i],
title: titles[i] ?? "",
original_extension: getFileExtension(status.file),
access,
projects: add_to_projects.map((p) => p.id),
Expand All @@ -190,6 +198,9 @@ progress through the three-part upload process.
// upload one file
async function upload(id: string, metadata: DocumentUpload, form: FormData) {
if (!csrf_token) return;
if (!STATUS[id]) return;
const file = STATUS[id].file;
// create
Expand All @@ -210,6 +221,18 @@ progress through the three-part upload process.
return console.error(error);
}
if (!document || !document.presigned_url) {
STATUS[id] = {
...STATUS[id],
error: {
status: 500,
message: "API returned invalid document. Please try again.",
},
};
return;
}
// upload
let presigned_url: URL;
try {
Expand All @@ -220,16 +243,22 @@ progress through the three-part upload process.
message: "Invalid presigned URL",
errors: e,
};
}
if (error) {
STATUS[id].error = error;
return console.error(error);
}
if (!presigned_url) {
STATUS[id].error = {
status: 500,
message: "Invalid presigned URL",
};
return;
}
STATUS[id].step = "uploading";
const resp = await documents
.upload(new URL(document.presigned_url), file)
.upload(presigned_url, file)
.catch(console.error);
if (!resp) {
Expand All @@ -245,7 +274,7 @@ progress through the three-part upload process.
const force_ocr = form.get("force_ocr") === "on";
({ error } = await documents.process(
[{ id: document.id, force_ocr, ocr_engine: ocrEngine.value }],
[{ id: document?.id, force_ocr, ocr_engine: ocrEngine?.value }],
csrf_token,
));
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/forms/UploadListItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
<!-- file is ready, uploading or processing -->
<Flex align="baseline" gap={1} role="listitem" class="upload-list-item" {id}>
<div class="title">
{#if linkable}
<a href={canonicalUrl(status.document).href}>{status.document.title}</a>
{#if linkable && status.document}
<a href={canonicalUrl(status.document).href}>{status.document?.title}</a>
{:else}
<Field inline sronly title={$_("common.title")} {description}>
<Text
Expand Down
15 changes: 8 additions & 7 deletions src/lib/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import {

const types = new Set(DOCUMENT_TYPES);

export function getFileExtensionFromType(filetype?: string) {
export function getFileExtensionFromType(filetype?: string): string {
if (filetype?.includes("/")) {
return filetype.split("/")[1];
return filetype.split("/")[1] ?? "";
}
return filetype;
return filetype ?? "";
}

export function getFileExtension(file?: File): Maybe<string> {
if (!file) return;
export function getFileExtension(file?: File): string {
if (!file) return "";
if (file.name.includes(".")) {
return file.name.toLowerCase().trim().split(".").pop();
return file.name.toLowerCase().trim().split(".").pop() ?? "";
} else if (file.type) {
return getFileExtensionFromType(file.type);
return getFileExtensionFromType(file.type) ?? "";
}
return "";
}

export function isSupported(file: File): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/tests/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("files.getFileExtensionFromType", () => {
it("returns the provided input if not a Mimetype", () => {
expect(files.getFileExtensionFromType("")).toEqual("");
expect(files.getFileExtensionFromType("pdf")).toEqual("pdf");
expect(files.getFileExtensionFromType(undefined)).toEqual(undefined);
expect(files.getFileExtensionFromType(undefined)).toEqual("");
});
});

Expand All @@ -33,7 +33,7 @@ describe("files.getFileExtension", () => {

it("returns undefined if neither filename extension nor file type", () => {
const file = new File([], "filename");
expect(files.getFileExtension(file)).toBeUndefined();
expect(files.getFileExtension(file)).toEqual("");
});
});

Expand Down
22 changes: 0 additions & 22 deletions src/routes/(app)/upload/+page.server.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/routes/(app)/upload/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
export let data;
$: form = $page.form;
$: projects = data.projects.results;
$: projects = data.projects?.results ?? [];
</script>

<svelte:head>
Expand Down

0 comments on commit 6ac15c1

Please sign in to comment.