Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): upload images from the text editor through the local worker

Elian Doranb01c18865fdd
diff --git a/apps/client/src/services/glob.ts b/apps/client/src/services/glob.ts
index de9b8c87e4..92c0337726 100644
--- a/apps/client/src/services/glob.ts
+++ b/apps/client/src/services/glob.ts
@@ -11,6 +11,7 @@ function setupGlobs() {
window.glob.getComponentByEl = (el) => appContext.getComponentByEl(el);
window.glob.getHeaders = server.getHeaders;
+ window.glob.getLocalUpload = server.getLocalUpload;
window.glob.getReferenceLinkTitle = (href) => linkService.getReferenceLinkTitle(href);
window.glob.getReferenceLinkTitleSync = (href, storedTitle) =>
linkService.getReferenceLinkTitleSync(href, storedTitle);
diff --git a/apps/client/src/services/server.ts b/apps/client/src/services/server.ts
index b949f40bb1..eab194c341 100644
--- a/apps/client/src/services/server.ts
+++ b/apps/client/src/services/server.ts
@@ -154,7 +154,7 @@ async function uploadForm(url: string, options: UploadFormOptions): Promise<any>
}
}
- const localUpload = USE_LOCAL_FETCH ? window.standaloneApi?.localUpload : undefined;
+ const localUpload = getLocalUpload();
if (localUpload) {
return await localUploadForm(localUpload, url, headers, options);
}
@@ -177,6 +177,11 @@ async function uploadForm(url: string, options: UploadFormOptions): Promise<any>
});
}
+/** The standalone leader tab's in-page upload to its worker, or `undefined` elsewhere. */
+function getLocalUpload() {
+ return USE_LOCAL_FETCH ? window.standaloneApi?.localUpload : undefined;
+}
+
async function localUploadForm(
localUpload: NonNullable<StandaloneApi["localUpload"]>,
url: string,
@@ -552,5 +557,6 @@ export default {
uploadForm,
// don't remove, used from CKEditor image upload!
getHeaders,
+ getLocalUpload,
getMaxKnownEntityChangeId: () => maxKnownEntityChangeId
};
diff --git a/apps/client/src/types.d.ts b/apps/client/src/types.d.ts
index 8d65072d00..79f99a5510 100644
--- a/apps/client/src/types.d.ts
+++ b/apps/client/src/types.d.ts
@@ -20,6 +20,7 @@ interface CustomGlobals extends BootstrapDefinition {
isMobile: typeof utils.isMobile;
getComponentByEl: typeof appContext.getComponentByEl;
getHeaders: typeof server.getHeaders;
+ getLocalUpload: typeof server.getLocalUpload;
getReferenceLinkTitle: (href: string) => Promise<string>;
getReferenceLinkTitleSync: (href: string, storedTitle?: string) => string;
getActiveContextNote: () => FNote | null;
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index e569f5c8e9..2dc14e739a 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -1,5 +1,7 @@
import "ckeditor5";
+import type { StandaloneApi } from "@triliumnext/commons";
+
import type { InlineEmbedSource } from "./plugins/content_embed/inline_source.js";
declare global {
@@ -195,6 +197,8 @@ declare global {
renderLinkMention(container: HTMLElement, metadata: Pick<LinkEmbedMetadata, "url" | "title" | "favicon">, editable?: boolean): void;
}
+ type LocalUpload = NonNullable<StandaloneApi["localUpload"]>;
+
var glob: {
getComponentByEl<T extends Component>(el: unknown): T;
getActiveContextNote(): {
@@ -202,6 +206,11 @@ declare global {
};
/** The headers of a request to the server, with `headers` added to them. */
getHeaders(headers?: Record<string, string | undefined>): Promise<Record<string, string>>;
+ /**
+ * The host's in-page upload to its local server, when it has one. Uploads go through it
+ * because a raw request with a body cannot reach that server from every platform.
+ */
+ getLocalUpload?(): LocalUpload | undefined;
getReferenceLinkTitle(href: string): Promise<string>;
getReferenceLinkTitleSync(href: string, storedTitle?: string): string;
};
diff --git a/packages/ckeditor5/src/plugins/uploadimage.spec.ts b/packages/ckeditor5/src/plugins/uploadimage.spec.ts
index bafe7f03df..3bf6ce8302 100644
--- a/packages/ckeditor5/src/plugins/uploadimage.spec.ts
+++ b/packages/ckeditor5/src/plugins/uploadimage.spec.ts
@@ -338,6 +338,73 @@ describe("UploadimagePlugin", () => {
await expect(uploadPromise).resolves.toEqual({ default: "http://example.com/pic.png" });
});
+ describe("with a local server", () => {
+ let localUpload: ReturnType<typeof vi.fn>;
+
+ function answerWith(body: string, status = 200, contentType = "application/json") {
+ localUpload.mockResolvedValue(new Response(body, { status, headers: { "content-type": contentType } }));
+ }
+
+ beforeEach(() => {
+ localUpload = vi.fn();
+ installGlobMock({
+ getHeaders,
+ getActiveContextNote: () => ({ noteId: "noteAbc" }),
+ getComponentByEl: () => ({}),
+ getLocalUpload: () => localUpload
+ });
+ });
+
+ it("hands the file to the local server instead of sending a request", async () => {
+ answerWith(JSON.stringify({ uploaded: true, url: "api/attachments/att1/image/pic.png" }));
+ const file = new File(["content"], "pic.png", { type: "image/png" });
+
+ await expect(createAdapter(file).upload())
+ .resolves.toEqual({ default: "api/attachments/att1/image/pic.png" });
+ expect(FakeXHR.last).toBeUndefined();
+ expect(localUpload).toHaveBeenCalledExactlyOnceWith("api/notes/noteAbc/attachments/upload", {
+ method: "POST",
+ headers: { Authorization: "Bearer token", "x-csrf": "abc" },
+ fields: {},
+ file
+ });
+ });
+
+ it("reports a failed upload as the request path does", async () => {
+ const file = new File(["content"], "pic.png", { type: "image/png" });
+
+ answerWith(JSON.stringify({ uploaded: false, error: { message: "Disk full" } }), 500);
+ await expect(createAdapter(file).upload()).rejects.toBe("Cannot upload file: pic.png. Disk full");
+
+ answerWith("<html>Too large</html>", 413, "text/html");
+ await expect(createAdapter(file).upload()).rejects.toBe(
+ "Cannot upload file: pic.png. The file is too large to be uploaded (HTTP 413). If Trilium is behind a reverse proxy, raise its request body size limit.");
+
+ localUpload.mockRejectedValue(new Error("Worker gone"));
+ await expect(createAdapter(file).upload()).rejects.toBe("Cannot upload file: pic.png.");
+ });
+
+ it("rejects with no reason when aborted, and ignores the late answer", async () => {
+ let answer: (response: Response) => void = () => {};
+ localUpload.mockReturnValue(new Promise<Response>(resolve => {
+ answer = resolve;
+ }));
+ const adapter = createAdapter(new File(["content"], "pic.png", { type: "image/png" }));
+
+ const uploadPromise = adapter.upload();
+ await vi.waitFor(() => expect(localUpload).toHaveBeenCalledOnce());
+ adapter.abort();
+ answer(new Response(JSON.stringify({ uploaded: true, url: "late" })));
+
+ await expect(uploadPromise).rejects.toBeUndefined();
+ });
+
+ it("rejects when the loader has no file", async () => {
+ await expect(createAdapter(null).upload()).rejects.toBe("Missing file");
+ expect(localUpload).not.toHaveBeenCalled();
+ });
+ });
+
it("rejects when the loader has no file", async () => {
const adapter = createAdapter(null);
diff --git a/packages/ckeditor5/src/plugins/uploadimage.ts b/packages/ckeditor5/src/plugins/uploadimage.ts
index 8e2c0eedc4..8e01f57ad0 100644
--- a/packages/ckeditor5/src/plugins/uploadimage.ts
+++ b/packages/ckeditor5/src/plugins/uploadimage.ts
@@ -39,6 +39,7 @@ class Adapter implements UploadAdapter {
private editor: Editor;
private t: LocaleTranslate;
private xhr?: XMLHttpRequest;
+ private rejectLocal?: () => void;
/**
* Creates a new adapter instance.
@@ -58,6 +59,11 @@ class Adapter implements UploadAdapter {
* @see module:upload/filerepository~Adapter#upload
*/
upload() {
+ const localUpload = glob.getLocalUpload?.();
+ if (localUpload) {
+ return this._uploadLocally(localUpload) as Promise<any>;
+ }
+
return this.loader.file
.then( file => new Promise<File | null>( ( resolve, reject ) => {
this._initRequest().then(() => {
@@ -77,6 +83,45 @@ class Adapter implements UploadAdapter {
if (this.xhr) {
this.xhr.abort();
}
+ this.rejectLocal?.();
+ }
+
+ /**
+ * Hands the file to the host's local server. A raw request with a body cannot reach it: the
+ * mobile app's native request bridges answer GET and HEAD only.
+ */
+ private async _uploadLocally(localUpload: LocalUpload) {
+ const file = await this.loader.file;
+ if (!file) {
+ throw "Missing file";
+ }
+
+ const genericError = this.t('Cannot upload file:') + ` ${file.name}.`;
+ const { url, headers } = await this._getRequestTarget();
+ const aborted = new Promise<never>((_, reject) => {
+ this.rejectLocal = () => reject();
+ });
+
+ const upload = (async () => {
+ let resp: Response;
+ try {
+ resp = await localUpload(url, { method: 'POST', headers, fields: {}, file });
+ } catch {
+ throw genericError;
+ }
+
+ const response = await resp.json().catch(() => null);
+ if (!response?.uploaded) {
+ throw `${genericError} ${describeUploadFailure(this.t, resp.status, response)}`.trim();
+ }
+ return { default: response.url };
+ })();
+
+ try {
+ return await Promise.race([ upload, aborted ]);
+ } finally {
+ this.rejectLocal = undefined;
+ }
}
/**
@@ -85,21 +130,9 @@ class Adapter implements UploadAdapter {
* @private
*/
_initRequest() {
- const domRoot = this.editor.editing.view.getDomRoot();
- const component = glob.getComponentByEl<EditorComponent>( domRoot );
-
- // Sent as a change of the host, which then does not redraw the links and embeds it shows.
- return glob.getHeaders({ "trilium-component-id": component?.componentId }).then(headers => {
+ return this._getRequestTarget().then(({ url, headers }) => {
const xhr = this.xhr = new XMLHttpRequest();
- // The note the editor holds, which is not the active tab's in a split or the quick edit
- // popup. A host that does not say falls back to the active tab's.
- const noteId = component?.getNoteId?.() ?? glob.getActiveContextNote().noteId;
- const query = isUploadAsLink(this.loader) ? "?link=true" : "";
-
- // this must be a relative path
- const url = `api/notes/${noteId}/attachments/upload${query}`;
-
xhr.open('POST', url, true);
xhr.responseType = 'json';
@@ -109,6 +142,23 @@ class Adapter implements UploadAdapter {
});
}
+ /** The relative URL of the upload route for the editor's note, and the request headers. */
+ private async _getRequestTarget() {
+ const domRoot = this.editor.editing.view.getDomRoot();
+ const component = glob.getComponentByEl<EditorComponent>( domRoot );
+
+ // Sent as a change of the host, which then does not redraw the links and embeds it shows.
+ const headers = await glob.getHeaders({ "trilium-component-id": component?.componentId });
+
+ // The note the editor holds, which is not the active tab's in a split or the quick edit
+ // popup. A host that does not say falls back to the active tab's.
+ const noteId = component?.getNoteId?.() ?? glob.getActiveContextNote().noteId;
+ const query = isUploadAsLink(this.loader) ? "?link=true" : "";
+
+ // this must be a relative path
+ return { url: `api/notes/${noteId}/attachments/upload${query}`, headers };
+ }
+
/**
* Initializes XMLHttpRequest listeners.
*