Sha256: 4a81684bdd6b2203e7f135ff7a25b5252ce8c6a49a21c854c9e95e22321ad233

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

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

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

        # TODO safe to memoize?
        def read
          r = super
          lockbox.decrypt(r) if r
        end

        def size
          read.bytesize
        end

        # based on CarrierWave::SanitizedFile#mime_magic_content_type
        def content_type
          MimeMagic.by_magic(read).try(:type) || "invalid/invalid"
        end

        def rotate_encryption!
          io = Lockbox::IO.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
  end
end

CarrierWave::Uploader::Base.extend(Lockbox::CarrierWaveExtensions)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lockbox-0.3.6 lib/lockbox/carrier_wave_extensions.rb