Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/libs/actions/Attachment/index.native.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getImageCacheFileExtension} from '@libs/AttachmentUtils';
import fileURIToPath from '@libs/fileURIToPath';
import Log from '@libs/Log';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 25 additions & 2 deletions tests/actions/AttachmentTest.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) => {
Expand Down Expand Up @@ -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';
Expand Down
Loading