/
file_data_store.rb
197 lines (159 loc) · 4.6 KB
/
file_data_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'pathname'
require 'yaml'
require 'fileutils'
require 'dragonfly/utils'
module Dragonfly
class FileDataStore
# Exceptions
class BadUID < RuntimeError; end
class UnableToFormUrl < RuntimeError; end
class MetaStore
def write(data_path, meta)
File.open(meta_path(data_path), 'wb') do |f|
f.write dump(meta)
end
end
def read(data_path)
path = meta_path(data_path)
File.open(path,'rb'){|f| load(f.read) } if File.exist?(path)
end
def destroy(path)
FileUtils.rm_f meta_path(path)
end
private
def meta_path(data_path)
raise NotImplementedError
end
def dump(meta)
raise NotImplementedError
end
def load(string)
raise NotImplementedError
end
end
class YAMLMetaStore < MetaStore
def meta_path(data_path)
"#{data_path}.meta.yml"
end
def dump(meta)
YAML.dump(meta)
end
def load(string)
YAML.load(string)
end
end
class MarshalMetaStore < MetaStore
def meta_path(data_path)
"#{data_path}.meta"
end
def dump(meta)
Marshal.dump(meta)
end
def load(string)
Utils.stringify_keys Marshal.load(string)
end
end
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
attr_writer :store_meta
attr_reader :root_path, :server_root
def root_path=(path)
@root_path = path ? path.to_s : nil
end
def server_root=(path)
@server_root = path ? path.to_s : nil
end
def store_meta?
@store_meta != false # Default to true if not set
end
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
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
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
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
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
private
attr_reader :meta_store, :deprecated_meta_store
def absolute(relative_path)
relative_path.to_s == '.' ? root_path : File.join(root_path, relative_path)
end
def relative(absolute_path)
absolute_path[/^#{Regexp.escape root_path}\/?(.*)$/, 1]
end
def directory_empty?(path)
Dir.entries(path).sort == ['.','..'].sort
end
def root_path?(dir)
root_path == dir
end
def relative_path_for(filename)
time = Time.now
"#{time.strftime '%Y/%m/%d/'}#{rand(1e15).to_s(36)}_#{filename.gsub(/[^\w.]+/,'_')}"
end
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
def validate_path!(uid)
raise BadUID, uid if Utils.blank?(uid) || uid['../']
end
end
end