Class: Ratonvirus::Storage::ActiveStorage

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

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#accept?(resource) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/ratonvirus/storage/active_storage.rb', line 13

def accept?(resource)
  resource.is_a?(::ActiveStorage::Attached::One) ||
    resource.is_a?(::ActiveStorage::Attached::Many)
end

#asset_path(asset, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ratonvirus/storage/active_storage.rb', line 33

def asset_path(asset, &block)
  return unless block_given?
  return unless asset.is_a?(Array)

  ext = asset[0].filename.extension_with_delimiter
  case asset[1]
  when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
    # These files should be already locally stored but their permissions
    # can prevent the virus scanner executable from accessing them.
    # Therefore, a temporary file is created for them as well.
    io_path(asset[1], ext, &block)
  when Hash
    io = asset[1].fetch(:io)
    io_path(io, ext, &block) if io
  when ::ActiveStorage::Blob
    asset[1].open do |tempfile|
      prepare_for_scanner tempfile.path
      yield tempfile.path
    end
  end
end

#asset_remove(asset) ⇒ Object

This is actually only required for the dyncamic blob uploads but for consistency, it is handled for all the cases accordingly either by closing the tempfile of the upload which also removes the file when called with the bang method. For the IO references, the IO is closed which should trigger the file deletion by latest at the Rack or Ruby level during garbage collection. There is no guarantee that the file for which the IO was opened would be deleted beause the IO itself is not necessarily associated with an actual file.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ratonvirus/storage/active_storage.rb', line 63

def asset_remove(asset)
  return unless asset.is_a?(Array)

  case asset[1]
  when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
    # This removes the temp file from the system.
    asset[1].tempfile.close!
  when Hash
    # No guarantee all references for the file are deleted.
    io = asset[1].fetch(:io)
    io.close
  when ::ActiveStorage::Blob
    # This deletes the dynamically uploaded blobs that might not be
    # associated with any record at this point. This ensures the blobs are
    # not left "hanging" in the storage system and the database in case
    # automatic file deletion is applied.
    asset[1].purge
  end
end

#changed?(record, attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

def changed?(record, attribute)
  resource = record.public_send attribute
  !resource.record.attachment_changes[resource.name].nil?
end

#process(resource, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ratonvirus/storage/active_storage.rb', line 18

def process(resource, &block)
  return unless block_given?
  return if resource.nil?
  return unless resource.attached?

  change = resource.record.attachment_changes[resource.name]

  case change
  when ::ActiveStorage::Attached::Changes::CreateOne
    handle_create_one(change, &block)
  when ::ActiveStorage::Attached::Changes::CreateMany
    handle_create_many(change, &block)
  end
end