Class: ImageVise::FetcherFile

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

Defined Under Namespace

Classes: AccessError, SizeError

Class Method Summary collapse

Class Method Details

.fetch_uri_to_tempfile(uri) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

.maximum_source_file_size_bytesObject



42
43
44
# File 'lib/image_vise/fetchers/fetcher_file.rb', line 42

def self.maximum_source_file_size_bytes
  ImageVise::DEFAULT_MAXIMUM_SOURCE_FILE_SIZE
end

.uri_to_path(uri) ⇒ Object



24
25
26
# File 'lib/image_vise/fetchers/fetcher_file.rb', line 24

def self.uri_to_path(uri)
  File.expand_path(URI.decode(uri.path))
end

.verify_file_size_within_limit!(path_on_filesystem) ⇒ Object



35
36
37
38
39
40
# File 'lib/image_vise/fetchers/fetcher_file.rb', line 35

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) ⇒ Object

Raises:



28
29
30
31
32
33
# File 'lib/image_vise/fetchers/fetcher_file.rb', line 28

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