formidable: Add support for site-specific filesize
continuous-integration/appveyor/branch AppVeyor build succeeded Details

This commit is contained in:
Jonatan Nilsson 2023-11-29 21:57:14 +00:00
parent afde7fb89a
commit d1bab7a929
3 changed files with 47 additions and 1 deletions

View File

@ -39,6 +39,12 @@ export function uploadFile(ctx, siteName, noprefix = false) {
form.uploadDir = `${config.get('uploadFolder')}/${siteName}`
form.maxFileSize = config.get('fileSize')
let siteSize = config.get(`sites:${siteName}:fileSize`)
if (siteSize && typeof(siteSize) === 'number') {
form.maxFileSize = siteSize
}
form.parse(ctx.req, function(err, fields, files) {
if (err) return rej(err)
if (!files || !files.file) return rej(new HttpError(422, 'File in body was missing'))

View File

@ -1,6 +1,6 @@
{
"name": "storage-upload",
"version": "2.2.11",
"version": "2.2.12",
"description": "Micro service for uploading and image resizing files to a storage server.",
"main": "index.js",
"scripts": {

View File

@ -99,6 +99,17 @@ t.timeout(10000).describe('Media (API)', () => {
})
t.describe('POST /media', function temp() {
let config
t.before(async () => {
config = (await import('../../api/config.mjs')).default
})
t.afterEach(function() {
config.sources[2].store.fileSize = 524288000
delete config.sources[1].store.sites.development.fileSize
})
t.test('should require authentication', async () => {
resetLog()
assert.strictEqual(log.error.callCount, 0)
@ -276,6 +287,35 @@ t.timeout(10000).describe('Media (API)', () => {
assert.strictEqual(img.height, 700)
assert.strictEqual(img.format, 'png')
})
t.test('should support site-specific fileSize', async () => {
let token = encode(null, { iss: 'development' }, secret)
config.sources[2].store.fileSize = 1000
let err = await assert.isRejected(
client.upload(
`/media?token=${token}`,
resolve('test.png')
)
)
assert.match(err.message, /maxFileSize exceeded/)
config.sources[1].store.sites.development.fileSize = 524288000
let data = await assert.isFulfilled(
client.upload(
`/media?token=${token}`,
resolve('test.png')
)
)
testFiles.push(data.path)
assert.ok(data)
assert.ok(data.filename)
assert.ok(data.filename.startsWith(currYear))
})
})
t.describe('POST /media/noprefix', function temp() {