Module: CarrierWave::SecureFile::Uploader

Defined in:
lib/carrierwave/securefile/uploader.rb

Class Method Summary collapse

Class Method Details

.secure_file(model = nil, file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/carrierwave/securefile/uploader.rb', line 5

def self.secure_file(model=nil, file)
	if !model.nil? && model.id.nil?
		Rails.logger.debug "Uploading using #{CarrierWave::SecureFile.cryptable}..."
		ext_file = file + ".x1"
		File.rename(file, ext_file)
		configuration = CarrierWave::SecureFile.configuration
		if configuration.encryption_type.downcase.to_sym == :aes
			aes_key = configuration.aes_key
			if model.respond_to? :aes_key
				digest = Digest::SHA256.new
				digest.update(file.to_s)
				model.aes_key = aes_key = digest.to_s
			end
			encryptor = CarrierWave::SecureFile::AESFileEncrypt.new(aes_key, configuration.aes_iv)
			encryptor.do ext_file, file
		else
			encryptor = CarrierWave::SecureFile.cryptable.new(CarrierWave::SecureFile.cypher)
			encryptor.encrypt_file(ext_file, file)
		end
		File.unlink(ext_file)
		file
	end
end