dropboximus_prime 0.1.0

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 +7 -0
  2. data/lib/dropboximus_prime.rb +181 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5532f0bc696c5c601838a3d6ce9d3b6ada885eba
4
+ data.tar.gz: 76ab84b5a565016c2a7ecf56c920a3ae0aabdfa6
5
+ SHA512:
6
+ metadata.gz: 4aaab11bab033a435a07112a2c6e8a70b918506cbffebe4058626509e9ee0b1e97cd19d80a2783fe8f4447882af5bfc63b303584420ae8a4cfefa16249ff02d1
7
+ data.tar.gz: 4bf2ecba2e7e8c67d4ff938cfca20355c6018b32d23d8b64bc4d6887d68ee1a35b814cad286b391f2172a2161fd14b74973e4d3e5d9663e62ce3a68a1fb43180
@@ -0,0 +1,181 @@
1
+ require 'delegate'
2
+ require 'yaml'
3
+ require 'dropbox_sdk'
4
+ require 'redcarpet'
5
+ require 'guid'
6
+ require 'pathname'
7
+ require 'fileutils'
8
+
9
+ class DropboximusPrime
10
+ attr_accessor :settings
11
+
12
+ def initialize settings=false
13
+ @settings = !settings ? YAML.load_file('config/drizzle_settings.yml') : settings
14
+ @dropbox = DropboxClient.new(@settings['dropbox']['access_token'])
15
+ @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
16
+ end
17
+
18
+ def get *items
19
+ gotten = []
20
+ items.each { |item| gotten << get_item(item) }
21
+ gotten.length == 1 ? gotten.first : gotten
22
+ end
23
+
24
+ def get_item simple_path
25
+ filepath = proper_path(simple_path)
26
+ puts filepath
27
+
28
+ return false unless File.exist?(filepath)
29
+
30
+ item = File.read(filepath)
31
+
32
+ return YAML.load(item) if is_yaml?(filepath)
33
+ return @markdown.render(item) if is_markdown?(filepath)
34
+ return image_info(simple_path) if is_image?(filepath)
35
+ item
36
+ end
37
+
38
+ def refresh! *items
39
+ items = dropbox_lsr_f(@settings['dropbox']['path']) unless items.length > 0
40
+ items.each { |i| refresh_item!(dropbox_path_to_simple_path(i), @settings['cache']['timeout']) }
41
+ end
42
+
43
+ def refresh_item! simple_path, cache_timeout=nil
44
+ filename = Pathname.new(simple_path).basename.to_s
45
+ local_filepath = proper_path(simple_path)
46
+ local_path = local_filepath.sub('/' + filename,'')
47
+ dropbox_filepath = dropbox_path(simple_path)
48
+ tmp_filepath = make_tmp_path
49
+
50
+ return false unless item_stale?(local_filepath, cache_timeout)
51
+ return false unless dropbox_file_exists?(dropbox_filepath)
52
+
53
+ store(dropbox_filepath, local_filepath, local_path, tmp_filepath)
54
+
55
+ if is_image? filename
56
+ dropbox_thumbnail_sizes.each { |size| store_thumbnail(dropbox_filepath, local_filepath, local_path, tmp_filepath, size) }
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def store(dropbox_filepath, local_filepath, local_path, tmp_filepath)
63
+ File.open(tmp_filepath, 'w') { |file| file.write(@dropbox.get_file(dropbox_filepath)) }
64
+ FileUtils::mkdir_p local_path unless File.directory? local_path
65
+ FileUtils.mv tmp_filepath, local_filepath
66
+ end
67
+
68
+ def store_thumbnail(dropbox_filepath, local_filepath, local_path, tmp_filepath, size)
69
+ extension = File.extname(local_filepath)
70
+ File.open(tmp_filepath, 'w') { |file| file.write(@dropbox.thumbnail(dropbox_filepath, size)) }
71
+ FileUtils::mkdir_p local_path unless File.directory? local_path
72
+ FileUtils.mv tmp_filepath, local_filepath.sub(extension, '_' + size + extension)
73
+ end
74
+
75
+ def dropbox_path simple_path
76
+ simple_path = strip_initial_path(simple_path)
77
+ @settings['dropbox']['path'] + '/' + simple_path
78
+ end
79
+
80
+ def dropbox_path_to_simple_path dropbox_path
81
+ dropbox_path.downcase.sub(@settings['dropbox']['path'].downcase + '/', '')
82
+ end
83
+
84
+ def dropbox_lsr(root_path, list=[])
85
+ @dropbox.metadata(root_path)['contents'].each { |obj|
86
+ dropbox_lsr(obj['path'], list) if obj['is_dir']
87
+ list << obj
88
+ }
89
+ list
90
+ end
91
+
92
+ def dropbox_lsr_d root_path
93
+ list = []
94
+ dropbox_lsr(root_path).each { |item| list << item['path'].downcase if item['is_dir'] == true }
95
+ list
96
+ end
97
+
98
+ def dropbox_lsr_f root_path
99
+ list = []
100
+ dropbox_lsr(root_path).each { |item| list << item['path'].downcase if item['is_dir'] == false }
101
+ list
102
+ end
103
+
104
+ def dropbox_file_exists? path
105
+ begin
106
+ meta = @dropbox.metadata path
107
+ !meta['is_deleted'] && !meta['is_dir']
108
+ rescue
109
+ end
110
+ end
111
+
112
+ def strip_initial_path simple_path
113
+ simple_path = simple_path.sub('/','') if simple_path[0,1] == '/'
114
+ simple_path = simple_path.sub('./','') if simple_path[0,2] == './'
115
+ simple_path
116
+ end
117
+
118
+ def private_path simple_path
119
+ simple_path = strip_initial_path(simple_path)
120
+ @settings['cache']['path'] + '/' + simple_path
121
+ end
122
+
123
+ def public_path simple_path
124
+ simple_path = strip_initial_path(simple_path)
125
+ @settings['cache']['public_path'] + '/' + simple_path
126
+ end
127
+
128
+ def proper_path simple_path
129
+ public_file?(simple_path) ? public_path(simple_path) : private_path(simple_path)
130
+ end
131
+
132
+ def make_tmp_path
133
+ @settings['cache']['tmp_path'] + '/' + Guid.new.to_s
134
+ end
135
+
136
+ def dropbox_thumbnail_sizes
137
+ ['s', 'm', 'l', 'xl']
138
+ end
139
+
140
+ def public_file? filepath
141
+ Pathname.new(filepath).basename.to_s.index('_') != 0
142
+ end
143
+
144
+ def is_image? filename
145
+ ['.jpg', '.gif', '.png', '.bmp'].any? { |word| filename.include?(word) }
146
+ end
147
+
148
+ def is_markdown? filename
149
+ ['.md','.markdown'].any? { |word| filename.include?(word) }
150
+ end
151
+
152
+ def is_yaml? filename
153
+ ['.yml','.yaml'].any? { |word| filename.include?(word) }
154
+ end
155
+
156
+ def item_stale? filepath, cache_timeout=nil
157
+ return true unless File.exist? filepath
158
+ cache_timeout ||= @settings['cache']['timeout']
159
+ (Time.now - File.stat(filepath).mtime).to_i > cache_timeout
160
+ end
161
+
162
+ def image_info simple_path
163
+ p_path = @settings['cache']['public_prefix'] + simple_path
164
+
165
+ {
166
+ 'public_path' => p_path,
167
+ 'thumbnails' => {
168
+ 's' => insert_into_filename_before_extension(p_path, '_s'),
169
+ 'm' => insert_into_filename_before_extension(p_path, '_m'),
170
+ 'l' => insert_into_filename_before_extension(p_path, '_l'),
171
+ 'xl' => insert_into_filename_before_extension(p_path, '_xl')
172
+ }
173
+ }
174
+ end
175
+
176
+ def insert_into_filename_before_extension(filename, insert)
177
+ extension = File.extname(filename)
178
+ filename.reverse.sub(extension.reverse, (insert + extension).reverse).reverse
179
+ end
180
+
181
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dropboximus_prime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Burnette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Dropboximus Prime is a content cacher and processor written for Dropbox
14
+ in Ruby.
15
+ email: ryan.burnette@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dropboximus_prime.rb
21
+ homepage: http://ryanburnette.com/dropboximus-prime
22
+ licenses:
23
+ - Apache2
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Dropboximus Prime is a content cacher and processor written for Dropbox in
45
+ Ruby.
46
+ test_files: []