Class: Ratonvirus::Storage::Carrierwave

Inherits:
Base
  • Object
show all
Includes:
Support::IoHandling
Defined in:
lib/ratonvirus/storage/carrierwave.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize, #process

Constructor Details

This class inherits a constructor from Ratonvirus::Storage::Base

Instance Method Details

#accept?(resource) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/ratonvirus/storage/carrierwave.rb', line 12

def accept?(resource)
  if resource.is_a?(Array)
    resource.all? { |subr| subr.is_a?(::CarrierWave::Uploader::Base) }
  else
    resource.is_a?(::CarrierWave::Uploader::Base)
  end
end

#asset_path(asset, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ratonvirus/storage/carrierwave.rb', line 20

def asset_path(asset, &block)
  return unless block_given?
  return if asset.nil?
  return if asset.file.nil?

  # If the file is a local SanitizedFile, it is faster to run the scan
  # directly against that file instead of copying it to a tempfile first
  # as below for external file storages.
  return yield asset.file.path if asset.file.is_a?(::CarrierWave::SanitizedFile)

  # The file could be externally stored, so we need to read it to memory
  # in order to create a temporary file for the scanner to perform the
  # scan on.
  io = StringIO.new(asset.file.read)
  ext = File.extname(asset.file.path)
  io_path(io, ext, &block)
end

#asset_remove(asset) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ratonvirus/storage/carrierwave.rb', line 38

def asset_remove(asset)
  path = asset.file.path
  delete_dir = asset.file.is_a?(::CarrierWave::SanitizedFile)
  asset.remove!

  return unless delete_dir

  # Remove the temp cache dir if it exists
  dir = File.dirname(path)
  FileUtils.remove_dir(dir) if File.directory?(dir)
end

#changed?(record, attribute) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/ratonvirus/storage/carrierwave.rb', line 8

def changed?(record, attribute)
  record.public_send :"#{attribute}_changed?"
end