Module: CarrierWave::AttachmentScanner

Defined in:
lib/carrierwave/attachmentscanner.rb,
lib/carrierwave/attachmentscanner/version.rb

Defined Under Namespace

Classes: AttachmentScannerError

Constant Summary collapse

Config =
Struct.new(:url, :api_token, :enabled, :logger)
                   .new(ENV['ATTACHMENT_SCANNER_URL'], ENV['ATTACHMENT_SCANNER_API_TOKEN'],
true, Logger.new(STDOUT))
DISABLED_WARNING =
"[CarrierWave::AttachmentScanner] Disabled".freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure {|Config| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/carrierwave/attachmentscanner.rb', line 27

def self.configure
  raise ArgumentError, "Block must be specified for configure" unless block_given?

  yield(Config)
end

.included(base) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/carrierwave/attachmentscanner.rb', line 18

def self.included(base)
  if Config.enabled
    raise ArgumentError, "AttachmentScanner API Token is required" unless Config.api_token
    raise ArgumentError, "AttachmentScanner URL is required" unless Config.url
  end

  base.before :cache, :scan_file!
end

Instance Method Details

#blocked_scan_statusesObject



52
53
54
# File 'lib/carrierwave/attachmentscanner.rb', line 52

def blocked_scan_statuses
  %w(found)
end

#scan_error_message(_result) ⇒ Object

This can be overridden in order to change the message



57
58
59
# File 'lib/carrierwave/attachmentscanner.rb', line 57

def scan_error_message(_result)
  "AttachmentScanner prevented this upload"
end

#scan_file!(new_file) ⇒ Object



33
34
35
36
37
38
# File 'lib/carrierwave/attachmentscanner.rb', line 33

def scan_file!(new_file)
  return Config.logger.warn(DISABLED_WARNING) unless Config.enabled

  result = send_to_scanner(new_file)
  scan_result_allowed?(result)
end

#scan_result_allowed?(result) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/carrierwave/attachmentscanner.rb', line 40

def scan_result_allowed?(result)
  Config.logger.info("[CarrierWave::AttachmentScanner] status: #{result['status']}")
  return true unless blocked_scan_statuses.include?(result['status'])

  Config.logger.warn("[CarrierWave::AttachmentScanner] matched: #{result['matches']}")

  error = AttachmentScannerError.new(scan_error_message(result))
  error.status = result['status']
  error.matches = result['matches']
  raise error
end