mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 01:36:29 -05:00
fix: check for existent notes on POST
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
parent
97a5e58077
commit
550bf91049
1 changed files with 25 additions and 10 deletions
|
@ -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) {
|
||||||
|
|
Loading…
Reference in a new issue