Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): let the file picker show types Android cannot name

Elian Doran94649a5add5c
diff --git a/apps/client/src/services/utils.spec.ts b/apps/client/src/services/utils.spec.ts
index 95f34d117a..7841e5eeec 100644
--- a/apps/client/src/services/utils.spec.ts
+++ b/apps/client/src/services/utils.spec.ts
@@ -8,6 +8,7 @@ import utils, {
escapeHtml,
escapeQuotes,
escapeRegExp,
+ fileAccept,
formatDateTime,
formatSize,
getErrorMessage,
@@ -305,6 +306,19 @@ describe("platform / device detection", () => {
delete (window as any).Capacitor;
});
+ it("fileAccept widens the filter on Android and iOS only", () => {
+ const uaSpy = vi.spyOn(navigator, "userAgent", "get");
+
+ uaSpy.mockReturnValue("Mozilla/5.0 (X11; Linux x86_64) Chrome/120.0.0.0");
+ expect(fileAccept(".gpx,application/gpx+xml")).toBe(".gpx,application/gpx+xml");
+
+ uaSpy.mockReturnValue("Mozilla/5.0 (Linux; Android 14; SM-S911B) Chrome/120.0.0.0 Mobile");
+ expect(fileAccept(".gpx,application/gpx+xml")).toBe("*/*");
+
+ uaSpy.mockReturnValue("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) Safari/605.1");
+ expect(fileAccept(".enex")).toBe("*/*");
+ });
+
it("isMobile / isDesktop respond to glob.device", () => {
const original = window.glob.device;
window.glob.device = "mobile";
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index c490f8bfa1..846438aedb 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -318,6 +318,16 @@ export function isIOS() {
return /iPad|iPhone|iPod/.test(navigator.userAgent);
}
+/**
+ * The `accept` value for a file input, widened to every file on Android and iOS: both turn `accept`
+ * into a MIME filter (`Intent.EXTRA_MIME_TYPES`, UTIs) whose tables lack `.gpx`, `.enex` and
+ * `.tnbackup`, so a document provider types those files as `application/octet-stream` and the picker
+ * greys out every one of them. A caller offering `.zip`, which both tables name, keeps its filter.
+ */
+export function fileAccept(accept: string) {
+ return /Android|iPad|iPhone|iPod/.test(navigator.userAgent) ? "*/*" : accept;
+}
+
export function isDesktop() {
return (
window.glob?.device === "desktop" ||
diff --git a/apps/client/src/setup_restore.tsx b/apps/client/src/setup_restore.tsx
index 706d52a3da..aa78f7e47f 100644
--- a/apps/client/src/setup_restore.tsx
+++ b/apps/client/src/setup_restore.tsx
@@ -8,7 +8,7 @@ import { ChunkedUploadError, uploadInChunks } from "./services/chunked_upload";
import { describeDatabaseFile, describeDatabaseFormat } from "./services/database_files";
import { t } from "./services/i18n";
import server from "./services/server";
-import { formatSize } from "./services/utils";
+import { fileAccept, formatSize } from "./services/utils";
import Button from "./widgets/react/Button";
import { Card, CardSection } from "./widgets/react/Card";
import DatabaseFileBadges from "./widgets/react/DatabaseFileBadges";
@@ -404,7 +404,7 @@ function PickBackup({ backups, onPick, onUpload, onFailure }: {
<input
ref={fileInput}
type="file"
- accept=".db,.tnbackup"
+ accept={fileAccept(".db,.tnbackup")}
class="restore-file-input"
onChange={(e) => {
const file = (e.target as HTMLInputElement).files?.[0];
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index c40dd7a2b5..7dc64f8689 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -9,6 +9,7 @@ import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
import server from "../../../services/server";
import toast from "../../../services/toast";
+import { fileAccept } from "../../../services/utils";
import { logError } from "../../../services/ws";
import CollectionProperties from "../../note_bars/CollectionProperties";
import { useCollectionTreeDrag, useColorScheme, useEffectiveReadOnly, useNoteBlob, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate } from "../../react/hooks";
@@ -506,7 +507,7 @@ function pickGpxFile(): Promise<File | null> {
return new Promise((resolve) => {
const input = document.createElement("input");
input.type = "file";
- input.accept = ".gpx,application/gpx+xml";
+ input.accept = fileAccept(".gpx,application/gpx+xml");
input.addEventListener("change", () => resolve(input.files?.[0] ?? null));
input.addEventListener("cancel", () => resolve(null));
input.click();
diff --git a/apps/client/src/widgets/dialogs/import/evernote.tsx b/apps/client/src/widgets/dialogs/import/evernote.tsx
index cbbceac4d5..9eebf26bb0 100644
--- a/apps/client/src/widgets/dialogs/import/evernote.tsx
+++ b/apps/client/src/widgets/dialogs/import/evernote.tsx
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import importService from "../../../services/import.js";
+import { fileAccept } from "../../../services/utils.js";
import Button from "../../react/Button.js";
import { Card, CardSection } from "../../react/Card.js";
import FileDropZone from "../../react/FileDropZone.js";
@@ -53,7 +54,7 @@ function EvernotePanel({ parentNoteId, closeDialog, setFooter }: ImportProviderP
<Card heading={t("evernote_import.choose_file")}>
<CardSection>
<OptionsRow name="import-file" description={t("evernote_import.description_long")} stacked>
- <FileDropZone multiple onChange={onChange} accept=".enex" />
+ <FileDropZone multiple onChange={onChange} accept={fileAccept(".enex")} />
</OptionsRow>
<OptionsRowWithToggle
name="shrink-images"