class ImageVise::FetcherFile
Public Class Methods
decode_file_uri_path(path_with_percent_encoded_components)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 24 def self.decode_file_uri_path(path_with_percent_encoded_components) path_with_percent_encoded_components.split('/').map { |component| URI.decode_www_form_component(component) }.join('/') end
encode_file_uri_path(path)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 32 def self.encode_file_uri_path(path) path.split('/').map { |component| URI.encode_www_form_component(component) }.join('/') end
fetch_uri_to_tempfile(uri)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 10 def self.fetch_uri_to_tempfile(uri) tf = Tempfile.new 'imagevise-localfs-copy' real_path_on_filesystem = uri_to_path(uri) verify_filesystem_access!(real_path_on_filesystem) verify_file_size_within_limit!(real_path_on_filesystem) File.open(real_path_on_filesystem, 'rb') do |f| IO.copy_stream(f, tf) end tf.rewind; tf rescue Exception => e ImageVise.close_and_unlink(tf) raise e end
file_url_for(path)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 28 def self.file_url_for(path) "file://#{encode_file_uri_path(path)}" end
maximum_source_file_size_bytes()
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 54 def self.maximum_source_file_size_bytes ImageVise::DEFAULT_MAXIMUM_SOURCE_FILE_SIZE end
uri_to_path(uri)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 36 def self.uri_to_path(uri) File.expand_path(decode_file_uri_path(uri.path)) end
verify_file_size_within_limit!(path_on_filesystem)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 47 def self.verify_file_size_within_limit!(path_on_filesystem) file_size = File.size(path_on_filesystem) if file_size > maximum_source_file_size_bytes raise SizeError, "#{path_on_filesystem} is too large to process (#{file_size} bytes)" end end
verify_filesystem_access!(path_on_filesystem)
click to toggle source
# File lib/image_vise/fetchers/fetcher_file.rb, line 40 def self.verify_filesystem_access!(path_on_filesystem) patterns = ImageVise.allowed_filesystem_sources matches = patterns.any? { |glob_pattern| File.fnmatch?(glob_pattern, path_on_filesystem) } raise AccessError, "filesystem access is disabled" unless patterns.any? raise AccessError, "#{path_on_filesystem} is not on the path whitelist" unless matches end