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
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;
Comment thread
teneeto marked this conversation as resolved.