Skip to content

Commit 30a3a79

Browse files
author
Rajat Saxena
committed
Text editor image upload ported to new medialit api
1 parent 00aa5d3 commit 30a3a79

3 files changed

Lines changed: 73 additions & 80 deletions

File tree

packages/components-library/src/media-selector/file-upload-dialog.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,6 @@ export function FileUploadAlertDialog({
175175
}
176176
};
177177

178-
const getMedia = async (mediaId: string) => {
179-
const fetch = new FetchBuilder()
180-
.setUrl(`${address.backend}/api/media/${mediaId}/${type}`)
181-
.setHttpMethod("GET")
182-
.setIsGraphQLEndpoint(false)
183-
.build();
184-
return await fetch.exec();
185-
};
186-
187178
const handleReset = () => {
188179
if (uploadRef.current) {
189180
uploadRef.current.abort();

packages/text-editor/src/extensions.ts

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
DocExtension,
44
DropCursorExtension,
55
HeadingExtension,
6-
ImageAttributes,
76
ImageExtension,
87
LinkExtension,
98
OrderedListExtension,
@@ -30,75 +29,7 @@ import { CodeMirrorExtension } from "@remirror/extension-codemirror6";
3029
import { TableExtension } from "@remirror/extension-react-tables";
3130
import { oneDark } from "@codemirror/theme-one-dark";
3231
import { basicSetup } from "codemirror";
33-
import { DelayedPromiseCreator, ErrorConstant, invariant } from "remirror";
34-
import { FetchBuilder } from "@courselit/utils";
35-
import { Media } from "@courselit/common-models";
36-
37-
// const wysiwygPresetArrayWithoutImageExtension = wysiwygPreset().filter(
38-
// (extension) => extension instanceof ImageExtension !== true,
39-
// );
40-
41-
type SetProgress = (progress: number) => void;
42-
43-
interface FileWithProgress {
44-
file: File;
45-
progress: SetProgress;
46-
}
47-
48-
async function getPresignedUrl(url: string) {
49-
const fetch = new FetchBuilder()
50-
.setUrl(`${url}/api/media/presigned`)
51-
.setIsGraphQLEndpoint(false)
52-
.build();
53-
const response = await fetch.exec();
54-
return response.url;
55-
}
56-
57-
function getUploadHandler(url: string) {
58-
return function uploadFileToMediaLit(
59-
files: FileWithProgress[],
60-
): DelayedPromiseCreator<ImageAttributes>[] {
61-
invariant(files.length > 0, {
62-
code: ErrorConstant.EXTENSION,
63-
message:
64-
"The upload handler was applied for the image extension without any valid files",
65-
});
66-
67-
let completed = 0;
68-
const promises: DelayedPromiseCreator<ImageAttributes>[] = [];
69-
70-
for (const { file, progress } of files) {
71-
promises.push(
72-
() =>
73-
new Promise<ImageAttributes>((resolve, reject) => {
74-
getPresignedUrl(url)
75-
.then((presignedUrl) => {
76-
const fD = new FormData();
77-
fD.append("caption", file.name);
78-
fD.append("access", "public");
79-
fD.append("file", file);
80-
81-
return fetch(presignedUrl, {
82-
method: "POST",
83-
body: fD,
84-
});
85-
})
86-
.then((data) => data.json())
87-
.then((data: Media) => {
88-
completed += 1;
89-
progress(completed / files.length);
90-
resolve({
91-
src: data.file,
92-
fileName: data.originalFileName,
93-
});
94-
})
95-
.catch((err) => reject(err));
96-
}),
97-
);
98-
}
99-
return promises;
100-
};
101-
}
32+
import { getUploadHandler } from "./file-upload-extention";
10233

10334
export const getExtensions = (placeholder, url) => () => [
10435
new DocExtension({}),
@@ -143,5 +74,4 @@ export const getExtensions = (placeholder, url) => () => [
14374
new OrderedListExtension(),
14475
new TaskListExtension(),
14576
new ShortcutsExtension(),
146-
// ...wysiwygPresetArrayWithoutImageExtension,
14777
];
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DelayedPromiseCreator, ErrorConstant, invariant } from "remirror";
2+
import { FetchBuilder } from "@courselit/utils";
3+
import { Media } from "@courselit/common-models";
4+
import { ImageAttributes } from "remirror/extensions";
5+
6+
type SetProgress = (progress: number) => void;
7+
8+
interface FileWithProgress {
9+
file: File;
10+
progress: SetProgress;
11+
}
12+
13+
async function getPresignedUrl(url: string) {
14+
const fetch = new FetchBuilder()
15+
.setUrl(`${url}/api/media/presigned`)
16+
.setIsGraphQLEndpoint(false)
17+
.build();
18+
return await fetch.exec();
19+
}
20+
21+
export function getUploadHandler(url: string) {
22+
return function uploadFileToMediaLit(
23+
files: FileWithProgress[],
24+
): DelayedPromiseCreator<ImageAttributes>[] {
25+
invariant(files.length > 0, {
26+
code: ErrorConstant.EXTENSION,
27+
message:
28+
"The upload handler was applied for the image extension without any valid files",
29+
});
30+
31+
let completed = 0;
32+
const promises: DelayedPromiseCreator<ImageAttributes>[] = [];
33+
34+
for (const { file, progress } of files) {
35+
promises.push(
36+
() =>
37+
new Promise<ImageAttributes>((resolve, reject) => {
38+
if (file.size > 2097152) {
39+
// 2 MB (taken from: https://stackoverflow.com/a/49490014)
40+
return reject("File is larger than 2MB");
41+
}
42+
getPresignedUrl(url)
43+
.then(({ signature, endpoint }) => {
44+
const fD = new FormData();
45+
fD.append("caption", file.name);
46+
fD.append("access", "public");
47+
fD.append("file", file);
48+
49+
return fetch(`${endpoint}/media/create`, {
50+
method: "POST",
51+
headers: {
52+
"x-medialit-signature": signature,
53+
},
54+
body: fD,
55+
});
56+
})
57+
.then((data) => data.json())
58+
.then((data: Media) => {
59+
completed += 1;
60+
progress(completed / files.length);
61+
resolve({
62+
src: data.file,
63+
fileName: data.originalFileName,
64+
});
65+
})
66+
.catch((err) => reject(err.message));
67+
}),
68+
);
69+
}
70+
return promises;
71+
};
72+
}

0 commit comments

Comments
 (0)