Skip to content

Commit

Permalink
Add ability to upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Nov 10, 2024
1 parent e77f9e1 commit 4b20d22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ REST web API for the [libgphoto2](http://www.gphoto.org) library. You can use it
| `height` | *integer* | Returns the image scaled down to the given height |
- `DELETE /cameras/:id/blob/*filepath`
- `PUT /cameras/:id/blob/*path`
Example request:
```sh
curl \
-X PUT \
-H "Accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F file="@/path/to/file.jpg" \
"http://localhost:3000/cameras/5a337150-30ba-40fd-adc2-b9ffacdad188/blob/store_00010001/DCIM007/IMG_0001.jpg"
```
#### `/cameras/:id/zip`
Expand Down
23 changes: 23 additions & 0 deletions src/gphoto2/web/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,29 @@ delete "/cameras/:id/blob/*filepath" do |env|
send_204 env
end

put "/cameras/:id/blob/*filepath" do |env|
id = env.params.url["id"]
filepath = env.params.url["filepath"]
filepath = Path.posix("/", filepath)

upload = env.params.files["file"]? || raise ArgumentError.new("Missing file")
data = upload.tempfile.getb_to_end

GPhoto2::Web.camera_by_id(id) do |camera|
file = camera
.filesystem(filepath.dirname)
.put(filepath.basename, data)

env.response.headers["Vary"] = "Accept"

if request_accepts_json?(env.request)
send_json env, file
else
send_204 env
end
end
end

# /cameras/:id/zip

get "/cameras/:id/zip" do |env|
Expand Down

0 comments on commit 4b20d22

Please sign in to comment.