Skip to content

Commit

Permalink
feat(file): add option to define custom tmp file names
Browse files Browse the repository at this point in the history
Now one can define `tmpFileName` function inside the config, which can return the file name to be
used for tmp files

Enhances #2
  • Loading branch information
thetutlage committed Oct 29, 2017
1 parent 257d031 commit 0f50a83
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
19 changes: 19 additions & 0 deletions examples/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,24 @@ module.exports = {
|
*/
processManually: []

/*
|--------------------------------------------------------------------------
| Temporary file name
|--------------------------------------------------------------------------
|
| Define a function, which should return a string to be used as the
| tmp file name.
|
| If not defined, Bodyparser will use `uuid` as the tmp file name.
|
| To be defined as. If you are defining the function, then do make sure
| to return a value from it.
|
| tmpFileName () {
| return 'some-unique-value'
| }
|
*/
}
}
2 changes: 1 addition & 1 deletion src/BodyParser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class BodyParser {

request.multipart.file('*', {}, async (file) => {
this.files.add(file._fieldName, file)
await file.moveToTmp()
await file.moveToTmp(this.config.files.tmpFileName)
})

request.multipart.field((name, value) => {
Expand Down
12 changes: 10 additions & 2 deletions src/Multipart/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,22 @@ class File {
*
* @method moveToTmp
*
* @package {Function} tmpNameFn
*
* @return {Promise}
*/
moveToTmp () {
moveToTmp (tmpNameFn) {
if (this.ended) {
throw CE.FileMoveException.multipleMoveAttempts(this._fieldName)
}

this._tmpPath = path.join(os.tmpdir(), `ab-${uuid()}.tmp`)
/**
* The function to be used for generating
* the tmp file name
*/
tmpNameFn = typeof (tmpNameFn) === 'function' ? tmpNameFn : () => `ab-${uuid()}.tmp`

this._tmpPath = path.join(os.tmpdir(), tmpNameFn())
debug('moving file %s to tmp directory %s', this._fieldName, this._tmpPath)
return this._streamFile(this._tmpPath)
}
Expand Down
27 changes: 26 additions & 1 deletion test/unit/body-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

const test = require('japa')
const path = require('path')
const os = require('os')
const Browser = require('zombie')
const { Config } = require('@adonisjs/sink')
const app = require('./app')
const BodyParser = require('../../src/BodyParser')
const supertest = require('supertest')
const TEST_URL = 'http://localhost:4000/'
const supertest = require('supertest')

test.group('Body Parser', (group) => {
group.before(() => {
Expand Down Expand Up @@ -511,4 +512,28 @@ test.group('Body Parser', (group) => {
assert.deepEqual(body.files, {})
assert.deepEqual(body.raw, {})
})

test('create custom tmp file name from config', async (assert) => {
/**
* Post method to handle the form submission
*/
app.post = async function (request, res) {
const config = new Config()
config.set('bodyParser.files.tmpFileName', function () {
return 'abc'
})

const parser = new BodyParser(config)
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._files['ignore-file'].toJSON()))
res.end()
}

const { body } = await supertest(app.server)
.post('/')
.attach('ignore-file', path.join(__dirname, '../../.gitignore'))

assert.equal(body.tmpPath, path.join(os.tmpdir(), 'abc'))
})
})

0 comments on commit 0f50a83

Please sign in to comment.