All files / src/lib downoader.js

27.27% Statements 3/11
0% Branches 0/2
33.33% Functions 1/3
27.27% Lines 3/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31            1x     1x 2x                                        
import url from 'url';
import path from 'path';
import debug from 'debug';
import fs from 'mz/fs';
import axios from './axiosFix';
 
const log = debug('page-loader');
 
 
const downloadFiles = async (links, address, dir) => {
  await Promise.all(links.map(async (link) => {
    log(`File ${link} is ready`);
    const { hostname } = url.parse(address);
    const name = (path.dirname(link) + path.basename(link)).replace(/\//gi, '-');
    try {
      const file = await axios.get(link, {
        baseURL: `http://${hostname}`,
        responseType: 'arraybuffer' });
      await fs.writeFile(path.resolve(dir, name.length < 10 ?
          name :
          name.slice(name.length / 1.2)), file.data);
    } catch (err) {
      setTimeout(() =>
        console.error(`✗  ${link}, repsponse code: ${err.response.status} [skipped]`.yellow)
      , 2000);
    }
  }));
};
 
export default downloadFiles;