Merge pull request #70 from walkonsocial/master

Fix for issues - #58, #23 & #67
This commit is contained in:
Raymond Feng 2015-06-08 15:29:47 -07:00
commit 382f9e1a1f
3 changed files with 19 additions and 2 deletions

View File

@ -166,7 +166,14 @@ FileSystemProvider.prototype.upload = function (options, cb) {
}; };
try { try {
return fs.createWriteStream(filePath, fileOpts); //simulate the success event in filesystem provider
//fixes: https://github.com/strongloop/loopback-component-storage/issues/58
// & #23 & #67
var stream = fs.createWriteStream(filePath, fileOpts);
stream.on('finish', function(){
stream.emit('success');
});
return stream;
} catch (e) { } catch (e) {
cb && cb(e); cb && cb(e);
} }

View File

@ -131,6 +131,10 @@ exports.upload = function (provider, req, res, options, cb) {
self._maybeEnd(); self._maybeEnd();
}; };
writer.on('success', function (file) {
endFunc();
});
var fileSize = 0; var fileSize = 0;
if (maxFileSize) { if (maxFileSize) {
part.on('data', function (buffer) { part.on('data', function (buffer) {
@ -149,7 +153,6 @@ exports.upload = function (provider, req, res, options, cb) {
part.pipe(writer, { end: false }); part.pipe(writer, { end: false });
part.on("end", function () { part.on("end", function () {
writer.end(); writer.end();
endFunc();
}); });
}; };

View File

@ -80,6 +80,13 @@ describe('Storage service', function () {
writer.on('error', done); writer.on('error', done);
}); });
it('should emit success event', function (done) {
var writer = storageService.uploadStream('c1', 'f1.txt');
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
writer.on('success', done);
writer.on('error', done);
});
it('should download a file', function (done) { it('should download a file', function (done) {
var reader = storageService.downloadStream('c1', 'f1.txt'); var reader = storageService.downloadStream('c1', 'f1.txt');
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt'))); reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));