Skip to content

Commit 11c096a

Browse files
committed
added support for the generate names func
1 parent 6dbaf83 commit 11c096a

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

lib/index.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const pkg = require('../package.json')
44
const fs = require('fs')
55
const Async = require('async')
66
const Hoek = require('hoek')
7+
const Boom = require('boom')
78
const Validation = require('./helpers/validation')
89
const Uploader = require('./uploader')
910

@@ -15,6 +16,7 @@ function fileUploader (server, options, next) {
1516
const tags = routeOptions.tags
1617
const auth = routeOptions.auth || false
1718
const maxBytes = uploadOptions.maxBytes
19+
const log = server.log
1820
// TODO best practice?
1921
uploadOptions.log = server.log
2022

@@ -27,7 +29,10 @@ function fileUploader (server, options, next) {
2729
function errorValidation (cb) {
2830
let methods = {}
2931
if (uploadOptions.generateName && typeof uploadOptions.generateName !== 'function') {
30-
return cb(new Error('Generate name must be a function'))
32+
if (typeof uploadOptions.generateName !== 'function') {
33+
return cb(new Error('Generate name must be a function'))
34+
}
35+
methods.generateName = uploadOptions.generateName
3136
}
3237
if (options.preUpload) {
3338
if (typeof options.preUpload !== 'function') {
@@ -59,7 +64,30 @@ function fileUploader (server, options, next) {
5964
const upload = Uploader(uploadOptions)
6065
const pre = []
6166
const uploadMethod = function (request, reply) {
62-
return upload(request.payload, reply)
67+
let options = {}
68+
if (request.pre.fileNames) options.fileNames = request.pre.fileNames
69+
return upload(request.payload, options, reply)
70+
}
71+
const fileNamesMethod = function (request, reply) {
72+
let names = {}
73+
Async.forEachOf(request.payload, (item, key, cb) => {
74+
try {
75+
names[key] = methods.generateName(item.hapi.filename, request)
76+
} catch (e) {
77+
return cb(e)
78+
}
79+
cb()
80+
}, (err) => {
81+
if (err) {
82+
log(['error'], err)
83+
return reply(Boom.internal())
84+
}
85+
reply(null, names)
86+
})
87+
}
88+
89+
if (methods.generateName) {
90+
pre.push({method: fileNamesMethod, assign: 'fileNames'})
6391
}
6492
if (methods.pre) pre.push({method: methods.pre, assign: 'preUpload'})
6593
pre.push({method: uploadMethod, assign: 'file'})

lib/uploader.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,32 @@ const fs = require('fs')
99

1010
function uploader (options) {
1111
const uploadPath = options.path
12-
const fileName = options.name
1312
const log = options.log
1413

1514
return upload
1615

17-
function upload (data, cb) {
16+
function upload (data, options, cb) {
1817
const files = []
18+
const fileNames = options.fileNames || {}
1919
Async.each(Object.keys(data), (prop, cbAsync) => {
2020
if (data.hasOwnProperty(prop)) files.push(prop)
2121
cbAsync()
2222
},
2323
(err) => {
2424
if (err) return cb(Boom.internal())
25-
saveFiles(files, data, cb)
25+
saveFiles(files, fileNames, data, cb)
2626
})
2727
}
2828

29-
function saveFiles (files, data, cb) {
29+
function saveFiles (files, fileNames, data, cb) {
3030
if (!files) return cb(Boom.badData())
31-
if (files.length === 1) return saveFile(data[files[0]], cb)
31+
if (files.length === 1) return saveFile(data[files[0]], fileNames[files[0]], cb)
3232
Async.map(files, (file, cbAsync) => {
33-
saveFile(data[file], cbAsync)
33+
saveFile(data[file], fileNames[file], cbAsync)
3434
}, cb)
3535
}
3636

37-
function saveFile (data, cb) {
37+
function saveFile (data, fileName, cb) {
3838
const mimeType = data.hapi.headers['content-type']
3939
const name = fileName || urlencode.decode(data.hapi.filename, 'utf8')
4040
const file = data
@@ -54,9 +54,10 @@ function uploader (options) {
5454
'Error while saving file, possible issue with path - ' +
5555
err
5656
))
57+
} else {
58+
// Still not sure if throw is the best to go here
59+
log(['error'], new Error('Error while saving file - ' + err))
5760
}
58-
// Still not sure if throw is the best to go here
59-
log(['error'], new Error('Error while saving file - ' + err))
6061
return cb(Boom.internal())
6162
})
6263

0 commit comments

Comments
 (0)