Skip to content

Commit

Permalink
added download progress callback
Browse files Browse the repository at this point in the history
  • Loading branch information
da1nerd committed Nov 8, 2016
1 parent 6a7d308 commit 1ebe0ed
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
26 changes: 15 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,10 @@ function Client(dbPath, resourceDir, opts) {
* @param languageSlug {string}
* @param projectSlug {string}
* @param resourceSlug {string}
* @param progressCallback {function} receives download progress updates
* @returns {Promise.<Container>} The new container
*/
const downloadContainer = function(languageSlug, projectSlug, resourceSlug) {
const downloadContainer = function(languageSlug, projectSlug, resourceSlug, progressCallback) {
// support passing args as an object
if(languageSlug != null && typeof languageSlug == 'object') {
resourceSlug = languageSlug.resourceSlug;
Expand All @@ -798,7 +799,7 @@ function Client(dbPath, resourceDir, opts) {
}

// get the legacy data as if it were a real resource container
return downloadContainer_Future(languageSlug, projectSlug, resourceSlug)
return downloadContainer_Future(languageSlug, projectSlug, resourceSlug, progressCallback)
.then(function(path) {
// migrate to resource container
let resource;
Expand Down Expand Up @@ -844,9 +845,10 @@ function Client(dbPath, resourceDir, opts) {
* @param languageSlug {string}
* @param projectSlug {string}
* @param resourceSlug {string}
* @param progressCallback {function} receives download progress updates
* @returns {Promise.<String>} the path to the downloaded resource container
*/
const downloadContainer_Future = function(languageSlug, projectSlug, resourceSlug) {
const downloadContainer_Future = function(languageSlug, projectSlug, resourceSlug, progressCallback) {
// support passing args as an object
if(languageSlug != null && typeof languageSlug == 'object') {
resourceSlug = languageSlug.resourceSlug;
Expand Down Expand Up @@ -877,7 +879,7 @@ function Client(dbPath, resourceDir, opts) {

mkdirp(path.dirname(containerDir));
if(!containerFormat.url) return Promise.reject('Missing resource format url');
return request.download(containerFormat.url, tempFile);
return request.download(containerFormat.url, tempFile, progressCallback);
})
.then(function(response) {
if(response.status !== 200) {
Expand Down Expand Up @@ -1036,13 +1038,15 @@ function Client(dbPath, resourceDir, opts) {
// build categories
try {
let categories = [];
for(let catSlug of container.info.project.categories) {
let existingCat = library.public_getters.getCategory(container.language.slug, catSlug);
let catName = existingCat ? existingCat.name : catSlug;
categories.push({
slug: catSlug,
name: catName
});
if(container.info.project.categories) {
for (let catSlug of container.info.project.categories) {
let existingCat = library.public_getters.getCategory(container.language.slug, catSlug);
let catName = existingCat ? existingCat.name : catSlug;
categories.push({
slug: catSlug,
name: catName
});
}
}
container.project.categories = categories;
} catch (err) {
Expand Down
12 changes: 11 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ function read(uri) {
*
* @param uri {string} the uri to download
* @param dest {string}
* @param progressCallback {function} receives progress updates
* @returns {Promise.<{}|Error>} the status code or an error
*/
function download(uri, dest) {
function download(uri, dest, progressCallback) {
"use strict";
progressCallback = progressCallback || function() {};
var parsedUrl = url.parse(uri, false, true);
var makeRequest = parsedUrl.protocol === 'https:' ? https.request.bind(https) : http.request.bind(http);
var serverPort = parsedUrl.port ? parsedUrl.port : parsedUrl.protocol === 'https:' ? 443 : 80;
Expand All @@ -82,6 +84,14 @@ function download(uri, dest) {
return new Promise(function (resolve, reject) {

var req = makeRequest(options, function (response) {
var size = response.headers['content-length'];
var progress = 0;

response.on('data', function(chunk) {
progress += chunk.length;
progressCallback(size, progress);
});

response.pipe(file);
file.on('finish', function() {
resolve({
Expand Down
9 changes: 8 additions & 1 deletion mocha_tests/download-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ describe('Download', function() {
this.timeout(10000);

it('should download a resource container successfully', function() {
return client.downloadResourceContainer('en', 'gen', 'ulb');
var progressCalled = false;
return client.downloadResourceContainer('en', 'gen', 'ulb', function(size, progress) {
progressCalled = true;
})
.then(function(container) {
assert.ok(progressCalled);
assert.notEqual(null, container);
});
});

it('should download, close, and reopen a resource container successfully', function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "door43-client",
"version": "0.8.7",
"version": "0.8.8",
"description": "A client library for interacting with the Door43 Resource API.",
"main": "./lib/main.js",
"scripts": {
Expand Down

0 comments on commit 1ebe0ed

Please sign in to comment.