Module: Lockbox::CarrierWaveExtensions

Defined in:
lib/lockbox/carrier_wave_extensions.rb

Defined Under Namespace

Classes: FileIO

Instance Method Summary collapse

Instance Method Details

#encrypt(**options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lockbox/carrier_wave_extensions.rb', line 7

def encrypt(**options)
  class_eval do
    before :cache, :encrypt

    def encrypt(file)
      @file = CarrierWave::SanitizedFile.new(StringIO.new(lockbox.encrypt(file.read)))
    end

    def read
      r = super
      lockbox.decrypt(r) if r
    end

    def size
      read.bytesize
    end

    def rotate_encryption!
      io = FileIO.new(read)
      io.original_filename = file.filename
      previous_value = enable_processing
      begin
        self.enable_processing = false
        store!(io)
      ensure
        self.enable_processing = previous_value
      end
    end

    private

    define_method :lockbox do
      @lockbox ||= begin
        table = model ? model.class.table_name : "_uploader"
        attribute =
          if mounted_as
            mounted_as.to_s
          else
            uploader = self
            while uploader.parent_version
              uploader = uploader.parent_version
            end
            uploader.class.name.sub(/Uploader\z/, "").underscore
          end

        Utils.build_box(self, options, table, attribute)
      end
    end
  end
end