Skip to content

Commit

Permalink
Added the /overwrite-chunk endpoint along with its docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnworth committed Apr 8, 2013
1 parent f7c9743 commit e68ab01
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
44 changes: 44 additions & 0 deletions docs/overwrite-chunk.md
@@ -0,0 +1,44 @@
Overwriting a chunk of a file
-------------------------

__URL Path__: /overwrite-chunk

__HTTP Method__: POST

__Action__: "overwrite-chunk"

__Error Codes___: ERR_DOES_NOT_EXIST, ERR_NOT_READABLE, ERR_NOT_A_FILE, ERR_NOT_A_USER

__Request Query Parameters___:
* user - the iRODS username of the user making the request.

__Request Body__:

{
"path" : "/iplant/home/wregglej/testfile",
"position" : "20",
"update" : "I am an update"
}

__Response__:

{
"action" : "overwrite-chunk",
"status" : "success",
"path" : "/iplant/home/wregglej/testfile",
"user" : "wregglej",
"start" : "20",
"chunk-size" : "14",
"file-size" : "34"
}

__Curl Command__:

curl -H "Content-Type:application/json" -d '{"path" : "/iplant/home/wregglej/testfile", "position" : "20", "update" : "I am an update"}' http://127.0.0.1:31360/overwrite-chunk?user=wregglej

Notes:
* 'position' is in bytes.
* 'position' must be parseable as longs.
* 'start', 'chunk-size', and 'file-size' in the response are all in bytes.
* 'start', 'chunk-size', amd 'file-size' in the response should all be parseable as longs.
* The byte at 'position' is not included in the overwrite. The overwrite begins at position + 1.
2 changes: 1 addition & 1 deletion docs/read-chunk.md
Expand Up @@ -5,7 +5,7 @@ __URL Path__: /read-chunk

__HTTP Method__: POST

__Action__: "rename"
__Action__: "read-chunk"

__Error Codes___: ERR_DOES_NOT_EXIST, ERR_NOT_READABLE, ERR_NOT_A_USER

Expand Down
22 changes: 22 additions & 0 deletions src/nibblonian/controllers.clj
Expand Up @@ -737,3 +737,25 @@
pos (Long/parseLong (:position body))
size (Long/parseLong (:chunk-size body))]
(irods-actions/read-file-chunk user path pos size)))

(defn do-overwrite-chunk
[request]
(log/debug "do-overwrite-chunk")

(when-not (query-param? request "user")
(bad-query "user"))

(when-not (valid-body? request {:path string?})
(bad-body request {:path string?}))

(when-not (valid-body? request {:position string?})
(bad-body request {:position string?}))

(when-not (valid-body? request {:update string?})
(bad-body request {:update string?}))

(let [user (query-param request "user")
body (:body request)
path (:path body)
pos (Long/parseLong (:position body))]
(irods-actions/overwrite-file-chunk user path pos (:update body))))
3 changes: 3 additions & 0 deletions src/nibblonian/core.clj
Expand Up @@ -137,6 +137,9 @@

(POST "/read-chunk" request
(trap "read-chunk" do-read-chunk request))

(POST "/overwrite-chunk" request
(trap "overwrite-chunk" do-overwrite-chunk request))

(route/not-found "Not Found!"))

Expand Down
15 changes: 15 additions & 0 deletions src/nibblonian/irods_actions.clj
Expand Up @@ -1051,3 +1051,18 @@
:chunk-size (str chunk-size)
:file-size (str (file-size cm path))
:chunk (read-at-position cm path position chunk-size)}))

(defn overwrite-file-chunk
"Writes a chunk of a file starting at 'position' and extending to the length of the string."
[user path position update-string]
(with-jargon (jargon-config) [cm]
(validators/user-exists cm user)
(validators/path-exists cm path)
(validators/path-is-file cm path)
(validators/path-writeable cm user path)
(overwrite-at-position cm path position update-string)
{:path path
:user user
:start (str position)
:chunk-size (str (count (.getBytes update-string)))
:file-size (str (file-size cm path))}))

0 comments on commit e68ab01

Please sign in to comment.