Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should passes when used properly', () => {
createDownloadResumable('url', 'fileUri', { md5: false });
createDownloadResumable('url', 'fileUri', { cache: false });
createDownloadResumable('url', 'fileUri', {
headers: { 'header-name': 'abc' },
});
createDownloadResumable('url', 'fileUri', {
md5: false,
cache: false,
headers: { 'X-Auth': 'abc' },
});
const controller = createDownloadResumable('url', 'fileUri');
(controller: DownloadResumable);
// $ExpectError: check any
(controller: number);
});
it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
createDownloadResumable();
// $ExpectError: first argument must be a string
createDownloadResumable(69);
// $ExpectError: second argument must be a string
createDownloadResumable('', 69);
// $ExpectError: third argument must be an object
createDownloadResumable('', '', 69);
// $ExpectError: 'abc' is missing in options
createDownloadResumable('', '', { abc: 'test' });
createDownloadResumable('', '', {
// $ExpectError: invalid value
md5: 'need boolean',
});
});
describe('DownloadResumable', () => {
const controller = createDownloadResumable('url', 'fileUri');
it('should work when call .downloadAsync()', () => {
controller.downloadAsync().then(result => {
if (result) {
(result.uri: string);
(result.status: number);
(result.headers: { [string]: string });
(result.md5: ?string);
} else {
(result: void);
}
// $ExpectError: check any
(result: number);
});
});
it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
createDownloadResumable();
// $ExpectError: first argument must be a string
createDownloadResumable(69);
// $ExpectError: second argument must be a string
createDownloadResumable('', 69);
// $ExpectError: third argument must be an object
createDownloadResumable('', '', 69);
// $ExpectError: 'abc' is missing in options
createDownloadResumable('', '', { abc: 'test' });
createDownloadResumable('', '', {
// $ExpectError: invalid value
md5: 'need boolean',
});
});
it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
createDownloadResumable();
// $ExpectError: first argument must be a string
createDownloadResumable(69);
// $ExpectError: second argument must be a string
createDownloadResumable('', 69);
// $ExpectError: third argument must be an object
createDownloadResumable('', '', 69);
// $ExpectError: 'abc' is missing in options
createDownloadResumable('', '', { abc: 'test' });
createDownloadResumable('', '', {
// $ExpectError: invalid value
md5: 'need boolean',
});
});
it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
createDownloadResumable();
// $ExpectError: first argument must be a string
createDownloadResumable(69);
// $ExpectError: second argument must be a string
createDownloadResumable('', 69);
// $ExpectError: third argument must be an object
createDownloadResumable('', '', 69);
// $ExpectError: 'abc' is missing in options
createDownloadResumable('', '', { abc: 'test' });
createDownloadResumable('', '', {
// $ExpectError: invalid value
md5: 'need boolean',
});
});
it('should passes when used properly', () => {
createDownloadResumable('url', 'fileUri', { md5: false });
createDownloadResumable('url', 'fileUri', { cache: false });
createDownloadResumable('url', 'fileUri', {
headers: { 'header-name': 'abc' },
});
createDownloadResumable('url', 'fileUri', {
md5: false,
cache: false,
headers: { 'X-Auth': 'abc' },
});
const controller = createDownloadResumable('url', 'fileUri');
(controller: DownloadResumable);
// $ExpectError: check any
(controller: number);
});
async getPath(): Promise {
const { uri, options } = this;
const { path, exists, tmpPath } = await getCacheEntry(uri);
if (exists) {
return path;
}
const result = await FileSystem.createDownloadResumable(uri, tmpPath, options).downloadAsync();
// If the image download failed, we don't cache anything
if (result && result.status !== 200) {
return undefined;
}
await FileSystem.moveAsync({ from: tmpPath, to: path });
return path;
}
}
async getPath() {
const { uri, options } = this
const { path, exists, tmpPath } = await getCacheEntry(uri)
if (exists) {
return path
}
const result = await FileSystem.createDownloadResumable(
uri,
tmpPath,
options
).downloadAsync()
// If the image download failed, we don't cache anything
if (result && result.status !== 200) {
return undefined
}
await FileSystem.moveAsync({ from: tmpPath, to: path })
return path
}
}
_startDownloading = async () => {
const url = 'http://ipv4.download.thinkbroadband.com/5MB.zip';
const fileUri = FileSystem.documentDirectory + '5MB.zip';
const callback: FileSystem.DownloadProgressCallback = downloadProgress => {
const progress =
downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
this.setState({
downloadProgress: progress,
});
};
const options = { md5: true };
this.download = FileSystem.createDownloadResumable(url, fileUri, options, callback);
try {
await this.download.downloadAsync();
if (this.state.downloadProgress === 1) {
alert('Download complete!');
}
} catch (e) {
console.log(e);
}
}