diff --git a/apps/readest-app/src/__tests__/helpers/open-with.test.ts b/apps/readest-app/src/__tests__/helpers/open-with.test.ts index 5111019c1..e9f675a26 100644 --- a/apps/readest-app/src/__tests__/helpers/open-with.test.ts +++ b/apps/readest-app/src/__tests__/helpers/open-with.test.ts @@ -180,6 +180,18 @@ describe('parseOpenWithFiles', () => { expect(result).toEqual(['/path/to/book.epub']); }); + test.each([ + 'A%23B.epub', + '100%25.epub', + 'Report%2520Final.epub', + '%E4%B8%89%E4%BD%93%20book.epub', + ])('preserves the raw iOS file URL for %s', async (basename) => { + const url = `file:///path/to/${basename}`; + mockGetCurrent.mockResolvedValue([url]); + const result = await parseOpenWithFiles({ isIOSApp: true } as never); + expect(result).toEqual([url]); + }); + test('preserves file:// prefix for iOS', async () => { const mockAppService = { isIOSApp: true } as MockAppService; mockGetCurrent.mockResolvedValue(['file:///path/to/book.epub']); diff --git a/apps/readest-app/src/__tests__/hooks/useOpenWithBooks.test.tsx b/apps/readest-app/src/__tests__/hooks/useOpenWithBooks.test.tsx new file mode 100644 index 000000000..981cc2284 --- /dev/null +++ b/apps/readest-app/src/__tests__/hooks/useOpenWithBooks.test.tsx @@ -0,0 +1,56 @@ +import { afterEach, describe, expect, test, vi } from 'vitest'; +import { act, cleanup, renderHook, waitFor } from '@testing-library/react'; + +const h = vi.hoisted(() => ({ + appService: { + isIOSApp: true, + hasWindow: false, + loadSettings: vi.fn(async () => ({ autoImportBooksOnOpen: true })), + }, + navigateToLibrary: vi.fn(), + setCheckOpenWithBooks: vi.fn(), +})); + +vi.mock('next/navigation', () => ({ useRouter: () => ({}) })); +vi.mock('@tauri-apps/api/window', () => ({ + getAllWindows: vi.fn(), + getCurrentWindow: vi.fn(), +})); +vi.mock('@/context/EnvContext', () => ({ useEnv: () => ({ appService: h.appService }) })); +vi.mock('@/services/environment', () => ({ isTauriAppPlatform: () => true })); +vi.mock('@/store/libraryStore', () => ({ + useLibraryStore: () => ({ setCheckOpenWithBooks: h.setCheckOpenWithBooks }), +})); +vi.mock('@/utils/nav', () => ({ + navigateToLibrary: h.navigateToLibrary, + navigateToReader: vi.fn(), + showLibraryWindow: vi.fn(), +})); +vi.mock('@/utils/md5', () => ({ partialMD5: vi.fn() })); + +import { useOpenWithBooks } from '@/hooks/useOpenWithBooks'; +import { eventDispatcher } from '@/utils/event'; + +afterEach(() => { + cleanup(); + delete window.OPEN_WITH_FILES; + vi.clearAllMocks(); +}); + +describe('useOpenWithBooks iOS file URL ingress', () => { + test.each([ + 'A%23B.epub', + '100%25.epub', + 'Report%2520Final.epub', + '%E4%B8%89%E4%BD%93%20book.epub', + ])('preserves the raw iOS URL for %s', async (basename) => { + const uri = `file:///private/var/Inbox/${basename}`; + renderHook(() => useOpenWithBooks()); + act(() => { + eventDispatcher.dispatch('app-incoming-url', { urls: [uri] }); + }); + await waitFor(() => expect(h.navigateToLibrary).toHaveBeenCalledTimes(1)); + expect(window.OPEN_WITH_FILES).toEqual([uri]); + expect(h.setCheckOpenWithBooks).toHaveBeenCalledWith(true); + }); +}); diff --git a/apps/readest-app/src/__tests__/services/native-app-service-share.test.ts b/apps/readest-app/src/__tests__/services/native-app-service-share.test.ts index 273d6b642..c88c7694e 100644 --- a/apps/readest-app/src/__tests__/services/native-app-service-share.test.ts +++ b/apps/readest-app/src/__tests__/services/native-app-service-share.test.ts @@ -123,6 +123,22 @@ describe('NativeAppService.saveFile share gating', () => { expect(file.name).toBe('Original Manual.pdf'); }); + test.each([ + ['A%23B.epub', 'A#B.epub'], + ['100%25.epub', '100%.epub'], + ['Report%2520Final.epub', 'Report%20Final.epub'], + ['A%3FB.epub', 'A?B.epub'], + ['%E4%B8%89%E4%BD%93%20book.epub', '三体 book.epub'], + ])('decodes an iOS import basename exactly once: %s', async (encodedName, expected) => { + const service = await loadServiceWithOS('ios'); + const uri = `file:///private/var/Inbox/${encodedName}`; + const file = await service.openFile(uri, 'None'); + expect(file.name).toBe(expected); + const request = copyURIToPathMock.mock.calls[0]![0] as { uri: string; dst: string }; + expect(request.uri).toBe(uri); + expect(request.dst.endsWith(`-${expected}`)).toBe(true); + }); + test.each([ ['https://example.com/manual.pdf#zoom=1.25', undefined, 'manual.pdf'], ['https://example.com/C%2B%2B.pdf?type=application/pdf', undefined, 'C++.pdf'], diff --git a/apps/readest-app/src/helpers/openWith.ts b/apps/readest-app/src/helpers/openWith.ts index 7f0e2b8f4..8e594237a 100644 --- a/apps/readest-app/src/helpers/openWith.ts +++ b/apps/readest-app/src/helpers/openWith.ts @@ -54,7 +54,7 @@ const parseIntentOpenWithFiles = async (appService: AppService | null) => { .map((url) => { if (url.startsWith('file://')) { if (appService?.isIOSApp) { - return decodeURI(url); + return url; } else { return decodeURI(url.replace('file://', '')); } diff --git a/apps/readest-app/src/hooks/useOpenWithBooks.ts b/apps/readest-app/src/hooks/useOpenWithBooks.ts index c2b028ebf..d859d15f6 100644 --- a/apps/readest-app/src/hooks/useOpenWithBooks.ts +++ b/apps/readest-app/src/hooks/useOpenWithBooks.ts @@ -62,7 +62,7 @@ export function useOpenWithBooks() { const filePaths: string[] = []; for (let url of urls) { if (url.startsWith('file://')) { - url = appService?.isIOSApp ? decodeURI(url) : decodeURI(url.replace('file://', '')); + url = appService?.isIOSApp ? url : decodeURI(url.replace('file://', '')); } if (!/^(https?:|data:|blob:|readest:)/i.test(url)) { filePaths.push(url); diff --git a/apps/readest-app/src/services/nativeAppService.ts b/apps/readest-app/src/services/nativeAppService.ts index afd78f6d3..2d5e27c60 100644 --- a/apps/readest-app/src/services/nativeAppService.ts +++ b/apps/readest-app/src/services/nativeAppService.ts @@ -318,7 +318,8 @@ export const nativeFileSystem: FileSystem = { return await new RemoteFile(path, fname, '', Date.now(), fetcher ?? tauriFetch).open(); } if (isContentURI(path) || (isFileURI(path) && OS_TYPE === 'ios')) { - const fname = safeDecodePath(await basename(path)); + const isIOSFileURI = isFileURI(path) && OS_TYPE === 'ios'; + const fname = isIOSFileURI ? getURLFilename(path) : safeDecodePath(await basename(path)); if (path.includes('com.android.externalstorage')) { // If the URI is from shared internal storage (like /storage/emulated/0), // we can access it directly using the path β€” no need to copy. @@ -328,7 +329,8 @@ export const nativeFileSystem: FileSystem = { // or file:// URIs is security scoped resource in iOS (e.g. from Files app), // we cannot access the file directly β€” so we copy it to a temporary cache location. const prefix = await this.getPrefix('Cache'); - const dst = await join(prefix, `${crypto.randomUUID()}-${decodeURIComponent(fname)}`); + const copyName = isIOSFileURI ? fname : decodeURIComponent(fname); + const dst = await join(prefix, `${crypto.randomUUID()}-${copyName}`); const res = await copyURIToPath({ uri: path, dst }); if (!res.success) { console.error('Failed to open file:', res);