From cf60bdb65268233b85f568799e04f67d1bac4342 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Tue, 29 May 2018 13:48:23 +0200 Subject: Disable local copy of files with direct_upload When workhorse writes files directly to object storage, there's no need to also write it on disk --- app/uploaders/object_storage.rb | 1 + lib/uploaded_file.rb | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/uploaders/object_storage.rb b/app/uploaders/object_storage.rb index 0a44d60778d..800a7e5cbb7 100644 --- a/app/uploaders/object_storage.rb +++ b/app/uploaders/object_storage.rb @@ -185,6 +185,7 @@ module ObjectStorage end def workhorse_local_upload_path + return if self.object_store_enabled? && self.direct_upload_enabled? File.join(self.root, TMP_UPLOAD_PATH) end diff --git a/lib/uploaded_file.rb b/lib/uploaded_file.rb index aae542f02ac..cfd866f97ab 100644 --- a/lib/uploaded_file.rb +++ b/lib/uploaded_file.rb @@ -18,16 +18,23 @@ class UploadedFile attr_reader :remote_id attr_reader :sha256 + attr_reader :size - def initialize(path, filename: nil, content_type: "application/octet-stream", sha256: nil, remote_id: nil) - raise InvalidPathError, "#{path} file does not exist" unless ::File.exist?(path) + def initialize(path, filename: nil, content_type: "application/octet-stream", sha256: nil, remote_id: nil, size: nil) + if remote_id.blank? + raise InvalidPathError, "#{path} file does not exist" unless ::File.exist?(path) + + @tempfile = File.new(path, 'rb') + @size = @tempfile.size + else + @size = size + end @content_type = content_type @original_filename = sanitize_filename(filename || path) @content_type = content_type @sha256 = sha256 @remote_id = remote_id - @tempfile = File.new(path, 'rb') end def self.from_params(params, field, upload_paths) @@ -37,18 +44,25 @@ class UploadedFile return end - file_path = File.realpath(params["#{field}.path"]) + file_path = nil + + if params["#{field}.path"] + file_path = File.realpath(params["#{field}.path"]) - paths = Array(upload_paths) << Dir.tmpdir - unless self.allowed_path?(file_path, paths.compact) - raise InvalidPathError, "insecure path used '#{file_path}'" + paths = Array(upload_paths) << Dir.tmpdir + unless self.allowed_path?(file_path, paths.compact) + raise InvalidPathError, "insecure path used '#{file_path}'" + end + else + raise InvalidPathError, "file is invalid" if params["#{field}.remote_id"].blank? end UploadedFile.new(file_path, filename: params["#{field}.name"], content_type: params["#{field}.type"] || 'application/octet-stream', sha256: params["#{field}.sha256"], - remote_id: params["#{field}.remote_id"]) + remote_id: params["#{field}.remote_id"], + size: params["#{field}.size"]) end def self.allowed_path?(file_path, paths) -- cgit v1.2.1