Skip to content

Commit

Permalink
Uploads working
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 8, 2024
1 parent 2c5362a commit 5d9bd4d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
20 changes: 14 additions & 6 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import type {
} from "./types";

import { error } from "@sveltejs/kit";
import { CSRF_HEADER_NAME } from "@/config/config.js";
import { DEFAULT_EXPAND } from "@/api/common.js";
import { isOrg } from "@/api/types/orgAndUser";
import { APP_URL, BASE_API_URL } from "@/config/config.js";
import { APP_URL, BASE_API_URL, CSRF_HEADER_NAME } from "@/config/config.js";
import { isErrorCode } from "../utils";

/**
Expand Down Expand Up @@ -69,6 +68,7 @@ export async function get(

return resp.json();
}

/**
* Create new documents in a batch (or a batch of one).
*
Expand All @@ -92,12 +92,15 @@ export async function create(
method: "POST",
headers: {
"Content-type": "application/json",
CSRF_HEADER_NAME: csrf_token,
[CSRF_HEADER_NAME]: csrf_token,
Referer: APP_URL,
},
body: JSON.stringify(documents),
referrerPolicy: "origin",
});

if (isErrorCode(resp.status)) {
console.error(await resp.json());
error(resp.status, resp.statusText);
}

Expand Down Expand Up @@ -143,16 +146,21 @@ export async function upload(
* @export
*/
export async function process(
documents: { id: string | number; force_ocr: boolean }[],
documents: { id: string | number; force_ocr: boolean; ocr_engine: string }[],
csrf_token: string,
fetch = globalThis.fetch,
): Promise<Response> {
console.log(`processing ${documents.length} documents`);
const endpoint = new URL("document/process/", BASE_API_URL);
const endpoint = new URL("documents/process/", BASE_API_URL);

return fetch(endpoint, {
credentials: "include",
method: "POST",
headers: { "Content-type": "application/json" },
headers: {
"Content-type": "application/json",
[CSRF_HEADER_NAME]: csrf_token,
Referer: APP_URL,
},
body: JSON.stringify(documents),
});
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/api/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { Page } from "@/api/types/common";

export type Access = "public" | "private" | "organization"; // https://www.documentcloud.org/help/api#access-levels

export type Data = Record<string, string[]>;

export type Status = "success" | "readable" | "pending" | "error" | "nofile"; // https://www.documentcloud.org/help/api#statuses

export type Sizes = "thumbnail" | "small" | "normal" | "large" | "xlarge";
Expand Down Expand Up @@ -70,11 +72,15 @@ export interface AddOnListItem {
// most fields are optional
export interface DocumentUpload {
access: Access;
data?: Record<string, string[]>;
data?: Data;
language?: string;
ocr_engine?: string;
original_extension?: string;
noindex?: boolean;
projects?: number[]; // project ids only
related_article?: string | URL;
revision_control?: boolean;
source?: string;
title: string;
}

Expand All @@ -86,7 +92,7 @@ export interface Document {
asset_url: string | URL;
canonical_url: string | URL;
created_at: string | Date;
data: Record<string, string[]>;
data: Data;
description?: string;
edit_access: boolean;
file_hash?: string;
Expand Down
1 change: 1 addition & 0 deletions src/lib/components/forms/DocumentUpload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { filesize } from "filesize";
import { _ } from "svelte-i18n";
import { File16, File24, Upload16, XCircleFill24 } from "svelte-octicons";
import { enhance } from "$app/forms";
import Button from "../common/Button.svelte";
import Empty from "../common/Empty.svelte";
Expand Down
24 changes: 14 additions & 10 deletions src/routes/app/upload/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import * as documents from "$lib/api/documents";
export const actions = {
default: async ({ request, cookies, fetch }) => {
const csrf_token = cookies.get(CSRF_COOKIE_NAME);
const data = await request.formData();
const form = await request.formData();

// one per file
const files = Array.from(data.getAll("uploads")) as File[];
const titles = data.getAll("title") as string[];
const filenames = data.getAll("filename") as string[];
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 = data.get("access") as Access;
const access = form.get("access") as Access;

// value is a JSON string
const ocr_engine: OCREngine = JSON.parse(data.get("ocr_engine") as string);
const force_ocr = Boolean(data.get("force_ocr"));
const revision_control = Boolean(data.get("revision_control"));
const ocr_engine: OCREngine = JSON.parse(form.get("ocr_engine") as string);
const force_ocr = Boolean(form.get("force_ocr"));
const revision_control = Boolean(form.get("revision_control"));

// not yet implemented
// const projects = data.get("projects");
Expand All @@ -31,7 +31,6 @@ export const actions = {
return {
title,
access,
ocr_engine: ocr_engine.value,
revision_control,
};
});
Expand All @@ -52,7 +51,12 @@ export const actions = {

// process
const process_response = await documents.process(
created.map((d) => ({ id: d.id, force_ocr })),
created.map((d) => ({
id: d.id,
force_ocr,
ocr_engine: ocr_engine.value,
})),
csrf_token,
fetch,
);

Expand Down

0 comments on commit 5d9bd4d

Please sign in to comment.