Class: CarrierWave::Storage::ImageKitStore
- Inherits:
-
Abstract
- Object
- Abstract
- CarrierWave::Storage::ImageKitStore
- Defined in:
- lib/carrierwave/storage/imagekit_store.rb
Instance Method Summary collapse
- #cache!(new_file) ⇒ Object
- #clean_cache!(seconds) ⇒ Object
- #delete_dir!(path) ⇒ Object
-
#initialize ⇒ ImageKitStore
constructor
A new instance of ImageKitStore.
- #retrieve!(identifier) ⇒ Object
- #retrieve_from_cache!(identifier) ⇒ Object
- #store!(file) ⇒ Object
Constructor Details
#initialize ⇒ ImageKitStore
Returns a new instance of ImageKitStore.
8 9 10 11 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 8 def initialize(*) super @cache_called = nil end |
Instance Method Details
#cache!(new_file) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 22 def cache!(new_file) new_file.move_to(::File.(uploader.cache_path, uploader.root), uploader., uploader., true) rescue Errno::EMLINK, Errno::ENOSPC => e raise(e) if @cache_called @cache_called = true # NOTE: Remove cached files older than 10 minutes clean_cache!(600) cache!(new_file) end |
#clean_cache!(seconds) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 55 def clean_cache!(seconds) Dir.glob(::File.(::File.join(uploader.cache_dir, '*'), CarrierWave.root)).each do |dir| # generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND time = dir.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map(&:to_i) time = Time.at(*time) if time < (Time.now.utc - seconds) FileUtils.rm_rf(dir) end end end |
#delete_dir!(path) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 41 def delete_dir!(path) if path begin Dir.rmdir(::File.(path, uploader.root)) rescue Errno::ENOENT # Ignore: path does not exist rescue Errno::ENOTDIR # Ignore: path is not a dir rescue Errno::ENOTEMPTY, Errno::EEXIST # Ignore: dir is not empty end end end |
#retrieve!(identifier) ⇒ Object
17 18 19 20 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 17 def retrieve!(identifier) IKFile.new(identifier) end |
#retrieve_from_cache!(identifier) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 34 def retrieve_from_cache!(identifier) CarrierWave::SanitizedFile.new(::File.(uploader.cache_path(identifier), uploader.root)) resp=@client.get(identifier) # binding.pry IKFile.new(resp) end |
#store!(file) ⇒ Object
13 14 15 |
# File 'lib/carrierwave/storage/imagekit_store.rb', line 13 def store!(file) file.delete end |