Skip to content

Commit 6f737cf

Browse files
authored
Getting actual downloaded file name.
1 parent 734baae commit 6f737cf

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

lib/RepositoryDownloader.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ RepositoryDownloader.prototype.fetch = function(destination) {
2020
download = require('download'),
2121
totalSize,
2222
receivedSize,
23-
previousPercent = 0;
23+
previousPercent = 0,
24+
fileName = '';
2425

2526
download(this.url, destination, {
2627
extract: true
2728
})
2829
.on('response', function (response) {
2930
totalSize = Number(response.headers['content-length']);
31+
fileName = getFileName(response.headers['content-disposition']);
3032
receivedSize = 0;
3133
})
3234
.on('data', function(data) {
@@ -46,11 +48,23 @@ RepositoryDownloader.prototype.fetch = function(destination) {
4648
deferred.reject(err);
4749
})
4850
.then(function() {
49-
deferred.resolve(destination);
51+
deferred.resolve(fileName);
5052
});
5153

5254
return deferred.promise;
5355
};
5456

57+
function getFileName(disposition) {
58+
var fileName = "";
59+
if (disposition && disposition.indexOf('attachment') !== -1) {
60+
var regex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
61+
var matches = regex.exec(disposition);
62+
if (matches != null && matches[1]) {
63+
fileName = matches[1].replace(/['"]/g, '');
64+
}
65+
}
66+
return fileName;
67+
}
68+
5569
module.exports = RepositoryDownloader;
5670

0 commit comments

Comments
 (0)