-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |