diff --git a/src/libs/actions/Attachment/index.native.ts b/src/libs/actions/Attachment/index.native.ts index 624bec0ef605..078da0eb9dfb 100644 --- a/src/libs/actions/Attachment/index.native.ts +++ b/src/libs/actions/Attachment/index.native.ts @@ -1,4 +1,5 @@ import {getImageCacheFileExtension} from '@libs/AttachmentUtils'; +import fileURIToPath from '@libs/fileURIToPath'; import Log from '@libs/Log'; import CONST from '@src/CONST'; @@ -26,7 +27,9 @@ async function cacheAttachment({attachmentID, uri, mimeType}: CacheAttachmentPro try { // The OS can purge Caches wholesale, so the directory may need recreating await RNFS.mkdir(ATTACHMENT_DIR); - await RNFS.copyFile(uri, destPath); + const decodedPath = fileURIToPath(uri); + const sourcePath = (await RNFS.exists(decodedPath)) ? decodedPath : uri.slice('file://'.length); + await RNFS.copyFile(sourcePath, destPath); await Onyx.set(`${ONYXKEYS.COLLECTION.ATTACHMENT}${attachmentID}`, { attachmentID, source: destPath, diff --git a/tests/actions/AttachmentTest.ts b/tests/actions/AttachmentTest.ts index 7f386edcd0b9..e7ee663ea8f4 100644 --- a/tests/actions/AttachmentTest.ts +++ b/tests/actions/AttachmentTest.ts @@ -1,6 +1,6 @@ import {rand64} from '@libs/NumberUtils'; -import {clearCachedAttachments, getCachedAttachment} from '@userActions/Attachment'; +import {cacheAttachment, clearCachedAttachments, getCachedAttachment} from '@userActions/Attachment'; import {addAttachmentWithComment, addComment, deleteReportComment} from '@userActions/Report'; import CONST from '@src/CONST'; @@ -23,7 +23,7 @@ jest.mock('react-native-fs', () => ({ mkdir: jest.fn(() => Promise.resolve()), })); -const mockRNFS: {exists: jest.Mock} = jest.requireMock('react-native-fs'); +const mockRNFS: {copyFile: jest.Mock; exists: jest.Mock} = jest.requireMock('react-native-fs'); jest.mock('react-native-blob-util', () => ({ config: jest.fn((data: {path?: string} | undefined) => { @@ -111,6 +111,29 @@ describe('AttachmentStorage', () => { source: `/mock/caches/attachments/${attachmentID}.jpg`, }); }); + it('should decode an encoded file URI before copying a shared attachment', async () => { + const attachmentID = 'encoded-attachment'; + + await cacheAttachment({ + attachmentID, + uri: 'file:///mock/sharedFiles/test%20image%20%231.jpg', + mimeType: 'image/jpeg', + }); + + expect(mockRNFS.copyFile).toHaveBeenCalledWith('/mock/sharedFiles/test image #1.jpg', `/mock/caches/attachments/${attachmentID}.jpg`); + }); + it('should fall back to the raw file URI path for legacy filenames', async () => { + const attachmentID = 'legacy-attachment'; + mockRNFS.exists.mockResolvedValueOnce(false); + + await cacheAttachment({ + attachmentID, + uri: 'file:///mock/sharedFiles/report%23.jpg', + mimeType: 'image/jpeg', + }); + + expect(mockRNFS.copyFile).toHaveBeenCalledWith('/mock/sharedFiles/report%23.jpg', `/mock/caches/attachments/${attachmentID}.jpg`); + }); it('should fall back to the current source when the cached file was purged from disk', async () => { // Given a cached attachment whose file no longer exists (the OS can purge Caches) const attachmentID = 'purged-attachment';