Skip to content

Commit 1223fdc

Browse files
committed
lightbox: Download images with correct names and MIME types.
Currently, the `downloadImage` function saves images with the wrong names, extensions and MIME types. This commit fixes that by introducing a new argument for the function - `fileName`, and by determining the MIME type from the file extension. Fixes: #4138 Fixes: #4137
1 parent ed645fe commit 1223fdc

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/lightbox/LightboxActionSheet.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ const tryToDownloadImage = async ({ src, auth }: DownloadImageType) => {
4242
return;
4343
}
4444

45+
const fileName = src.split('/').pop();
4546
try {
46-
await downloadImage(tempUrl, auth);
47+
await downloadImage(tempUrl, fileName, auth);
4748
showToast('Download complete');
4849
} catch (error) {
4950
showToast(error.message);

src/lightbox/downloadImage.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CameraRoll, Platform, PermissionsAndroid } from 'react-native';
33
import RNFetchBlob from 'rn-fetch-blob';
44

55
import type { Auth } from '../api/transportTypes';
6+
import { getMimeTypeFromFileExtension } from '../utils/url';
67

78
/**
89
* Request permission WRITE_EXTERNAL_STORAGE, or throw if can't get it.
@@ -36,21 +37,24 @@ const androidEnsureStoragePermission = async (): Promise<void> => {
3637
*
3738
* @param url A URL to the image. Should be a valid temporary URL generated
3839
* using `getTemporaryFileUrl`.
40+
* @param fileName Name of the file to be downloaded. Should include the
41+
* extension.
3942
* @param auth Authentication info for the current user.
4043
*/
41-
export default async (url: string, auth: Auth): Promise<mixed> => {
44+
export default async (url: string, fileName: string, auth: Auth): Promise<mixed> => {
4245
if (Platform.OS === 'ios') {
4346
return CameraRoll.saveToCameraRoll(url);
4447
}
4548

4649
// Platform.OS === 'android'
50+
const mime = getMimeTypeFromFileExtension(fileName.split('.').pop());
4751
await androidEnsureStoragePermission();
4852
return RNFetchBlob.config({
4953
addAndroidDownloads: {
50-
path: `${RNFetchBlob.fs.dirs.DownloadDir}/${url.split('/').pop()}`,
54+
path: `${RNFetchBlob.fs.dirs.DownloadDir}/${fileName}`,
5155
useDownloadManager: true,
52-
mime: 'text/plain', // Android DownloadManager fails if the url is missing a file extension
53-
title: url.split('/').pop(),
56+
mime, // Android DownloadManager fails if the url is missing a file extension
57+
title: fileName,
5458
notification: true,
5559
},
5660
}).fetch('GET', url);

src/lightbox/shareImage.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export default async (url: string, auth: Auth) => {
1818
return;
1919
}
2020

21+
const fileName = url.split('/').pop();
2122
if (Platform.OS === 'android') {
22-
const res: $FlowFixMe = await downloadImage(tempUrl, auth);
23+
const res: $FlowFixMe = await downloadImage(tempUrl, fileName, auth);
2324
await ShareImageAndroid.shareImage(res.path());
2425
} else {
2526
try {
26-
const uri = await downloadImage(tempUrl, auth);
27+
const uri = await downloadImage(tempUrl, fileName, auth);
2728
try {
2829
await Share.share({ url: uri, message: url });
2930
} catch (error) {

0 commit comments

Comments
 (0)