diff --git a/src/routes/upload.nim b/src/routes/upload.nim index fbeeb41..6ee60a7 100644 --- a/src/routes/upload.nim +++ b/src/routes/upload.nim @@ -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 diff --git a/src/types/files.nim b/src/types/files.nim index c0996f0..1b3ce0a 100644 --- a/src/types/files.nim +++ b/src/types/files.nim @@ -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