Skip to content

Commit

Permalink
Merge pull request #27857 from teneeto/ts-migration/local-file-download
Browse files Browse the repository at this point in the history
[No QA] [TS migration] Migrate 'localFileDownload' lib to TypeScript 
  • Loading branch information
Beamanator authored Oct 16, 2023
2 parents 58481fb + 497e6c7 commit 7791d32
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import RNFetchBlob from 'react-native-blob-util';
import * as FileUtils from '../fileDownload/FileUtils';
import LocalFileDownload from './types';

/**
* Writes a local file to the app's internal directory with the given fileName
* and textContent, so we're able to copy it to the Android public download dir.
* After the file is copied, it is removed from the internal dir.
*
* @param {String} fileName
* @param {String} textContent
*/
export default function localFileDownload(fileName, textContent) {
const localFileDownload: LocalFileDownload = (fileName, textContent) => {
const newFileName = FileUtils.appendTimeToFileName(fileName);
const dir = RNFetchBlob.fs.dirs.DocumentDir;
const path = `${dir}/${newFileName}.txt`;
Expand All @@ -34,4 +32,6 @@ export default function localFileDownload(fileName, textContent) {
RNFetchBlob.fs.unlink(path);
});
});
}
};

export default localFileDownload;
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {Share} from 'react-native';
import RNFetchBlob from 'react-native-blob-util';
import * as FileUtils from '../fileDownload/FileUtils';
import LocalFileDownload from './types';

/**
* Writes a local file to the app's internal directory with the given fileName
* and textContent, so we're able to share it using iOS' share API.
* After the file is shared, it is removed from the internal dir.
*
* @param {String} fileName
* @param {String} textContent
*/
export default function localFileDownload(fileName, textContent) {
const localFileDownload: LocalFileDownload = (fileName, textContent) => {
const newFileName = FileUtils.appendTimeToFileName(fileName);
const dir = RNFetchBlob.fs.dirs.DocumentDir;
const path = `${dir}/${newFileName}.txt`;
Expand All @@ -20,4 +18,6 @@ export default function localFileDownload(fileName, textContent) {
RNFetchBlob.fs.unlink(path);
});
});
}
};

export default localFileDownload;
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as FileUtils from '../fileDownload/FileUtils';
import LocalFileDownload from './types';

/**
* Creates a Blob with the given fileName and textContent, then dynamically
* creates a temporary anchor, just to programmatically click it, so the file
* is downloaded by the browser.
*
* @param {String} fileName
* @param {String} textContent
*/
export default function localFileDownload(fileName, textContent) {
const localFileDownload: LocalFileDownload = (fileName, textContent) => {
const blob = new Blob([textContent], {type: 'text/plain'});
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = FileUtils.appendTimeToFileName(`${fileName}.txt`);
link.href = url;
link.click();
}
};

export default localFileDownload;
3 changes: 3 additions & 0 deletions src/libs/localFileDownload/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type LocalFileDownload = (fileName: string, textContent: string) => void;

export default LocalFileDownload;

0 comments on commit 7791d32

Please sign in to comment.