parent
9314d38d99
commit
64ff2b5b7b
2 changed files with 133 additions and 0 deletions
@ -0,0 +1,55 @@ |
||||
'use strict'; |
||||
|
||||
const fs = require('fs-extra'); |
||||
const streamifier = require('streamifier'); |
||||
const md5 = require('md5'); |
||||
|
||||
module.exports = function(options) { |
||||
return { |
||||
name: options.name, |
||||
data: options.buffer, |
||||
encoding: options.encoding, |
||||
truncated: options.truncated, |
||||
mimetype: options.mimetype, |
||||
md5: md5(options.buffer), |
||||
mv: function(path, callback) { |
||||
// Callback is passed in, use the callback API
|
||||
if (callback) { |
||||
doMove( |
||||
() => { |
||||
callback(null); |
||||
}, |
||||
(error) => { |
||||
callback(error); |
||||
} |
||||
); |
||||
|
||||
// Otherwise, return a promise
|
||||
} else { |
||||
return new Promise((resolve, reject) => { |
||||
doMove(resolve, reject); |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* Local function that moves the file to a different location on the filesystem |
||||
* Takes two function arguments to make it compatible w/ Promise or Callback APIs |
||||
* @param {Function} successFunc |
||||
* @param {Function} errorFunc |
||||
*/ |
||||
function doMove(successFunc, errorFunc) { |
||||
const fstream = fs.createWriteStream(path); |
||||
|
||||
streamifier.createReadStream(options.buffer).pipe(fstream); |
||||
|
||||
fstream.on('error', function(error) { |
||||
errorFunc(error); |
||||
}); |
||||
|
||||
fstream.on('close', function() { |
||||
successFunc(); |
||||
}); |
||||
} |
||||
} |
||||
}; |
||||
}; |
@ -0,0 +1,78 @@ |
||||
'use strict'; |
||||
|
||||
const assert = require('assert'); |
||||
const fs = require('fs'); |
||||
const md5 = require('md5'); |
||||
const path = require('path'); |
||||
const fileFactory = require('../lib/fileFactory'); |
||||
const server = require('./server'); |
||||
|
||||
const mockBuffer = fs.readFileSync(path.join(server.fileDir, 'basketball.png')); |
||||
|
||||
describe('Test of the fileFactory factory', function() { |
||||
beforeEach(function() { |
||||
server.clearUploadsDir(); |
||||
}); |
||||
|
||||
it('return a file object', function() { |
||||
assert.ok(fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer |
||||
})); |
||||
}); |
||||
|
||||
describe('File object behavior', function() { |
||||
const file = fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer |
||||
}); |
||||
it('move the file to the specified folder', function(done) { |
||||
file.mv(path.join(server.uploadDir, 'basketball.png'), function(err) { |
||||
assert.ifError(err); |
||||
done(); |
||||
}); |
||||
}); |
||||
it('reject the mv if the destination does not exists', function(done) { |
||||
file.mv(path.join(server.uploadDir, 'unknown', 'basketball.png'), function(err) { |
||||
assert.ok(err); |
||||
done(); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('Properties', function() { |
||||
it('contains the name property', function() { |
||||
assert.equal(fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer |
||||
}).name, 'basketball.png'); |
||||
}); |
||||
it('contains the data property', function() { |
||||
assert.ok(fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer |
||||
}).data); |
||||
}); |
||||
it('contains the encoding property', function() { |
||||
assert.equal(fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer, |
||||
encoding: 'utf-8' |
||||
}).encoding, 'utf-8'); |
||||
}); |
||||
it('contains the mimetype property', function() { |
||||
assert.equal(fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer, |
||||
mimetype: 'image/png' |
||||
}).mimetype, 'image/png'); |
||||
}); |
||||
it('contains the md5 property', function() { |
||||
const mockMd5 = md5(mockBuffer); |
||||
assert.equal(fileFactory({ |
||||
name: 'basketball.png', |
||||
buffer: mockBuffer |
||||
}).md5, mockMd5); |
||||
}); |
||||
}); |
||||
}); |
Loading…
Reference in new issue