From 1e4514949c860d73a9eb91a3426ed93858cbfcb6 Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Tue, 27 Aug 2024 10:51:08 +0100 Subject: [PATCH] Remove console.log Add test that dir exists for file upload --- lib/files/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/files/index.js b/lib/files/index.js index dadad31..47dd0c8 100644 --- a/lib/files/index.js +++ b/lib/files/index.js @@ -1,6 +1,6 @@ const { mkdir, readdir, rm, rename, stat, writeFile } = require('fs/promises') const { existsSync } = require('fs') -const { dirname, join, normalize } = require('path') +const { dirname, join, normalize, basename } = require('path') const multer = require('multer') const filesInterface = (app, settings) => { @@ -52,7 +52,6 @@ const filesInterface = (app, settings) => { } else if (err.code === 'ENOTDIR') { reply.status(400).send() } else { - console.log('unknown error') reply.status(400).send() } } @@ -74,23 +73,24 @@ const filesInterface = (app, settings) => { app.post(regex, upload.single('file'), async (request, reply) => { const startPath = normalize(join(BASE_PATH, request.params[0])) - console.log(startPath) if (startPath.startsWith(BASE_PATH)) { - console.log(request.get('content-type')) if (request.get('content-type') === 'application/json') { const newPath = request.body.path const fullPath = normalize(join(startPath, newPath)) - console.log(fullPath) if (fullPath.startsWith(BASE_PATH)) { await mkdir(fullPath, { recursive: true }) reply.status(201).send() } else { - console.log('problem') reply.status(500).send() } } else if (request.get('content-type').startsWith('multipart/form-data')) { - await writeFile(startPath, request.file.buffer) - reply.status(201).send() + const dirname = basename(startPath) + if (existsSync(dirname)) { + await writeFile(startPath, request.file.buffer) + reply.status(201).send() + } else { + reply.status(404).send() + } } else { reply.status(406).send() }