2015-11-26 14:37:03 +05:30
|
|
|
require "tempfile"
|
2018-05-09 12:01:36 +05:30
|
|
|
require "tmpdir"
|
2015-11-26 14:37:03 +05:30
|
|
|
require "fileutils"
|
|
|
|
|
|
|
|
class UploadedFile
|
2018-05-09 12:01:36 +05:30
|
|
|
InvalidPathError = Class.new(StandardError)
|
|
|
|
|
2015-11-26 14:37:03 +05:30
|
|
|
# The filename, *not* including the path, of the "uploaded" file
|
|
|
|
attr_reader :original_filename
|
|
|
|
|
|
|
|
# The tempfile
|
|
|
|
attr_reader :tempfile
|
|
|
|
|
|
|
|
# The content type of the "uploaded" file
|
|
|
|
attr_accessor :content_type
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
attr_reader :remote_id
|
|
|
|
attr_reader :sha256
|
|
|
|
|
|
|
|
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)
|
2015-11-26 14:37:03 +05:30
|
|
|
|
|
|
|
@content_type = content_type
|
|
|
|
@original_filename = filename || ::File.basename(path)
|
2018-05-09 12:01:36 +05:30
|
|
|
@content_type = content_type
|
|
|
|
@sha256 = sha256
|
|
|
|
@remote_id = remote_id
|
2015-11-26 14:37:03 +05:30
|
|
|
@tempfile = File.new(path, 'rb')
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def self.from_params(params, field, upload_path)
|
|
|
|
unless params["#{field}.path"]
|
|
|
|
raise InvalidPathError, "file is invalid" if params["#{field}.remote_id"]
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
file_path = File.realpath(params["#{field}.path"])
|
|
|
|
|
|
|
|
unless self.allowed_path?(file_path, [upload_path, Dir.tmpdir].compact)
|
|
|
|
raise InvalidPathError, "insecure path used '#{file_path}'"
|
|
|
|
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"])
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.allowed_path?(file_path, paths)
|
|
|
|
paths.any? do |path|
|
|
|
|
File.exist?(path) && file_path.start_with?(File.realpath(path))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-26 14:37:03 +05:30
|
|
|
def path
|
|
|
|
@tempfile.path
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :local_path, :path
|
|
|
|
|
|
|
|
def method_missing(method_name, *args, &block) #:nodoc:
|
2018-03-17 18:26:18 +05:30
|
|
|
@tempfile.__send__(method_name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(method_name, include_private = false) #:nodoc:
|
|
|
|
@tempfile.respond_to?(method_name, include_private) || super
|
|
|
|
end
|
|
|
|
end
|