Skip to content

Commit

Permalink
Merge pull request #238 from esek/fix/crash
Browse files Browse the repository at this point in the history
[FIX] Crash when sending bad accessType
  • Loading branch information
blennster authored Sep 5, 2022
2 parents f22f04c + a51e62d commit 486f275
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Alla märkbara ändringar ska dokumenteras i denna fil.
Baserat på [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
och följer [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.3] - 2022-08-30
- Fixat krash i `/files/upload`

## [1.1.2] - 2022-08-24
- Fixat så att vi inte bloatar med scalars som inte används.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ekorre-ts",
"version": "1.1.2",
"version": "1.1.3",
"description": "E-Sektionens backend",
"main": "src/index.ts",
"scripts": {
Expand Down
16 changes: 10 additions & 6 deletions src/api/file.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class FileAPI {
path: string,
creator: string,
): Promise<PrismaFile> {
try {
const type = this.getFileType(file.name);
const type = this.getFileType(file.name);

const hashedName = this.createHashedName(file.name);
const hashedName = this.createHashedName(file.name);

const trimmedPath = this.trimFolder(path);
const trimmedPath = this.trimFolder(path);

const folder = `${ROOT}/${trimmedPath}`;
const location = `${folder}${hashedName}`;
const folder = `${ROOT}/${trimmedPath}`;
const location = `${folder}${hashedName}`;

try {
// Create folder(s) if it doesn't exist
if (!syncFs.existsSync(folder)) {
await fs.mkdir(folder, { recursive: true });
Expand All @@ -71,6 +71,10 @@ class FileAPI {
return res;
} catch (err) {
logger.error(err);

// We don't care if this fails since we can't do anything about it
await fs.rm(location).catch(() => {});

throw new ServerError('Kunde inte spara filen');
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/routes/file.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,18 @@ filesRoute.post('/upload', upload(), verifyAuthenticated, async (req, res) => {

const accessType = body?.accessType ?? AccessType.Public;
const path = body?.path ?? '/';
const dbFile = await fileApi.saveFile(file, accessType, path, res.locals.user.username);

return res.send(reduce(dbFile, fileReduce));
if (!Object.values(AccessType).includes(accessType)) {
return res.status(400).send('Invalid access type');
}

try {
const dbFile = await fileApi.saveFile(file, accessType, path, res.locals.user.username);
return res.send(reduce(dbFile, fileReduce));
} catch (e) {
logger.error(e);
return res.status(500).send(e);
}
});

filesRoute.post('/upload/avatar', upload(), verifyAuthenticated, async (req, res) => {
Expand Down

0 comments on commit 486f275

Please sign in to comment.