dropboximus_prime 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/dropboximus_prime.rb +214 -107
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 373d813b947c72c161f5e7aa35dc49c90792adb6
|
4
|
+
data.tar.gz: 1b438d41dd8bc92b2cf83ab2a28861e9baca66c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c0ef5a55b11611cded0a493416b13e3d1ecf4212fe1ddb44cb9325891978369fff2e42d6e2522526418336d36c0ff90699b27cb993550b68c4e0f17bae6d5f4
|
7
|
+
data.tar.gz: a74983230dc7989e099eeac90ff5bb8f7faa726bdca5702bb3ff0df9c2a69f9208baf0028348d448ab814003a2dafab8ba498acc09ea75e47ae38af86610b30e
|
data/lib/dropboximus_prime.rb
CHANGED
@@ -1,180 +1,287 @@
|
|
1
|
-
require 'delegate'
|
2
1
|
require 'yaml'
|
3
2
|
require 'dropbox_sdk'
|
4
3
|
require 'redcarpet'
|
5
4
|
require 'guid'
|
6
5
|
require 'pathname'
|
7
6
|
require 'fileutils'
|
7
|
+
require 'time'
|
8
8
|
|
9
9
|
class DropboximusPrime
|
10
|
-
attr_accessor :settings
|
10
|
+
attr_accessor :settings, :markdown, :dropbox
|
11
11
|
|
12
|
-
def initialize
|
13
|
-
@settings =
|
14
|
-
@dropbox =
|
15
|
-
@markdown =
|
12
|
+
def initialize arg=nil
|
13
|
+
@settings = init_settings(arg)
|
14
|
+
@dropbox = init_dropbox
|
15
|
+
@markdown = init_markdown
|
16
16
|
end
|
17
17
|
|
18
18
|
def get *items
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
aggregate_paths(items).each_with_object({}) { |key, memo|
|
20
|
+
memo[key] = get_item(cache_path(key))
|
21
|
+
}
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
25
|
-
|
26
|
-
|
27
|
-
return false unless File.exist?(filepath)
|
24
|
+
def get_one item
|
25
|
+
get(item).values[0]
|
26
|
+
end
|
28
27
|
|
29
|
-
|
28
|
+
def refresh *items
|
29
|
+
if items.length > 0
|
30
|
+
local_paths = aggregate_paths(items)
|
31
|
+
else
|
32
|
+
local_paths = dropbox_list_files_recursively(@settings['dropbox']['path']).map! { |item|
|
33
|
+
remote_path_to_relative_path item
|
34
|
+
}
|
35
|
+
end
|
30
36
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
item
|
37
|
+
local_paths.each { |item|
|
38
|
+
refresh_item(item)
|
39
|
+
}
|
35
40
|
end
|
36
41
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
42
|
+
def prune!
|
43
|
+
local_files = aggregate_paths(['**/*'])
|
44
|
+
local_dirs = dirs_only(local_files)
|
45
|
+
|
46
|
+
remote_files = dropbox_list_files_recursively(@settings['dropbox']['path']).each_with_object([]) { |x, memo|
|
47
|
+
memo.push remote_path_to_relative_path(x)
|
48
|
+
}
|
49
|
+
remote_dirs = dropbox_list_dirs_recursively(@settings['dropbox']['path']).each_with_object([]) { |x, memo|
|
50
|
+
memo.push remote_path_to_relative_path(x)
|
51
|
+
}
|
52
|
+
|
53
|
+
orphan_files = local_files.each_with_object([]) { |x, memo|
|
54
|
+
memo.push x unless remote_files.include? x
|
55
|
+
}
|
56
|
+
orphan_dirs = local_dirs.each_with_object([]) { |x, memo|
|
57
|
+
memo.push x unless remote_dirs.include? x
|
58
|
+
}
|
59
|
+
|
60
|
+
orphan_files.each { |x|
|
61
|
+
FileUtils.rm(cache_path(x), force: true)
|
62
|
+
}
|
63
|
+
orphan_dirs.each { |x|
|
64
|
+
FileUtils.rm(cache_path(x), force: true)
|
65
|
+
}
|
40
66
|
end
|
41
67
|
|
42
|
-
|
43
|
-
filename = Pathname.new(simple_path).basename.to_s
|
44
|
-
local_filepath = proper_path(simple_path)
|
45
|
-
local_path = local_filepath.sub('/' + filename,'')
|
46
|
-
dropbox_filepath = dropbox_path(simple_path)
|
47
|
-
tmp_filepath = make_tmp_path
|
68
|
+
private
|
48
69
|
|
49
|
-
|
50
|
-
|
70
|
+
def aggregate_paths items
|
71
|
+
items.flatten.each_with_object([]) { |x, memo|
|
72
|
+
Dir.glob(cache_path(x)).each { |y|
|
73
|
+
memo.push relative_path(y) if File.file?(y) && !is_thumbnail?(y)
|
74
|
+
}
|
75
|
+
}
|
76
|
+
end
|
51
77
|
|
52
|
-
|
78
|
+
def get_item path
|
79
|
+
return YAML.load_file(path) if is_yaml?(path)
|
80
|
+
return @markdown.render(File.read(path)) if is_markdown?(path)
|
81
|
+
return imagify(path) if is_image?(path)
|
82
|
+
end
|
53
83
|
|
54
|
-
|
55
|
-
|
84
|
+
DPImage = Struct.new(:url, :title, :alt, :thumbnail_s_url, :thumbnail_m_url, :thumbnail_l_url, :thumbnail_xl_url)
|
85
|
+
def imagify relative_path
|
86
|
+
url = public_path(relative_path)
|
87
|
+
|
88
|
+
meta = get_image_meta(relative_path)
|
89
|
+
alt = meta[0]
|
90
|
+
title = meta[1]
|
91
|
+
|
92
|
+
thumbnail_s_url = imagify_suffix url, '_s'
|
93
|
+
thumbnail_m_url = imagify_suffix url, '_m'
|
94
|
+
thumbnail_l_url = imagify_suffix url, '_l'
|
95
|
+
thumbnail_xl_url = imagify_suffix url, '_xl'
|
96
|
+
|
97
|
+
DPImage.new(url,title,alt,thumbnail_s_url,thumbnail_m_url,thumbnail_l_url,thumbnail_xl_url)
|
56
98
|
end
|
57
|
-
end
|
58
99
|
|
59
|
-
|
100
|
+
def imagify_suffix path, suffix
|
101
|
+
File.join(File.dirname(path), insert_into_filename_before_extension(File.basename(path), suffix))
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_image_meta relative_path
|
105
|
+
begin
|
106
|
+
cache_path = cache_path(relative_path)
|
107
|
+
meta_file_path = File.join(File.dirname(cache_path), '_'+File.basename(cache_path,".*")+'.yml')
|
108
|
+
meta = YAML.load_file(meta_file_path)
|
109
|
+
[
|
110
|
+
meta['alt'],
|
111
|
+
meta['title']
|
112
|
+
]
|
113
|
+
rescue
|
114
|
+
[
|
115
|
+
'',
|
116
|
+
''
|
117
|
+
]
|
118
|
+
end
|
119
|
+
end
|
60
120
|
|
61
|
-
def
|
62
|
-
File.
|
63
|
-
FileUtils::mkdir_p local_path unless File.directory? local_path
|
64
|
-
FileUtils.mv tmp_filepath, local_filepath
|
121
|
+
def cache_path relative_path
|
122
|
+
File.join(@settings['cache']['path'], relative_path)
|
65
123
|
end
|
66
124
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
FileUtils.mv tmp_filepath, local_filepath.sub(extension, '_' + size + extension)
|
125
|
+
def relative_path cache_path
|
126
|
+
new_path = cache_path.sub(@settings['cache']['path'], '')
|
127
|
+
new_path[0] = '' if new_path[0] == '/'
|
128
|
+
new_path
|
72
129
|
end
|
73
130
|
|
74
|
-
def
|
75
|
-
|
76
|
-
@settings['dropbox']['path'] + '/' + simple_path
|
131
|
+
def remote_path relative_path
|
132
|
+
File.join(@settings['dropbox']['path'], relative_path)
|
77
133
|
end
|
78
134
|
|
79
|
-
def
|
80
|
-
|
135
|
+
def remote_path_to_relative_path remote_path
|
136
|
+
new_path = remote_path.sub(@settings['dropbox']['path'], '')
|
137
|
+
new_path[0] = '' if new_path[0] == '/'
|
138
|
+
new_path
|
81
139
|
end
|
82
140
|
|
83
|
-
def
|
84
|
-
|
85
|
-
dropbox_lsr(obj['path'], list) if obj['is_dir']
|
86
|
-
list << obj
|
87
|
-
}
|
88
|
-
list
|
141
|
+
def public_path relative_path
|
142
|
+
File.join(@settings['cache']['http_prefix'], relative_path)
|
89
143
|
end
|
90
144
|
|
91
|
-
def
|
92
|
-
|
93
|
-
|
94
|
-
list
|
145
|
+
def insert_into_filename_before_extension(filename, insert)
|
146
|
+
extension = File.extname(filename)
|
147
|
+
filename.reverse.sub(extension.reverse, (insert + extension).reverse).reverse
|
95
148
|
end
|
96
149
|
|
97
|
-
def
|
98
|
-
|
99
|
-
|
100
|
-
|
150
|
+
def dirs_only(paths)
|
151
|
+
paths.each_with_object([]) { |x, memo|
|
152
|
+
memo.push File.dirname(x)
|
153
|
+
memo.delete('.')
|
154
|
+
}.uniq
|
155
|
+
end
|
156
|
+
|
157
|
+
def rev_path relative_path
|
158
|
+
File.join(@settings['rev_cache']['path'], relative_path)
|
159
|
+
end
|
160
|
+
|
161
|
+
def set_rev relative_path, rev
|
162
|
+
rev_path = rev_path(relative_path)
|
163
|
+
rev_dir = File.dirname(rev_path)
|
164
|
+
FileUtils::mkdir_p(rev_dir) unless File.directory?(rev_dir)
|
165
|
+
File.open(rev_path, 'w') { |file| file.write(rev) }
|
101
166
|
end
|
102
167
|
|
103
|
-
def
|
168
|
+
def unset_rev relative_path
|
169
|
+
rev_path = rev_path(relative_path)
|
170
|
+
File.delete(rev_path)
|
171
|
+
end
|
172
|
+
|
173
|
+
def get_rev relative_path
|
104
174
|
begin
|
105
|
-
|
106
|
-
!meta['is_deleted'] && !meta['is_dir']
|
175
|
+
File.read(rev_path(relative_path))
|
107
176
|
rescue
|
177
|
+
return nil
|
108
178
|
end
|
109
179
|
end
|
110
180
|
|
111
|
-
def
|
112
|
-
|
113
|
-
simple_path = simple_path.sub('./','') if simple_path[0,2] == './'
|
114
|
-
simple_path
|
181
|
+
def is_image? filename
|
182
|
+
['.jpg', '.gif', '.png', '.bmp'].any? { |word| filename.end_with?(word) }
|
115
183
|
end
|
116
184
|
|
117
|
-
def
|
118
|
-
|
119
|
-
@settings['cache']['path'] + '/' + simple_path
|
185
|
+
def is_markdown? filename
|
186
|
+
['.md','.markdown'].any? { |word| filename.end_with?(word) }
|
120
187
|
end
|
121
188
|
|
122
|
-
def
|
123
|
-
|
124
|
-
@settings['cache']['public_path'] + '/' + simple_path
|
189
|
+
def is_yaml? filename
|
190
|
+
['.yml','.yaml'].any? { |word| filename.end_with?(word) }
|
125
191
|
end
|
126
192
|
|
127
|
-
def
|
128
|
-
|
193
|
+
def is_thumbnail? path
|
194
|
+
thumbnail_suffixes.any? { |word| File.basename(path, ".*").end_with?(word) }
|
129
195
|
end
|
130
196
|
|
131
|
-
def
|
132
|
-
|
197
|
+
def init_markdown
|
198
|
+
Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
|
133
199
|
end
|
134
200
|
|
135
|
-
def
|
136
|
-
['
|
201
|
+
def init_dropbox
|
202
|
+
DropboxClient.new(@settings['dropbox']['access_token'])
|
137
203
|
end
|
138
204
|
|
139
|
-
def
|
140
|
-
|
205
|
+
def dropbox_list_recursively(root_path, list=[])
|
206
|
+
@dropbox.metadata(root_path)['contents'].each { |obj|
|
207
|
+
dropbox_list_recursively(obj['path'], list) if obj['is_dir']
|
208
|
+
list << obj
|
209
|
+
}
|
210
|
+
list
|
141
211
|
end
|
142
212
|
|
143
|
-
def
|
144
|
-
|
213
|
+
def dropbox_list_dirs_recursively(root_path, list=[])
|
214
|
+
dropbox_list_recursively(root_path).each { |item| list << item['path'].downcase if item['is_dir'] == true }
|
215
|
+
list
|
145
216
|
end
|
146
217
|
|
147
|
-
def
|
148
|
-
['.
|
218
|
+
def dropbox_list_files_recursively(root_path, list=[])
|
219
|
+
dropbox_list_recursively(root_path).each { |item| list << item['path'].downcase if item['is_dir'] == false }
|
220
|
+
list
|
149
221
|
end
|
150
222
|
|
151
|
-
def
|
152
|
-
|
223
|
+
def refresh_item relative_path
|
224
|
+
begin
|
225
|
+
meta = @dropbox.metadata(remote_path(relative_path))
|
226
|
+
rescue
|
227
|
+
return false
|
228
|
+
end
|
229
|
+
|
230
|
+
remote_path = remote_path(relative_path)
|
231
|
+
last_rev = get_rev(relative_path)
|
232
|
+
current_rev = meta['rev']
|
233
|
+
return relative_path if last_rev == current_rev
|
234
|
+
|
235
|
+
dropbox_get(remote_path, current_rev)
|
236
|
+
dropbox_get_thumbnails(remote_path) if is_image?(File.basename(remote_path))
|
237
|
+
relative_path
|
153
238
|
end
|
154
239
|
|
155
|
-
def
|
156
|
-
|
157
|
-
|
158
|
-
|
240
|
+
def dropbox_get remote_path, rev=nil
|
241
|
+
relative_path = remote_path_to_relative_path(remote_path)
|
242
|
+
cache_path = cache_path(relative_path)
|
243
|
+
cache_dir = File.dirname(cache_path)
|
244
|
+
rev_path = rev_path(relative_path)
|
245
|
+
tmp_path = make_tmp_path
|
246
|
+
|
247
|
+
File.open(tmp_path, 'w') { |file| file.write(@dropbox.get_file(remote_path)) }
|
248
|
+
FileUtils::mkdir_p(cache_dir) unless File.directory?(cache_dir)
|
249
|
+
FileUtils.mv tmp_path, cache_path
|
250
|
+
set_rev relative_path, rev if rev
|
159
251
|
end
|
160
252
|
|
161
|
-
def
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
'm' => insert_into_filename_before_extension(p_path, '_m'),
|
169
|
-
'l' => insert_into_filename_before_extension(p_path, '_l'),
|
170
|
-
'xl' => insert_into_filename_before_extension(p_path, '_xl')
|
171
|
-
}
|
253
|
+
def thumbnail_sizes
|
254
|
+
['s','m','l','xl']
|
255
|
+
end
|
256
|
+
|
257
|
+
def thumbnail_suffixes
|
258
|
+
thumbnail_sizes.each_with_object([]) { |x, memo|
|
259
|
+
memo.push '_'+x
|
172
260
|
}
|
173
261
|
end
|
174
262
|
|
175
|
-
def
|
176
|
-
|
177
|
-
|
263
|
+
def dropbox_get_thumbnails(remote_path)
|
264
|
+
relative_path = remote_path_to_relative_path(remote_path)
|
265
|
+
cache_path = cache_path(relative_path)
|
266
|
+
tmp_path = make_tmp_path
|
267
|
+
|
268
|
+
return false unless is_image?(relative_path)
|
269
|
+
|
270
|
+
thumbnail_sizes.each { |size|
|
271
|
+
suffix = '_'+size
|
272
|
+
File.open(tmp_path, 'w') { |file| file.write(@dropbox.thumbnail(remote_path, size)) }
|
273
|
+
FileUtils.mv tmp_path, File.join(File.dirname(cache_path), insert_into_filename_before_extension(File.basename(cache_path), suffix))
|
274
|
+
}
|
275
|
+
end
|
276
|
+
|
277
|
+
def make_tmp_path
|
278
|
+
File.join(@settings['tmp_cache']['path'], Guid.new.to_s)
|
279
|
+
end
|
280
|
+
|
281
|
+
def init_settings arg
|
282
|
+
return YAML.load_file('config/dropboximus_prime.yml') if !arg
|
283
|
+
return YAML.load_file(arg) if arg.is_a? String
|
284
|
+
return arg if arg.is_a? Hash
|
178
285
|
end
|
179
286
|
|
180
287
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dropboximus_prime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Burnette
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dropbox-sdk
|