diff --git a/flaska.mjs b/flaska.mjs index 9b60aea..98d361b 100644 --- a/flaska.mjs +++ b/flaska.mjs @@ -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 }) diff --git a/package.json b/package.json index 85ec795..10e26e3 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/client.mjs b/test/client.mjs index 029c0b1..27383b4 100644 --- a/test/client.mjs +++ b/test/client.mjs @@ -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) }) diff --git a/test/http.test.mjs b/test/http.test.mjs index 1d63c7b..e3e3742 100644 --- a/test/http.test.mjs +++ b/test/http.test.mjs @@ -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') }) }) diff --git a/test/test.gibberish b/test/test.gibberish new file mode 100644 index 0000000..f68a74f Binary files /dev/null and b/test/test.gibberish differ