Class: ImageVise::FetcherFile

Inherits:
Object
  • Object
show all
Defined in:
lib/image_vise/fetchers/fetcher_file.rb

Defined Under Namespace

Classes: AccessError

Class Method Summary collapse

Class Method Details

.fetch_uri_to_tempfile(uri) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/image_vise/fetchers/fetcher_file.rb', line 5

def self.fetch_uri_to_tempfile(uri)
  tf = Tempfile.new 'imagevise-localfs-copy'
  real_path_on_filesystem = File.expand_path(URI.decode(uri.path))
  verify_filesystem_access! real_path_on_filesystem
  # Do the checks
  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

.verify_filesystem_access!(path_on_filesystem) ⇒ Object

Raises:



19
20
21
22
23
24
# File 'lib/image_vise/fetchers/fetcher_file.rb', line 19

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