From 052dfe404f56299fe26c57fb922eef13112ff66c Mon Sep 17 00:00:00 2001 From: Mohammed Mutawa Date: Sun, 7 Jun 2020 17:58:18 +0300 Subject: [PATCH] Add option to set the uploaded file name with extension File name = hex + extension --- README.md | 8 ++++++++ lib/tus/server.rb | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 11a6efb..49af03e 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,14 @@ can change it to "attachment" if you want the browser to always force download: Tus::Server.opts[:disposition] = "attachment" ``` +## Extension + +If you need the extension in the file name, you can set `:file_extension` to true, default is false. + +```rb +Tus::Server.opts[:file_extension] = true +``` + ## Checksum The following checksum algorithms are supported for the `checksum` extension: diff --git a/lib/tus/server.rb b/lib/tus/server.rb index 43e8393..a204c35 100644 --- a/lib/tus/server.rb +++ b/lib/tus/server.rb @@ -81,7 +81,6 @@ class Server < Roda validate_upload_metadata! if request.headers["Upload-Metadata"] validate_upload_concat! if request.headers["Upload-Concat"] - uid = SecureRandom.hex info = Tus::Info.new( "Upload-Length" => request.headers["Upload-Length"], "Upload-Offset" => "0", @@ -91,6 +90,12 @@ class Server < Roda "Upload-Expires" => (Time.now + expiration_time).httpdate, ) + if opts[:file_extension] + uid = "#{SecureRandom.hex}#{File.extname(info.name)}" + else + uid = SecureRandom.hex + end + before_create(uid, info) if info.final?