Skip to content

Commit

Permalink
Remove console.log
Browse files Browse the repository at this point in the history
Add test that dir exists for file upload
  • Loading branch information
hardillb committed Aug 27, 2024
1 parent f616ef6 commit 1e45149
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/files/index.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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()
}
Expand Down

0 comments on commit 1e45149

Please sign in to comment.