fix: check for existent notes on POST

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2023-07-23 20:28:09 +02:00
parent 97a5e58077
commit 550bf91049
No known key found for this signature in database
GPG key ID: DB99ADDDC5C0AF82

View file

@ -2,6 +2,7 @@ const models = require('../../models')
const logger = require('../../logger') const logger = require('../../logger')
const config = require('../../config') const config = require('../../config')
const errors = require('../../errors') const errors = require('../../errors')
const { Op } = require('sequelize')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
@ -48,7 +49,14 @@ exports.checkViewPermission = function (req, note) {
exports.newNote = async function (req, res, body) { exports.newNote = async function (req, res, body) {
let owner = null let owner = null
let decodedNoteId
const noteId = req.params.noteId ? req.params.noteId : null const noteId = req.params.noteId ? req.params.noteId : null
try {
decodedNoteId = models.Note.decodeNoteId(noteId)
} catch (error) {
decodedNoteId = ''
}
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
owner = req.user.id owner = req.user.id
} else if (!config.allowAnonymous) { } else if (!config.allowAnonymous) {
@ -62,17 +70,24 @@ exports.newNote = async function (req, res, body) {
} }
try { try {
const id = await new Promise((resolve, reject) => { const possibleFilePath = path.join(config.docsPath, path.basename(noteId) + '.md')
models.Note.parseNoteId(noteId, (err, id) => { const noteFileExists = await models.Note.checkFileExist(possibleFilePath)
if (err) { const count = await models.Note.count({
reject(err) where: {
} else { [Op.or]: [
resolve(id) {
alias: req.alias
},
{
id: decodedNoteId
},
{
shortId: req.alias
}
]
} }
}) })
}) if (count > 0 || noteFileExists) {
if (id) {
return errors.errorConflict(res) return errors.errorConflict(res)
} }
} catch (error) { } catch (error) {