@pf126/file-downloader
v1.0.0-dev
Published
Library for downloading files
Downloads
32
Readme
Downloader
Usage
const { DownloaderClient } = require('@pf126/downloader');
const crypto = require('crypto');
const path = require('path');
const client = new DownloaderClient({
requester: {
// Request timeout in millisecond
timeout: 30000,
// Drive config when you want to use google-api for downloading file from google drive
drive: {
version: 'v3',
auth: {
scopes: ['https://www.googleapis.com/auth/drive.readonly'],
keyFile: path.join(process.cwd(), '.temp', 'google-credentials.json'),
},
},
// Proxy urls are used for downloading
proxy: ['http://13.52.68.123:55333', 'http://13.52.68.122:55333'],
},
retry: {
// Delay before next retry in millisecond
delay: 10000,
// Max retry
attempts: 10,
},
// Directory where downloaded file is stored
directory: path.join(process.cwd(), '.temp'),
// Filename generator
filename: (link) => {
return crypto.createHash('md5').update(link).digest('hex').toString();
},
// Customize output
transformer: (output) => {
return output;
},
});
// Emit before sending get http request
client.on('requesting', (link, retry) => {
console.log('[DownloaderClient][Fetching]: Retry=%d | Link=%s', retry, link);
});
// Emit before writing file into storage
client.on('writing', (link, retry, filepath) => {
console.log('[DownloaderClient][Writing]: Retry=%d | Link=%s | Filepath=%s', retry, link, filepath);
});
// Emit when download failed and could be retried
client.on('failed', (error, link, retry) => {
console.log('[DownloaderClient][Failed]: Retry=%d | Link=%s', retry, link, error);
});
// Emit before transforming output
client.on('transforming', (output) => {
console.log('[DownloaderClient][Transforming]:', output);
});
// Emit when file is successfully written into storage
client.on('success', (output) => {
console.log('[DownloaderClient][Success]:', output);
});
// Emit when can not retry anymore
client.on('error', (error) => {
console.log('[DownloaderClient][Error]:', error);
});
const link = 'https://lmdesign.sfo2.digitaloceanspaces.com/3098553679_Jen_reviews_2_Kanoe_Montaño_png';
client
.download(link)
.then((output) => console.log(output))
.catch((error) => console.log(error));