1 | 'use strict';
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 | const {URL} = require('url');
|
5 | const contentDisposition = require('content-disposition');
|
6 | const archiveType = require('archive-type');
|
7 | const decompress = require('decompress');
|
8 | const filenamify = require('filenamify');
|
9 | const getStream = require('get-stream');
|
10 | const got = require('got');
|
11 | const makeDir = require('make-dir');
|
12 | const pify = require('pify');
|
13 | const pEvent = require('p-event');
|
14 | const fileType = require('file-type');
|
15 | const extName = require('ext-name');
|
16 |
|
17 | const fsP = pify(fs);
|
18 | const filenameFromPath = res => path.basename(new URL(res.requestUrl).pathname);
|
19 |
|
20 | const getExtFromMime = res => {
|
21 | const header = res.headers['content-type'];
|
22 |
|
23 | if (!header) {
|
24 | return null;
|
25 | }
|
26 |
|
27 | const exts = extName.mime(header);
|
28 |
|
29 | if (exts.length !== 1) {
|
30 | return null;
|
31 | }
|
32 |
|
33 | return exts[0].ext;
|
34 | };
|
35 |
|
36 | const getFilename = (res, data) => {
|
37 | const header = res.headers['content-disposition'];
|
38 |
|
39 | if (header) {
|
40 | const parsed = contentDisposition.parse(header);
|
41 |
|
42 | if (parsed.parameters && parsed.parameters.filename) {
|
43 | return parsed.parameters.filename;
|
44 | }
|
45 | }
|
46 |
|
47 | let filename = filenameFromPath(res);
|
48 |
|
49 | if (!path.extname(filename)) {
|
50 | const ext = (fileType(data) || {}).ext || getExtFromMime(res);
|
51 |
|
52 | if (ext) {
|
53 | filename = `${filename}.${ext}`;
|
54 | }
|
55 | }
|
56 |
|
57 | return filename;
|
58 | };
|
59 |
|
60 | module.exports = (uri, output, opts) => {
|
61 | if (typeof output === 'object') {
|
62 | opts = output;
|
63 | output = null;
|
64 | }
|
65 |
|
66 | opts = Object.assign({
|
67 | encoding: null,
|
68 | rejectUnauthorized: process.env.npm_config_strict_ssl !== 'false'
|
69 | }, opts);
|
70 |
|
71 | const stream = got.stream(uri, opts);
|
72 |
|
73 | const promise = pEvent(stream, 'response').then(res => {
|
74 | const encoding = opts.encoding === null ? 'buffer' : opts.encoding;
|
75 | return Promise.all([getStream(stream, {encoding}), res]);
|
76 | }).then(result => {
|
77 | const [data, res] = result;
|
78 |
|
79 | if (!output) {
|
80 | return opts.extract && archiveType(data) ? decompress(data, opts) : data;
|
81 | }
|
82 |
|
83 | const filename = opts.filename || filenamify(getFilename(res, data));
|
84 | const outputFilepath = path.join(output, filename);
|
85 |
|
86 | if (opts.extract && archiveType(data)) {
|
87 | return decompress(data, path.dirname(outputFilepath), opts);
|
88 | }
|
89 |
|
90 | return makeDir(path.dirname(outputFilepath))
|
91 | .then(() => fsP.writeFile(outputFilepath, data))
|
92 | .then(() => data);
|
93 | });
|
94 |
|
95 | stream.then = promise.then.bind(promise);
|
96 | stream.catch = promise.catch.bind(promise);
|
97 |
|
98 | return stream;
|
99 | };
|