diff --git a/src/lightbox/LightboxActionSheet.js b/src/lightbox/LightboxActionSheet.js index 4f41cea00dc..e634a4b16e7 100644 --- a/src/lightbox/LightboxActionSheet.js +++ b/src/lightbox/LightboxActionSheet.js @@ -42,8 +42,9 @@ const tryToDownloadImage = async ({ src, auth }: DownloadImageType) => { return; } + const fileName = src.split('/').pop(); try { - await downloadImage(tempUrl, auth); + await downloadImage(tempUrl, fileName, auth); showToast('Download complete'); } catch (error) { showToast(error.message); diff --git a/src/lightbox/downloadImage.js b/src/lightbox/downloadImage.js index 178e18e8508..1e1b54fa142 100644 --- a/src/lightbox/downloadImage.js +++ b/src/lightbox/downloadImage.js @@ -3,6 +3,7 @@ import { CameraRoll, Platform, PermissionsAndroid } from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; import type { Auth } from '../api/transportTypes'; +import { getMimeTypeFromFileExtension } from '../utils/url'; /** * Request permission WRITE_EXTERNAL_STORAGE, or throw if can't get it. @@ -36,21 +37,24 @@ const androidEnsureStoragePermission = async (): Promise => { * * @param url A URL to the image. Should be a valid temporary URL generated * using `getTemporaryFileUrl`. + * @param fileName Name of the file to be downloaded. Should include the + * extension. * @param auth Authentication info for the current user. */ -export default async (url: string, auth: Auth): Promise => { +export default async (url: string, fileName: string, auth: Auth): Promise => { if (Platform.OS === 'ios') { return CameraRoll.saveToCameraRoll(url); } // Platform.OS === 'android' + const mime = getMimeTypeFromFileExtension(fileName.split('.').pop()); await androidEnsureStoragePermission(); return RNFetchBlob.config({ addAndroidDownloads: { - path: `${RNFetchBlob.fs.dirs.DownloadDir}/${url.split('/').pop()}`, + path: `${RNFetchBlob.fs.dirs.DownloadDir}/${fileName}`, useDownloadManager: true, - mime: 'text/plain', // Android DownloadManager fails if the url is missing a file extension - title: url.split('/').pop(), + mime, // Android DownloadManager fails if the url is missing a file extension + title: fileName, notification: true, }, }).fetch('GET', url); diff --git a/src/lightbox/shareImage.js b/src/lightbox/shareImage.js index 7975469f197..b67d5d79f55 100644 --- a/src/lightbox/shareImage.js +++ b/src/lightbox/shareImage.js @@ -18,12 +18,13 @@ export default async (url: string, auth: Auth) => { return; } + const fileName = url.split('/').pop(); if (Platform.OS === 'android') { - const res: $FlowFixMe = await downloadImage(tempUrl, auth); + const res: $FlowFixMe = await downloadImage(tempUrl, fileName, auth); await ShareImageAndroid.shareImage(res.path()); } else { try { - const uri = await downloadImage(tempUrl, auth); + const uri = await downloadImage(tempUrl, fileName, auth); try { await Share.share({ url: uri, message: url }); } catch (error) {