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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dropboximus_prime.rb +214 -107
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 034be9bebd56a383e503de2785a307857edb77bd
4
- data.tar.gz: c24f1e176e2fd5d8d66ab445b1100a8209c52379
3
+ metadata.gz: 373d813b947c72c161f5e7aa35dc49c90792adb6
4
+ data.tar.gz: 1b438d41dd8bc92b2cf83ab2a28861e9baca66c5
5
5
  SHA512:
6
- metadata.gz: 471f56380be4cfd1025f84b2ce548ae0a64639ffddde1a42bf7079edddfba34319350b168d553ed7607919336f32fc488f8740d6825d89554fbbc5a55a61581e
7
- data.tar.gz: 9c649d9b97b8a22fc7a26a637a9d3f267867dd5827ff6ba282a821140e9fb23fd10772e3ea401f01fe06db334f95ca4baa3abc990b932f0f30fab9f985213221
6
+ metadata.gz: 2c0ef5a55b11611cded0a493416b13e3d1ecf4212fe1ddb44cb9325891978369fff2e42d6e2522526418336d36c0ff90699b27cb993550b68c4e0f17bae6d5f4
7
+ data.tar.gz: a74983230dc7989e099eeac90ff5bb8f7faa726bdca5702bb3ff0df9c2a69f9208baf0028348d448ab814003a2dafab8ba498acc09ea75e47ae38af86610b30e
@@ -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 settings=false
13
- @settings = !settings ? YAML.load_file('config/dropboximus_prime_settings.yml') : settings
14
- @dropbox = DropboxClient.new(@settings['dropbox']['access_token'])
15
- @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
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
- gotten = []
20
- items.each { |item| gotten << get_item(item) }
21
- gotten.length == 1 ? gotten.first : gotten
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 get_item simple_path
25
- filepath = proper_path(simple_path)
26
-
27
- return false unless File.exist?(filepath)
24
+ def get_one item
25
+ get(item).values[0]
26
+ end
28
27
 
29
- item = File.read(filepath)
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
- return YAML.load(item) if is_yaml?(filepath)
32
- return @markdown.render(item) if is_markdown?(filepath)
33
- return image_info(simple_path) if is_image?(filepath)
34
- item
37
+ local_paths.each { |item|
38
+ refresh_item(item)
39
+ }
35
40
  end
36
41
 
37
- def refresh! *items
38
- items = dropbox_lsr_f(@settings['dropbox']['path']) unless items.length > 0
39
- items.each { |i| refresh_item!(dropbox_path_to_simple_path(i), @settings['cache']['timeout']) }
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
- def refresh_item! simple_path, cache_timeout=nil
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
- return false unless item_stale?(local_filepath, cache_timeout)
50
- return false unless dropbox_file_exists?(dropbox_filepath)
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
- store(dropbox_filepath, local_filepath, local_path, tmp_filepath)
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
- if is_image? filename
55
- dropbox_thumbnail_sizes.each { |size| store_thumbnail(dropbox_filepath, local_filepath, local_path, tmp_filepath, size) }
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
- private
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 store(dropbox_filepath, local_filepath, local_path, tmp_filepath)
62
- File.open(tmp_filepath, 'w') { |file| file.write(@dropbox.get_file(dropbox_filepath)) }
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 store_thumbnail(dropbox_filepath, local_filepath, local_path, tmp_filepath, size)
68
- extension = File.extname(local_filepath)
69
- File.open(tmp_filepath, 'w') { |file| file.write(@dropbox.thumbnail(dropbox_filepath, size)) }
70
- FileUtils::mkdir_p local_path unless File.directory? local_path
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 dropbox_path simple_path
75
- simple_path = strip_initial_path(simple_path)
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 dropbox_path_to_simple_path dropbox_path
80
- dropbox_path.downcase.sub(@settings['dropbox']['path'].downcase + '/', '')
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 dropbox_lsr(root_path, list=[])
84
- @dropbox.metadata(root_path)['contents'].each { |obj|
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 dropbox_lsr_d root_path
92
- list = []
93
- dropbox_lsr(root_path).each { |item| list << item['path'].downcase if item['is_dir'] == true }
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 dropbox_lsr_f root_path
98
- list = []
99
- dropbox_lsr(root_path).each { |item| list << item['path'].downcase if item['is_dir'] == false }
100
- list
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 dropbox_file_exists? path
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
- meta = @dropbox.metadata path
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 strip_initial_path simple_path
112
- simple_path = simple_path.sub('/','') if simple_path[0,1] == '/'
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 private_path simple_path
118
- simple_path = strip_initial_path(simple_path)
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 public_path simple_path
123
- simple_path = strip_initial_path(simple_path)
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 proper_path simple_path
128
- public_file?(simple_path) ? public_path(simple_path) : private_path(simple_path)
193
+ def is_thumbnail? path
194
+ thumbnail_suffixes.any? { |word| File.basename(path, ".*").end_with?(word) }
129
195
  end
130
196
 
131
- def make_tmp_path
132
- @settings['cache']['tmp_path'] + '/' + Guid.new.to_s
197
+ def init_markdown
198
+ Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
133
199
  end
134
200
 
135
- def dropbox_thumbnail_sizes
136
- ['s', 'm', 'l', 'xl']
201
+ def init_dropbox
202
+ DropboxClient.new(@settings['dropbox']['access_token'])
137
203
  end
138
204
 
139
- def public_file? filepath
140
- Pathname.new(filepath).basename.to_s.index('_') != 0
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 is_image? filename
144
- ['.jpg', '.gif', '.png', '.bmp'].any? { |word| filename.include?(word) }
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 is_markdown? filename
148
- ['.md','.markdown'].any? { |word| filename.include?(word) }
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 is_yaml? filename
152
- ['.yml','.yaml'].any? { |word| filename.include?(word) }
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 item_stale? filepath, cache_timeout=nil
156
- return true unless File.exist? filepath
157
- cache_timeout ||= @settings['cache']['timeout']
158
- (Time.now - File.stat(filepath).mtime).to_i > cache_timeout
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 image_info simple_path
162
- p_path = @settings['cache']['public_prefix'] + simple_path
163
-
164
- {
165
- 'public_path' => p_path,
166
- 'thumbnails' => {
167
- 's' => insert_into_filename_before_extension(p_path, '_s'),
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 insert_into_filename_before_extension(filename, insert)
176
- extension = File.extname(filename)
177
- filename.reverse.sub(extension.reverse, (insert + extension).reverse).reverse
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.3
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-07-28 00:00:00.000000000 Z
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dropbox-sdk