Class: Dragonfly::FileDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/file_data_store.rb

Defined Under Namespace

Classes: BadUID, MarshalMetaStore, MetaStore, UnableToFormUrl, YAMLMetaStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ FileDataStore

Returns a new instance of FileDataStore.



72
73
74
75
76
77
78
# 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

Instance Attribute Details

#root_pathObject

Returns the value of attribute root_path.



81
82
83
# File 'lib/dragonfly/file_data_store.rb', line 81

def root_path
  @root_path
end

#server_rootObject

Returns the value of attribute server_root.



81
82
83
# File 'lib/dragonfly/file_data_store.rb', line 81

def server_root
  @server_root
end

#store_meta=(value) ⇒ Object (writeonly)

Sets the attribute store_meta

Parameters:

  • value

    the value to set the attribute store_meta to.



80
81
82
# File 'lib/dragonfly/file_data_store.rb', line 80

def store_meta=(value)
  @store_meta = value
end

Instance Method Details

#destroy(relative_path) ⇒ Object



128
129
130
131
132
133
134
135
136
# 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) ⇒ Object



151
152
153
154
155
156
# 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) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

#store_meta?Boolean

Returns:

  • (Boolean)


91
92
93
# 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 = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# 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 = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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