Skip to content

Commit

Permalink
Fix a bug with the files upload for the campaign expenses (#587)
Browse files Browse the repository at this point in the history
* Reduce the cache ttl for public donations and total money collected.
The idea of the cache is to help in extreme scenarios when many requests are being fired.
One request every 2 seconds should be easy to handle by the backend.

* The expense original filenames are encoded in base64. This allows us to upload files with cyrilic names. But it adds a bit of complexity in the backend.
  • Loading branch information
slavcho authored Dec 18, 2023
1 parent c70b42f commit cf7c431
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 8 additions & 2 deletions apps/api/src/common/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ export function validateFileType(

const allowedExtensions = /txt|json|pdf|jpeg|jpg|png|xml|xlsx|xls|docx/

const isExtensionSupported = allowedExtensions.test(path.extname(file.originalname).toLowerCase())
const filename = file.originalname
let ext = path.extname(filename).toLowerCase()
if (ext == '') {
// for the expense files, the original filename is encoded in base64
ext = path.extname(file.filename).toLowerCase()
}
const isExtensionSupported = allowedExtensions.test(ext)
if (!isExtensionSupported) {
return cb(new Error('File extension is not allowed'), false)
return cb(new Error('File extension is not allowed: ' + file.filename), false)
}

cb(null, true)
Expand Down
10 changes: 9 additions & 1 deletion apps/api/src/expenses/expenses.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
StreamableFile,
NotFoundException,
UnauthorizedException,
Logger,
} from '@nestjs/common'

import { AuthenticatedUser, Public, RoleMatchingMode, Roles } from 'nest-keycloak-connect'
Expand Down Expand Up @@ -71,8 +72,15 @@ export class ExpensesController {
@Post(':expenseId/files')
@UseInterceptors(
FilesInterceptor('file', 5, {
limits: { fileSize: 1024 * 1024 * 10 }, //limit uploaded files to 5 at once and 10MB each
limits: { fileSize: 1024 * 1024 * 30 }, //limit uploaded files to 5 at once and 30MB each
fileFilter: (_req: Request, file, cb) => {
try {
// decode the name from base64
file.filename = Buffer.from(file.originalname, 'base64').toString('utf-8')
} catch {
Logger.error('Error decoding filename from base64: ', file.originalname)
}

validateFileType(file, cb)
},
}),
Expand Down

0 comments on commit cf7c431

Please sign in to comment.