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
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ edgeToEdgeEnabled=true
# AGP 9 defaults, turned off as in the RN 0.88 template:
# - builtInKotlin: many libraries (e.g. react-native-webview, react-native-vision-camera, @rnmapbox/maps, the nitro modules,
# and our own modules in modules/) still apply the kotlin-android plugin unconditionally, which fails with built-in Kotlin.
# - newDsl: app/build.gradle, Mobile-Expensify and react-native-blob-util still use the legacy
# - newDsl: app/build.gradle and Mobile-Expensify still use the legacy
# applicationVariants/libraryVariants API, which the new DSL removes.
android.builtInKotlin=false
android.newDsl=false
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ PODS:
- ReactCommon/turbomodule/core
- ReactNativeDependencies
- Yoga
- react-native-blob-util (0.24.12):
- react-native-blob-util (1.1.1):
- hermes-engine
- RCTRequired
- RCTTypeSafety
Expand Down Expand Up @@ -4213,7 +4213,7 @@ SPEC CHECKSUMS:
react-native-advanced-input-mask: 7569c46f445c0d9194b3cfd0135c004a3e9227bc
react-native-airship: 2e319f13ddb3dd5a4966beaa97c9aea08cfb566c
react-native-app-logs: 022478b8b3172ec1bbe1f72a625cd6a7b6109762
react-native-blob-util: 13d949591d518214a68e3daa57045c499a0dbd2a
react-native-blob-util: c14f542c2965a8d33a8feac2bed2d29e11be4971
react-native-cameraroll: 2b19ddd5ff316c20d4ed9533b077318318f8448c
react-native-config: eb6701f1dedce0fdd239789584c4194203787b76
react-native-document-picker: 2d4358affe7cd049ad8d042958d27d9ff781366f
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"react-native": "0.88.0",
"react-native-advanced-input-mask": "1.4.6",
"react-native-app-logs": "0.3.1",
"react-native-blob-util": "0.24.12",
"react-native-blob-util": "1.1.1",
"react-native-collapsible": "^1.6.2",
"react-native-config": "1.5.3",
"react-native-device-info": "10.3.1",
Expand Down
12 changes: 7 additions & 5 deletions src/libs/fileDownload/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ function handleDownload(translate: LocalizedTranslate, url: string, fileName?: s
fetchedAttachment = RNFetchBlob.config({
fileCache: true,
path: `${path}/${attachmentName}`,
addAndroidDownloads: {
useDownloadManager: true,
notification: false,
path: `${path}/Expensify/${attachmentName}`,
android: {
downloadManager: {
useDownloadManager: true,
notification: false,
path: `${path}/Expensify/${attachmentName}`,
},
},
}).fetch('GET', url);
}
Expand All @@ -55,7 +57,7 @@ function handleDownload(translate: LocalizedTranslate, url: string, fileName?: s
}

if (!isLocalFile) {
attachmentPath = (attachment as FetchBlobResponse).path();
attachmentPath = (attachment as FetchBlobResponse).path() ?? undefined;
}

return saveLocalFileToGallery(isLocalFile ? url : (attachmentPath ?? ''), attachmentName);
Expand Down
23 changes: 14 additions & 9 deletions src/libs/fileDownload/saveLocalFileToGallery.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import {getFileName} from './FileUtils';
/** Copies into Download/Expensify with no alert. Name is used as given; callers append a timestamp if they need collision-free names. */
function saveLocalFileToGallery(localPath: string, fileName?: string, mimeType?: string): Promise<void> {
const sourcePath = fileURIToPath(localPath);
return RNFetchBlob.MediaCollection.copyToMediaStore(
{
name: fileName ?? getFileName(localPath),
parentFolder: 'Expensify',
mimeType: mimeType ?? null,
},
'Download',
sourcePath,
).then(() => undefined);
return RNFetchBlob.media
.copyToMediaStore(
{
name: fileName ?? getFileName(localPath),
parentFolder: 'Expensify',
// The native side rejects a descriptor without a MIME type key, and takes null as "let the Media Store work it out".
// An undefined value is dropped on the way to native code, so null is sent although the library's types leave it out.
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- the library's types don't allow the null its native side accepts
mime: mimeType ?? (null as unknown as undefined),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mime: mimeType ?? (null as unknown as undefined),
mime: mimeType ?? getMimeType(fileExtension),

@m-bert can we use this?

    const name = fileName ?? getFileName(localPath);
    const {fileExtension} = splitExtensionFromFileName(name);
    ...
    mime: mimeType ?? getMimeType(fileExtension),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep null here. getMimeType only knows txt, csv, pdf, html, xml and zip, and returns application/octet-stream for everything else, so we would pass that type for images and videos, which is most of what is saved through this function.

With null, Android works the type out from the file name: in my tests the saved files were recorded as image/png, video/mp4 and application/pdf. That is also what main does today (mimeType: mimeType ?? null), so this PR doesn't change the behaviour. The assertion is only there because the library's types don't allow null.

Let me know if you agree.

},
'Download',
sourcePath,
)
.then(() => undefined);
}

export default saveLocalFileToGallery;
4 changes: 2 additions & 2 deletions src/libs/localFileDownload/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const localFileDownload: LocalFileDownload = (fileName, textContent, translate,
const {fileExtension} = splitExtensionFromFileName(newFileName);
const mimeType = getMimeType(fileExtension);
const tryMediaStore = (name: string) =>
RNFetchBlob.MediaCollection.copyToMediaStore(
RNFetchBlob.media.copyToMediaStore(
{
name,
parentFolder: '', // subdirectory in the Media Store, empty goes to 'Downloads'
mimeType,
mime: mimeType,
},
'Download',
path,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/getMimeTypeFromFileHeaderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('getMimeTypeFromFileHeader (iOS)', () => {

it('copies the first bytes into a temp file, reads them back, and removes the temp file', async () => {
// Given an extensionless QuickTime movie picked from a file:// uri
mockSlice.mockImplementation((src, dest) => Promise.resolve(dest));
mockSlice.mockResolvedValue();
mockReadFile.mockResolvedValue(Buffer.from(ftypHeader('qt ')).toString('base64'));

// When its type is detected
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/saveLocalFileToGalleryAndroidTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import saveLocalFileToGallery from '@libs/fileDownload/saveLocalFileToGallery.android';

const mockCopyToMediaStore = jest.fn<Promise<string>, unknown[]>(() => Promise.resolve('content://media/external/downloads/1'));

jest.mock('react-native-blob-util', () => ({
__esModule: true,
default: {
media: {copyToMediaStore: (...args: unknown[]) => mockCopyToMediaStore(...args)},
},
}));

describe('saveLocalFileToGallery (Android)', () => {
beforeEach(() => {
mockCopyToMediaStore.mockClear();
});

it('passes the given MIME type to the Media Store', async () => {
// Given a local receipt whose MIME type is known
// When saving it to the gallery
await saveLocalFileToGallery('file:///data/cache/receipt.jpg', 'receipt.jpg', 'image/jpeg');

// Then the Media Store gets that MIME type, so the file is recorded with the type the caller knows
expect(mockCopyToMediaStore).toHaveBeenCalledWith({name: 'receipt.jpg', parentFolder: 'Expensify', mime: 'image/jpeg'}, 'Download', '/data/cache/receipt.jpg');
});

it('sends a null MIME type when none is given', async () => {
// Given a downloaded attachment whose MIME type is unknown
// When saving it to the gallery
await saveLocalFileToGallery('file:///data/cache/photo.png', 'photo.png');

// Then the MIME type is null, not missing: the native side rejects a descriptor without it,
// and takes null as "work the type out from the file name"
expect(mockCopyToMediaStore).toHaveBeenCalledWith({name: 'photo.png', parentFolder: 'Expensify', mime: null}, 'Download', '/data/cache/photo.png');
});
});
Loading