Class: CarrierWave::Storage::NoBrainer

Inherits:
Abstract
  • Object
show all
Defined in:
lib/carrierwave/storage/nobrainer.rb

Defined Under Namespace

Classes: File

Instance Method Summary collapse

Instance Method Details

#cache!(new_file) ⇒ Object

Stores given file to cache directory.

Parameters

new_file (File, IOString, Tempfile)

any kind of file object

Returns

CarrierWave::SanitizedFile

a sanitized file



49
50
51
52
53
# File 'lib/carrierwave/storage/nobrainer.rb', line 49

def cache!(new_file)
  f = CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.cache_path)
  f.store(new_file)
  f
end

#clean_cache!(seconds) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/carrierwave/storage/nobrainer.rb', line 77

def clean_cache!(seconds)
  path_regex = %r{#{Regexp.escape(uploader.cache_dir)}/\d+-\d+-\d+-\d+/.+}

  ::NoBrainer::FileCache.where(
    path: path_regex
  ).pluck(:path).without_ordering.raw.to_a.each do |doc|
    matched = doc['path'].match(/(\d+)-\d+-\d+-\d+/)

    next unless matched
    next unless Time.at(matched[1].to_i) < (Time.now.utc - seconds)

    ::NoBrainer::FileCache.where(path: doc['path']).first.delete
  end
end

#delete_dir!(path) ⇒ Object

Deletes a cache dir



73
74
75
# File 'lib/carrierwave/storage/nobrainer.rb', line 73

def delete_dir!(path)
  # do nothing, because there's no such things as 'empty directory'
end

#retrieve!(identifier) ⇒ Object

Retrieve a file

Parameters

identifier (String)

unique identifier for file

Returns

CarrierWave::Storage::NoBrainer::File

the stored file



34
35
36
# File 'lib/carrierwave/storage/nobrainer.rb', line 34

def retrieve!(identifier)
  CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.store_path(identifier))
end

#retrieve_from_cache!(identifier) ⇒ Object

Retrieves the file with the given cache_name from the cache.

Parameters

cache_name (String)

uniquely identifies a cache file

Raises

CarrierWave::InvalidParameter

if the cache_name is incorrectly formatted.



66
67
68
# File 'lib/carrierwave/storage/nobrainer.rb', line 66

def retrieve_from_cache!(identifier)
  CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.cache_path(identifier))
end

#store!(file) ⇒ Object

Store a file

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::Storage::NoBrainer::File

the stored file



17
18
19
20
21
# File 'lib/carrierwave/storage/nobrainer.rb', line 17

def store!(file)
  f = CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.store_path)
  f.store(file)
  f
end