Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting sub-prefix from client side for S3 storage #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ a `:prefix` in the storage configuration.
Tus::Storage::S3.new(prefix: "tus", **options)
```

To set a subdirectory from the client side you can set `Sub-Prefix` in the HTTP headers. This can be in addition to the previous option or by its own.

```js
headers: {
'Sub-Prefix': 'images'
}
```

You can also specify additional options that will be fowarded to
[`Aws::S3::Client#create_multipart_upload`] using `:upload_options`.

Expand Down
4 changes: 3 additions & 1 deletion lib/tus/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class Server < Roda
"Upload-Expires" => (Time.now + expiration_time).httpdate,
)

storage.set_sub_prefix(request.headers['Sub-Prefix'])

before_create(uid, info)

if info.final?
Expand Down Expand Up @@ -396,7 +398,7 @@ def handle_cors!

if request.options?
response.headers["Access-Control-Allow-Methods"] = "POST, GET, HEAD, PATCH, DELETE, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "Authorization, Origin, X-Requested-With, X-Request-ID, X-HTTP-Method-Override, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata, Upload-Defer-Length, Upload-Concat"
response.headers["Access-Control-Allow-Headers"] = "Authorization, Origin, X-Requested-With, X-Request-ID, X-HTTP-Method-Override, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata, Upload-Defer-Length, Upload-Concat, Sub-Prefix"
response.headers["Access-Control-Max-Age"] = "86400"
else
response.headers["Access-Control-Expose-Headers"] = "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata, Upload-Defer-Length, Upload-Concat"
Expand Down
8 changes: 8 additions & 0 deletions lib/tus/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ def client
bucket.client
end

def set_sub_prefix(sub_prefix)
if prefix
@prefix = "#{prefix}/#{sub_prefix}"
else
@prefix = "#{sub_prefix}"
end
end

private

# Uploads given body as a new multipart part with the specified part
Expand Down