Fix FormidableHandler so it detects filetype based on extension if type is unknown or application octet-stream.
continuous-integration/appveyor/branch AppVeyor build succeeded Details

master v1.3.1
Jonatan Nilsson 2022-08-10 14:13:14 +00:00
parent 5f916e97ea
commit 8a56969015
5 changed files with 67 additions and 5 deletions

View File

@ -272,6 +272,10 @@ export function FormidableHandler(formidable, org = {}) {
return rename(ctx.req.files[key].path, target)
.then(function() {
if (!ctx.req.files[key].type || ctx.req.files[key].type === 'application/octet-stream') {
let found = MimeTypeDb[path.extname(filename).slice(1)]
ctx.req.files[key].type = found && found[0] || 'application/octet-stream'
}
ctx.req.files[key].path = target
ctx.req.files[key].filename = filename
})

View File

@ -1,6 +1,6 @@
{
"name": "flaska",
"version": "1.3.0",
"version": "1.3.1",
"description": "Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.",
"main": "flaska.mjs",
"scripts": {

View File

@ -126,7 +126,7 @@ const random = (length = 8) => {
return str;
}
Client.prototype.upload = function(url, files, method = 'POST', body = {}) {
Client.prototype.upload = function(url, files, method = 'POST', body = {}, overrideType = null) {
const boundary = `---------${random(32)}`
const crlf = '\r\n'
let upload = files
@ -145,8 +145,11 @@ Client.prototype.upload = function(url, files, method = 'POST', body = {}) {
const filename = path.basename(file)
uploadBody.push(Buffer.from(
`${crlf}--${boundary}${crlf}` +
`Content-Disposition: form-data; name="${key}"; filename="${filename}"` + crlf + crlf
`${crlf}--${boundary}${crlf}`
+ `Content-Disposition: form-data; name="${key}"; filename="${filename}"`
+ (overrideType ? crlf + `Content-Type: ${overrideType}`: '')
+ crlf
+ crlf
))
uploadBody.push(data)
})

View File

@ -301,7 +301,7 @@ t.describe('/filehandle', function() {
})
})
t.describe('/file/upload', function() {
t.only().describe('/file/upload', function() {
t.test('server should upload file', async function() {
let res = await client.upload('/file/upload', './test/test.png')
@ -312,6 +312,61 @@ t.describe('/file/upload', function() {
assert.strictEqual(statSource.size, statTarget.size)
assert.strictEqual(statSource.size, res.size)
assert.strictEqual(res.type, 'image/png')
})
t.test('server should have correct type', async function() {
let res = await client.upload('/file/upload', './test/test.jpg')
let [statSource, statTarget] = await Promise.all([
fs.stat('./test/test.jpg'),
fs.stat(res.path),
])
assert.strictEqual(statSource.size, statTarget.size)
assert.strictEqual(statSource.size, res.size)
assert.strictEqual(res.type, 'image/jpeg')
})
t.test('server should use type from user', async function() {
const assertType = 'some/test/here'
let res = await client.upload('/file/upload', './test/test.jpg', 'POST', {}, assertType)
let [statSource, statTarget] = await Promise.all([
fs.stat('./test/test.jpg'),
fs.stat(res.path),
])
assert.strictEqual(statSource.size, statTarget.size)
assert.strictEqual(statSource.size, res.size)
assert.strictEqual(res.type, assertType)
})
t.test('server should attempt to correct type if type is application/octet-stream', async function() {
const assertNotType = 'application/octet-stream'
let res = await client.upload('/file/upload', './test/test.jpg', 'POST', {}, assertNotType)
let [statSource, statTarget] = await Promise.all([
fs.stat('./test/test.jpg'),
fs.stat(res.path),
])
assert.strictEqual(statSource.size, statTarget.size)
assert.strictEqual(statSource.size, res.size)
assert.notStrictEqual(res.type, assertNotType)
assert.strictEqual(res.type, 'image/jpeg')
})
t.test('server fall back to type application/octet-stream if unknown', async function() {
let res = await client.upload('/file/upload', './test/test.gibberish')
let [statSource, statTarget] = await Promise.all([
fs.stat('./test/test.gibberish'),
fs.stat(res.path),
])
assert.strictEqual(statSource.size, statTarget.size)
assert.strictEqual(statSource.size, res.size)
assert.strictEqual(res.type, 'application/octet-stream')
res = await client.upload('/file/upload', './test/test.gibberish', 'POST', {}, 'application/octet-stream')
assert.strictEqual(res.type, 'application/octet-stream')
})
})

BIN
test/test.gibberish Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB