Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'localFileDownload' lib to TypeScript  #27857

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
ref: moved local file download to TS
  • Loading branch information
teneeto committed Sep 20, 2023
commit 88aff92a40ed443aaf7dac1881febecfe7d851f1
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {PermissionsAndroid, Platform} from 'react-native';
import RNFetchBlob from 'react-native-blob-util';
import RNFetchBlob, {FetchBlobResponse} from 'react-native-blob-util';
import * as FileUtils from './FileUtils';

/**
* Android permission check to store images
* @returns {Promise<Boolean>}
*/
function hasAndroidPermission() {
// On Android API Level 33 and above, these permissions do nothing and always return 'never_ask_again'
// More info here: https://stackoverflow.com/a/74296799
if (Platform.Version >= 33) {
if (Number(Platform.Version) >= 33) {
return Promise.resolve(true);
}

Expand All @@ -31,12 +30,9 @@ function hasAndroidPermission() {

/**
* Handling the download
* @param {String} url
* @param {String} fileName
* @returns {Promise<Void>}
*/
function handleDownload(url, fileName) {
return new Promise((resolve) => {
function handleDownload(url: string, fileName: string) {
return new Promise<void>((resolve) => {
const dirs = RNFetchBlob.fs.dirs;

// Android files will download to Download directory
Expand All @@ -46,7 +42,7 @@ function handleDownload(url, fileName) {
const isLocalFile = url.startsWith('file://');

let attachmentPath = isLocalFile ? url : undefined;
let fetchedAttachment = Promise.resolve();
let fetchedAttachment: Promise<FetchBlobResponse | void> = Promise.resolve();

if (!isLocalFile) {
// Fetching the attachment
Expand All @@ -68,7 +64,7 @@ function handleDownload(url, fileName) {
return Promise.reject();
}

if (!isLocalFile) attachmentPath = attachment.path();
if (!isLocalFile) attachmentPath = attachment?.path() ?? '';

return RNFetchBlob.MediaCollection.copyToMediaStore(
{
Expand All @@ -77,11 +73,11 @@ function handleDownload(url, fileName) {
mimeType: null,
},
'Download',
attachmentPath,
String(attachmentPath),
);
})
.then(() => {
RNFetchBlob.fs.unlink(attachmentPath);
RNFetchBlob.fs.unlink(String(attachmentPath));
FileUtils.showSuccessAlert();
})
.catch(() => {
Expand All @@ -93,12 +89,9 @@ function handleDownload(url, fileName) {

/**
* Checks permission and downloads the file for Android
* @param {String} url
* @param {String} fileName
* @returns {Promise<Void>}
*/
export default function fileDownload(url, fileName) {
return new Promise((resolve) => {
export default function fileDownload(url: string, fileName: string) {
return new Promise<void>((resolve) => {
hasAndroidPermission()
.then((hasPermission) => {
if (hasPermission) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import RNFetchBlob from 'react-native-blob-util';
import {CameraRoll} from '@react-native-camera-roll/camera-roll';
import lodashGet from 'lodash/get';
import * as FileUtils from './FileUtils';
import CONST from '../../CONST';

/**
* Downloads the file to Documents section in iOS
* @param {String} fileUrl
* @param {String} fileName
* @returns {Promise}
*/
function downloadFile(fileUrl, fileName) {
function downloadFile(fileUrl: string, fileName: string) {
const dirs = RNFetchBlob.fs.dirs;

// The iOS files will download to documents directory
Expand All @@ -31,29 +27,25 @@ function downloadFile(fileUrl, fileName) {

/**
* Download the image to photo lib in iOS
* @param {String} fileUrl
* @param {String} fileName
* @returns {String} URI
* @returns URI
*/
function downloadImage(fileUrl) {
function downloadImage(fileUrl: string) {
return CameraRoll.save(fileUrl);
}

/**
* Download the video to photo lib in iOS
* @param {String} fileUrl
* @param {String} fileName
* @returns {String} URI
* @returns URI
*/
function downloadVideo(fileUrl, fileName) {
function downloadVideo(fileUrl: string, fileName: string): Promise<string | null> {
return new Promise((resolve, reject) => {
let documentPathUri = null;
let cameraRollUri = null;
let documentPathUri: string;
let cameraRollUri: string;

// Because CameraRoll doesn't allow direct downloads of video with remote URIs, we first download as documents, then copy to photo lib and unlink the original file.
downloadFile(fileUrl, fileName)
.then((attachment) => {
documentPathUri = lodashGet(attachment, 'data');
documentPathUri = attachment?.data;
return CameraRoll.save(documentPathUri);
})
.then((attachment) => {
Expand All @@ -67,19 +59,16 @@ function downloadVideo(fileUrl, fileName) {

/**
* Download the file based on type(image, video, other file types)for iOS
* @param {String} fileUrl
* @param {String} fileName
* @returns {Promise<Void>}
*/
export default function fileDownload(fileUrl, fileName) {
return new Promise((resolve) => {
export default function fileDownload(fileUrl: string, fileName: string) {
return new Promise<void>((resolve) => {
let fileDownloadPromise = null;
const fileType = FileUtils.getFileType(fileUrl);
const attachmentName = FileUtils.appendTimeToFileName(fileName) || FileUtils.getAttachmentName(fileUrl);

switch (fileType) {
case CONST.ATTACHMENT_FILE_TYPE.IMAGE:
fileDownloadPromise = downloadImage(fileUrl, attachmentName);
fileDownloadPromise = downloadImage(fileUrl);
break;
case CONST.ATTACHMENT_FILE_TYPE.VIDEO:
fileDownloadPromise = downloadVideo(fileUrl, attachmentName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import * as Link from '../actions/Link';

/**
* Downloading attachment in web, desktop
* @param {String} url
* @param {String} fileName
* @returns {Promise}
*/
export default function fileDownload(url, fileName) {
return new Promise((resolve) => {
export default function fileDownload(url: string, fileName: string) {
return new Promise<void>((resolve) => {
fetch(url)
.then((response) => response.blob())
.then((blob) => {
Expand All @@ -34,7 +31,7 @@ export default function fileDownload(url, fileName) {

// Clean up and remove the link
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
link.parentNode?.removeChild(link);
return resolve();
})
.catch(() => {
Expand Down