mirror of
https://github.com/glimpse-app/server.git
synced 2025-04-02 10:52:45 -04:00
file name should be unique #8
This commit is contained in:
parent
e8466d08a2
commit
ab13b5d067
2 changed files with 12 additions and 4 deletions
|
@ -25,12 +25,20 @@ proc createUploadRoutes*(cfg: Cfg) =
|
||||||
let fileName = request.formData["file"].fields["filename"]
|
let fileName = request.formData["file"].fields["filename"]
|
||||||
var fileTags: string
|
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
|
# this is a hack, I hate this
|
||||||
# convert to JsonNode to ensure we were given a proper JSON
|
# convert to JsonNode to ensure we were given a proper JSON
|
||||||
# convert back to a string because db doesnt allow for JsonNode
|
# convert back to a string because db doesnt allow for JsonNode
|
||||||
try:
|
try:
|
||||||
fileTags = $parseJson(request.formData[
|
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:
|
except KeyError:
|
||||||
fileTags = "[]"
|
fileTags = "[]"
|
||||||
except: # "except JsonError:" doesn't work for some reason
|
except: # "except JsonError:" doesn't work for some reason
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import norm/model
|
import norm/[model, pragmas]
|
||||||
import ./users
|
import ./users
|
||||||
|
|
||||||
# file objects are owned by a user
|
# file objects are owned by a user
|
||||||
type File* = ref object of Model
|
type File* = ref object of Model
|
||||||
owner*: User
|
owner*: User
|
||||||
path*: string
|
path* {.unique.}: string
|
||||||
name*: string
|
name* {.unique.}: string
|
||||||
tags*: string #? This is a temporary hack should be `seq[string]` or `JsonNode` instead
|
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
|
# creates a new file object and sets default values, recommended by the norm documentation
|
||||||
|
|
Loading…
Add table
Reference in a new issue