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
3 changes: 2 additions & 1 deletion src/lightbox/LightboxActionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/lightbox/downloadImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -36,21 +37,24 @@ const androidEnsureStoragePermission = async (): Promise<void> => {
*
* @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<mixed> => {
export default async (url: string, fileName: string, auth: Auth): Promise<mixed> => {
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);
Expand Down
5 changes: 3 additions & 2 deletions src/lightbox/shareImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down