mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-02-21 09:08:11 +00:00
REST: added /upload?blockSize=uint parameter
Uploaded file split into blocks of requested size when being stored in BlockStore, that allows user to control efficiency of storage, and provide us simple facility to test performance with various block sizes.
This commit is contained in:
parent
de3d97db1e
commit
dd8fc32e7b
@ -161,27 +161,28 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
|
||||
router.rawApi(
|
||||
MethodPost,
|
||||
"/api/codex/v1/upload") do (
|
||||
blockSize: Option[uint]
|
||||
) -> RestApiResponse:
|
||||
## Upload a file in a streamming manner
|
||||
## Upload a file in a streamming manner,
|
||||
## split it into blocks of blockSize,
|
||||
## and save to the node's BlockStore
|
||||
##
|
||||
|
||||
trace "Handling file upload"
|
||||
var bodyReader = request.getBodyReader()
|
||||
if bodyReader.isErr():
|
||||
return RestApiResponse.error(Http500)
|
||||
return RestApiResponse.error(Http500, bodyReader.error)
|
||||
|
||||
# Attempt to handle `Expect` header
|
||||
# some clients (curl), wait 1000ms
|
||||
# before giving up
|
||||
#
|
||||
# Attempt to handle `Expect` header some clients (curl),
|
||||
# wait 1000ms before giving up
|
||||
await request.handleExpect()
|
||||
|
||||
let
|
||||
reader = bodyReader.get()
|
||||
|
||||
try:
|
||||
without cid =? (
|
||||
await node.store(AsyncStreamWrapper.new(reader = AsyncStreamReader(reader)))), error:
|
||||
let
|
||||
bodyStream = AsyncStreamWrapper.new(reader = AsyncStreamReader(bodyReader.get))
|
||||
blockSize = (blockSize.get |? BlockSize).int
|
||||
|
||||
without cid =? (await node.store(bodyStream, blockSize)), error:
|
||||
trace "Error uploading file", exc = error.msg
|
||||
return RestApiResponse.error(Http500, error.msg)
|
||||
|
||||
@ -192,7 +193,7 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
|
||||
except AsyncStreamError:
|
||||
return RestApiResponse.error(Http500)
|
||||
finally:
|
||||
await reader.closeWait()
|
||||
await bodyReader.get.closeWait()
|
||||
|
||||
# if we got here something went wrong?
|
||||
return RestApiResponse.error(Http500)
|
||||
|
Loading…
x
Reference in New Issue
Block a user