file name should be unique #8

This commit is contained in:
array-in-a-matrix 2024-08-07 11:17:09 -04:00
parent e8466d08a2
commit ab13b5d067
2 changed files with 12 additions and 4 deletions

View file

@ -25,12 +25,20 @@ proc createUploadRoutes*(cfg: Cfg) =
let fileName = request.formData["file"].fields["filename"]
var fileTags: string
block UniqueFileNameCheck:
try:
var file = newFile()
db.select(file, """"File".name = $1""", fileName)
except NotFoundError:
break UniqueFileNameCheck
resp Http403, "A file with that name already exists.\n"
# this is a hack, I hate this
# convert to JsonNode to ensure we were given a proper JSON
# convert back to a string because db doesnt allow for JsonNode
try:
fileTags = $parseJson(request.formData[
"tags"].body) # TODO: sanitize, only an array of strings (e.g. nested objects/arrays)
"tags"].body) # TODO: sanitize, only an array of strings (e.g. remove nested objects/arrays)
except KeyError:
fileTags = "[]"
except: # "except JsonError:" doesn't work for some reason

View file

@ -1,11 +1,11 @@
import norm/model
import norm/[model, pragmas]
import ./users
# file objects are owned by a user
type File* = ref object of Model
owner*: User
path*: string
name*: string
path* {.unique.}: string
name* {.unique.}: string
tags*: string #? This is a temporary hack should be `seq[string]` or `JsonNode` instead
# creates a new file object and sets default values, recommended by the norm documentation