module Rise::Uploadable
TODO: Remove previous uploads
Attributes
original_filename[R]
remove[RW]
Public Class Methods
included(base)
click to toggle source
# File lib/rise/uploadable.rb, line 4 def self.included(base) base.extend ClassMethods end
Public Instance Methods
cache()
click to toggle source
# File lib/rise/uploadable.rb, line 31 def cache File.join(@cache_prefix, original_filename) if cached? end
cache=(identifier)
click to toggle source
# File lib/rise/uploadable.rb, line 35 def cache=(identifier) return if identifier.nil? || identifier.empty? retrieve_from_cache!(identifier) unless cached? end
cache_dir()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 67 def cache_dir # Optionally override in destination class. end
cached?()
click to toggle source
# File lib/rise/uploadable.rb, line 40 def cached? !!@cache_prefix end
file()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 96 def file @file || retrieve_from_store!(read_identifier) end
filename()
click to toggle source
# File lib/rise/uploadable.rb, line 15 def filename original_filename end
identifier()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 20 def identifier read_identifier end
remove!()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 61 def remove! file.destroy if file @file, @cache_prefix, self.original_filename = nil end
remove?()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 56 def remove? ['1', true].include?(remove) end
retrieve_from_cache!(identifier)
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 87 def retrieve_from_cache!(identifier) @cache_prefix, self.original_filename = identifier.to_s.split('/', 2) raise Rise::InvalidCachePrefix unless @cache_prefix =~ Rise.cache_prefix_regexp @file = self.class.cache.get(cache_path) rescue Stow::NotFoundError raise Rise::NotFoundError, "Missing cache file: '#{identifier}'" end
retrieve_from_store!(identifier)
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 77 def retrieve_from_store!(identifier) return if identifier.nil? || identifier.empty? @cache_prefix = nil #self.original_filename = identifier @file = self.class.store.get(store_path(identifier)) rescue Stow::NotFoundError raise Rise::NotFoundError, "Missing file: '#{identifier}'" end
store!()
click to toggle source
# File lib/rise/uploadable.rb, line 44 def store! if remove? remove! elsif cached? stored = @file.copy(path: store_path, store: self.class.store) @file.destroy @file = stored @cache_prefix = nil end end
store_dir()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 72 def store_dir # Optionally override in destination class. end
upload=(file)
click to toggle source
# File lib/rise/uploadable.rb, line 24 def upload=(file) return unless upload?(file) @cache_prefix = Rise.generate_cache_prefix unless cached? self.original_filename = determine_filename(file) @file = self.class.cache.put(file, path: cache_path) end
Protected Instance Methods
read_identifier()
click to toggle source
TODO: test
# File lib/rise/uploadable.rb, line 103 def read_identifier # Does nothing by default but ActiveModel would do: # model.read_attribute :file end
write_identifier()
click to toggle source
TODO: test before_save
# File lib/rise/uploadable.rb, line 110 def write_identifier # Does nothing by default but ActiveModel would do: # return unless cached? || remove? # model.write_attribute :file, remove? ? '' : filename end
Private Instance Methods
cache_path(name = cache)
click to toggle source
# File lib/rise/uploadable.rb, line 128 def cache_path(name = cache) File.join([cache_dir, name].compact) end
determine_filename(file)
click to toggle source
# File lib/rise/uploadable.rb, line 141 def determine_filename(file) name = if file.respond_to?(:original_filename) file.original_filename elsif file.respond_to?(:path) && !(file.path.nil? || file.path.empty?) File.basename(File.expand_path(file.path)) end sanitize(name) end
original_filename=(filename)
click to toggle source
# File lib/rise/uploadable.rb, line 136 def original_filename=(filename) raise Rise::InvalidFilename if filename =~ Rise.sanitize_regexp @original_filename = filename end
sanitize(name)
click to toggle source
# File lib/rise/uploadable.rb, line 150 def sanitize(name) name = File.basename(name.to_s.gsub('\\', '/')) # IE name.gsub!(Rise.sanitize_regexp, '-') name.sub!(/\A(|[\.-]+)\z/, 'untitled') name.squeeze('-') end
store_path(name = filename)
click to toggle source
# File lib/rise/uploadable.rb, line 132 def store_path(name = filename) File.join([store_dir, name].compact) end
upload?(file)
click to toggle source
# File lib/rise/uploadable.rb, line 118 def upload?(file) if file.nil? || file.respond_to?(:empty?) && file.empty? false elsif file.respond_to?(:read) file.respond_to?(:path) && File.file?(file.path) || file.size > 0 else raise Rise::MultipartError end end