Skip to content

Commit

Permalink
Merge pull request #554 from dalebremner/download-http-file-enhancement
Browse files Browse the repository at this point in the history
Suppress download if file exists
  • Loading branch information
keedya committed Nov 10, 2017
2 parents b4e1757 + c9a9f44 commit 8245b88
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
48 changes: 28 additions & 20 deletions lib/jobs/download-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ di.annotate(downloadFileJobFactory,
new di.Inject(
'Job.Base',
'Logger',
'Assert',
'Util',
'_',
'Promise',
'fs',
'ChildProcess'
)
);

function downloadFileJobFactory(BaseJob,
Logger,
assert,
util,
_,
Promise,
fs,
ChildProcess) {
var logger = Logger.initialize(downloadFileJobFactory);

Expand All @@ -40,7 +40,6 @@ function downloadFileJobFactory(BaseJob,
DownloadFileJob.super_.call(self, logger, options, context, taskId);

self.nodeId = self.context.target;

if (self.options.filePath) {
self.options.filePath = self.options.filePath.trim();
if (_.last(self.options.filePath) === '/') {
Expand All @@ -57,30 +56,39 @@ function downloadFileJobFactory(BaseJob,
DownloadFileJob.prototype._run = function () {
var self = this;
if (self.options.filePath && self.options.filePath.toLowerCase().startsWith('http')) {
return Promise.resolve()
.then(function () {
var childProcess = new ChildProcess(
'wget',
[self.options.filePath]
);
return childProcess.run({retries: 0, delay: 0});
}).then(function () {
self._done();
}).catch(function (error) {
self._done(error);
logger.error('failed to get file', {
error: error,
filePath: self.filePath,
nodeId: self.nodeId,
context: self.context
var fileName = self.options.serverFilePath;
if (fs.existsSync(fileName)) {
return Promise.resolve()
.then(function () {
self._done();
});
});
} else {
return Promise.resolve()
.then(function () {
var childProcess = new ChildProcess(
'wget',
[self.options.filePath]
);
return childProcess.run({retries: 0, delay: 0});
}).then(function () {
self._done();
}).catch(function (error) {
self._done(error);
logger.error('failed to get file', {
error: error,
filePath: self.filePath,
nodeId: self.nodeId,
context: self.context
});
});
}
} else {
return Promise.resolve()
.then(function () {
self._done();
});
}
};

return DownloadFileJob;
}
3 changes: 2 additions & 1 deletion lib/task-data/tasks/download-http-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
injectableName: 'Task.Download.Http.File',
implementsTask: 'Task.Base.Download.Http.File',
options: {
filePath: null
filePath: null,
serverFilePath: null
},
properties: {}
};
10 changes: 9 additions & 1 deletion spec/lib/jobs/download-file-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'use strict';

describe("download-file", function () {
var instance, downloadFile, uuid;
var instance, downloadFile, uuid, fs;

var mockChildProcessFactory = function () {
function MockChildProcess(command, args, env) {
Expand Down Expand Up @@ -39,6 +39,7 @@ describe("download-file", function () {
]);
downloadFile = helper.injector.get('Job.Download.File');
uuid = helper.injector.get('uuid');
fs = helper.injector.get('fs');
});


Expand All @@ -48,23 +49,30 @@ describe("download-file", function () {
});

describe('runCommand', function () {
var fsExist;
beforeEach('runCommand before', function() {
fsExist = this.sandbox.stub(fs, 'existsSync');
});
afterEach('runCommand after', function () {
this.sandbox.restore();
});

it('should resolve on success', function () {
this.sandbox.spy(instance, '_done');
instance.options.filePath = 'http://somefile';
instance.options.serverFilePath = 'local/server/pathTo/somefile';
return instance._run()
.then(function () {
expect(instance._run).to.be.resolved;
expect(fs.existsSync).to.be.calledOnce;
expect(instance._done).to.be.calledWith();
});
});

it('should error on failure', function () {
this.sandbox.spy(instance, '_done');
instance.options.filePath = 'http://badLocation';
instance.options.serverFilePath = 'local/server/pathTo/somefile';
return instance._run()
.then(function () {
expect(instance._run).to.not.be.resolved;
Expand Down

0 comments on commit 8245b88

Please sign in to comment.