Skip to content

Commit 4dcb1aa

Browse files
committed
file upload to markdown: better error handling and refactoring
1 parent 7c2da1f commit 4dcb1aa

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/packages/frontend/editors/slate/upload.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { useEffect, useMemo, useRef } from "react";
99
import { Dropzone, BlobUpload } from "@cocalc/frontend/file-upload";
1010
import { getFocus } from "./format/commands";
1111
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
12-
import { BASE_URL } from "@cocalc/frontend/misc";
1312

1413
export default function useUpload(
1514
editor: SlateEditor,
@@ -59,33 +58,38 @@ export default function useUpload(
5958
// depends on in a ref and only create it once.
6059
const updloadEventHandlers = useMemo(() => {
6160
return {
61+
error: (_, message) => {
62+
actions.set_error(`${message}`);
63+
},
6264
sending: ({ name }) => {
6365
actionsRef.current?.set_status?.(`Uploading ${name}...`);
6466
},
6567
complete: (file) => {
6668
actionsRef.current?.set_status?.("");
67-
const { uuid } = JSON.parse(file.xhr.responseText);
68-
const url = `${BASE_URL}/blobs/${encodeURIComponent(
69-
file.upload.filename,
70-
)}?uuid=${uuid}`;
69+
const { url } = file;
70+
if (!url) {
71+
// probably an error
72+
return;
73+
}
7174
let node;
72-
if (!file.type.includes("image")) {
75+
const { dataURL, height, upload } = file;
76+
if (!height && !dataURL?.startsWith("data:image")) {
7377
node = {
7478
type: "link",
7579
isInline: true,
76-
children: [{ text: file.name }],
80+
children: [{ text: upload.filename ? upload.filename : "file" }],
7781
url,
78-
};
82+
} as const;
7983
} else {
8084
node = {
8185
type: "image",
8286
isInline: true,
8387
isVoid: true,
8488
src: url,
8589
children: [{ text: "" }],
86-
};
90+
} as const;
8791
}
88-
Transforms.insertFragment(editor, [node as any], {
92+
Transforms.insertFragment(editor, [node], {
8993
at: getFocus(editor),
9094
});
9195
},

src/packages/frontend/file-upload.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { join } from "path";
2626
import { useStudentProjectFunctionality } from "@cocalc/frontend/course";
2727
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
2828
import { Button } from "antd";
29+
import { BASE_URL } from "@cocalc/frontend/misc";
30+
import { MAX_BLOB_SIZE } from "@cocalc/util/db-schema/blobs";
2931

3032
// 3GB upload limit -- since that's the default filesystem quota
3133
// and it should be plenty?
@@ -309,7 +311,9 @@ export const FileUploadWrapper: React.FC<FileUploadWrapperProps> = (props) => {
309311
props.on_close();
310312
}
311313
if (remove_all && dropzone.current != null) {
312-
dropzone.current.removeAllFiles();
314+
try {
315+
dropzone.current.removeAllFiles();
316+
} catch (_) {}
313317
}
314318
set_files([]);
315319
}
@@ -534,8 +538,29 @@ export function BlobUpload(props) {
534538
return (
535539
<FileUploadWrapper
536540
{...props}
541+
event_handlers={{
542+
...props.event_handlers,
543+
sending: props.event_handlers?.sending,
544+
removedfile: props.event_handlers?.removedfile,
545+
complete: (file) => {
546+
if (file.xhr?.responseText) {
547+
const { uuid } = JSON.parse(file.xhr.responseText);
548+
const url = `${BASE_URL}/blobs/${encodeURIComponent(
549+
file.upload.filename,
550+
)}?uuid=${uuid}`;
551+
props.event_handlers?.complete({ ...file, uuid, url });
552+
} else {
553+
// e.g., if there was an error
554+
props.event_handlers?.complete(file);
555+
}
556+
},
557+
}}
537558
dest_path={""}
538-
config={{ url, ...props.config }}
559+
config={{
560+
url,
561+
maxFilesize: MAX_BLOB_SIZE / (1000 * 1000),
562+
...props.config,
563+
}}
539564
/>
540565
);
541566
}

0 commit comments

Comments
 (0)