class Dragonfly::FileDataStore

Attributes

deprecated_meta_store[R]
meta_store[R]
root_path[R]
server_root[R]
store_meta[W]

Public Class Methods

new(opts={}) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 72
def initialize(opts={})
  self.root_path = opts[:root_path] || 'dragonfly'
  self.server_root = opts[:server_root]
  self.store_meta = opts[:store_meta]
  @meta_store = YAMLMetaStore.new
  @deprecated_meta_store = MarshalMetaStore.new
end

Public Instance Methods

destroy(relative_path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 128
def destroy(relative_path)
  validate_path!(relative_path)
  path = absolute(relative_path)
  FileUtils.rm path
  meta_store.destroy(path)
  purge_empty_directories(relative_path)
rescue Errno::ENOENT => e
  Dragonfly.warn("#{self.class.name} destroy error: #{e}")
end
disambiguate(path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 151
def disambiguate(path)
  dirname = File.dirname(path)
  basename = File.basename(path, '.*')
  extname = File.extname(path)
  "#{dirname}/#{basename}_#{(Time.now.usec*10 + rand(100)).to_s(32)}#{extname}"
end
read(relative_path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 113
def read(relative_path)
  validate_path!(relative_path)
  path = absolute(relative_path)
  pathname = Pathname.new(path)
  return nil unless pathname.exist?
  [
    pathname,
    (
      if store_meta?
        meta_store.read(path) || deprecated_meta_store.read(path) || {}
      end
    )
  ]
end
root_path=(path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 83
def root_path=(path)
  @root_path = path ? path.to_s : nil
end
server_root=(path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 87
def server_root=(path)
  @server_root = path ? path.to_s : nil
end
store_meta?() click to toggle source
# File lib/dragonfly/file_data_store.rb, line 91
def store_meta?
  @store_meta != false # Default to true if not set
end
url_for(relative_path, opts={}) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 138
def url_for(relative_path, opts={})
  if server_root.nil?
    raise UnableToFormUrl, "you need to configure server_root for #{self.class.name} in order to form urls"
  else
    _, __, path = absolute(relative_path).partition(server_root)
    if path.empty?
      raise UnableToFormUrl, "couldn't form url for uid #{relative_path.inspect} with root_path #{root_path.inspect} and server_root #{server_root.inspect}"
    else
      path
    end
  end
end
write(content, opts={}) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 95
def write(content, opts={})
  relative_path = if opts[:path]
    opts[:path]
  else
    filename = content.name || 'file'
    relative_path = relative_path_for(filename)
  end

  path = absolute(relative_path)
  until !File.exist?(path)
    path = disambiguate(path)
  end
  content.to_file(path).close
  meta_store.write(path, content.meta) if store_meta?

  relative(path)
end

Private Instance Methods

absolute(relative_path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 162
def absolute(relative_path)
  relative_path.to_s == '.' ? root_path : File.join(root_path, relative_path)
end
directory_empty?(path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 170
def directory_empty?(path)
  Dir.entries(path) == ['.','..']
end
purge_empty_directories(path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 183
def purge_empty_directories(path)
  containing_directory = Pathname.new(path).dirname
  containing_directory.ascend do |relative_dir|
    dir = absolute(relative_dir)
    FileUtils.rmdir dir if directory_empty?(dir) && !root_path?(dir)
  end
end
relative(absolute_path) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 166
def relative(absolute_path)
  absolute_path[/^#{Regexp.escape root_path}\/?(.*)$/, 1]
end
relative_path_for(filename) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 178
def relative_path_for(filename)
  time = Time.now
  "#{time.strftime '%Y/%m/%d/'}#{rand(1e15).to_s(36)}_#{filename.gsub(/[^\w.]+/,'_')}"
end
root_path?(dir) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 174
def root_path?(dir)
  root_path == dir
end
validate_path!(uid) click to toggle source
# File lib/dragonfly/file_data_store.rb, line 191
def validate_path!(uid)
  raise BadUID, uid if Utils.blank?(uid) || uid['../']
end