obf 0.9.8.8 → 0.9.8.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/obf/external.rb +103 -72
  3. data/lib/obf/pdf.rb +14 -10
  4. data/lib/obf/utils.rb +32 -10
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2ed2e2f90f78fab2afac2c3d1f52af1c8b7345e7cb3d0fa6434832899b62a05
4
- data.tar.gz: cd9e8521d222e51a9fe6ea760134465a93e2bb05b9c9ee03252bd57e986890ca
3
+ metadata.gz: 6de44b1031f93761c343d16237a52774bb7eac26115059362c67adbb7e29fcf9
4
+ data.tar.gz: c3f9bb83b70af873f954df14810ce63c7ef164eeb3b9fcf5c0766370432edfd5
5
5
  SHA512:
6
- metadata.gz: 02ef108ce774799e4242d1818104d12ae2fcb9c725ef05218bc55b8784330adb458a2d89a1341a2cf7abdd90c61ce2d7374297155151271a9730777a1af5e195
7
- data.tar.gz: 56c075245f5cb0421b556c2618ca2dbe81ffda55d199e0f73787cdc34c0df4c942ae1419379babbeafee64dd088a09ea38b4311423c00a24a50dfb3a9212a34f
6
+ metadata.gz: a59b8d5ad10d2d11aeea4d62c5877f66adba5ce1e5366c1d9db63c49d9a76a54f121d485a33b3f2043895b2520643d04167c723b57f8373953a8fad575afce93
7
+ data.tar.gz: 9e484cc271472db85ba86f2e5a78d6271b1168175bbdfc2b9d24c2f09bc8ff25403ff45a3424f0e1e6c17a8fb670783b63c105f39404ee324e8d19ad3b9164c9
@@ -1,5 +1,6 @@
1
1
  module OBF::External
2
- def self.to_obf(hash, dest_path, path_hash=nil)
2
+ def self.to_obf(hash, dest_path, path_hash=nil, to_include=nil)
3
+ to_include ||= {images: true, sounds: true}
3
4
  if hash['boards']
4
5
  old_hash = hash
5
6
  hash = old_hash['boards'][0]
@@ -17,6 +18,7 @@ module OBF::External
17
18
  res['background'] = hash['background']
18
19
  res['url'] = hash['url']
19
20
  res['data_url'] = hash['data_url']
21
+ OBF::Utils.log("compressing board #{res['name'] || res['id']}")
20
22
 
21
23
  res['default_locale'] = hash['default_locale'] if hash['default_locale']
22
24
  res['label_locale'] = hash['label_locale'] if hash['label_locale']
@@ -114,87 +116,114 @@ module OBF::External
114
116
  OBF::Utils.update_current_progress(idx.to_f / button_count.to_f)
115
117
  end
116
118
 
117
- images.each do |original_image|
118
- image = {
119
- 'id' => original_image['id'],
120
- 'width' => original_image['width'],
121
- 'height' => original_image['height'],
122
- 'license' => OBF::Utils.parse_license(original_image['license']),
123
- 'url' => original_image['url'],
124
- 'data' => original_image['data'],
125
- 'data_url' => original_image['data_url'],
126
- 'content_type' => original_image['content_type']
127
- }
128
- if !path_hash
129
- image['data'] ||= OBF::Utils.image_base64(image['url']) if image['url']
130
- if image['data'] && (!image['content_type'] || !image['width'] || !image['height'])
131
- attrs = OBF::Utils.image_attrs(image['data'])
132
- image['content_type'] ||= attrs['content_type']
133
- image['width'] ||= attrs['width']
134
- image['height'] ||= attrs['height']
119
+ if to_include[:images]
120
+ hydra = Typhoeus::Hydra.hydra
121
+ grabs = []
122
+ images.each do |img|
123
+ if path_hash && path_hash['images'] && path_hash['images'][img['id']]
124
+ elsif img['url'] && !img['data']
125
+ got_url = OBF::Utils.get_url(img['url'], true)
126
+ if got_url['request']
127
+ hydra.queue(got_url['request'])
128
+ grabs << {req: got_url['request'], img: img, res: got_url}
129
+ end
135
130
  end
136
- else
137
- if path_hash['images'] && path_hash['images'][image['id']]
138
- image['path'] = path_hash['images'][image['id']]['path']
139
- image['content_type'] ||= path_hash['images'][image['id']]['content_type']
140
- image['width'] ||= path_hash['images'][image['id']]['width']
141
- image['height'] ||= path_hash['images'][image['id']]['height']
131
+ end
132
+ hydra.run
133
+ grabs.each do |grab|
134
+ str = "data:" + grab[:res]['content_type']
135
+ str += ";base64," + Base64.strict_encode64(grab[:res]['data'])
136
+ grab[:img]['data'] = str
137
+ grab[:img]['content_type'] ||= grab[:res]['content_type']
138
+ end
139
+
140
+ images.each do |original_image|
141
+ image = {
142
+ 'id' => original_image['id'],
143
+ 'width' => original_image['width'],
144
+ 'height' => original_image['height'],
145
+ 'license' => OBF::Utils.parse_license(original_image['license']),
146
+ 'url' => original_image['url'],
147
+ 'data' => original_image['data'],
148
+ 'data_url' => original_image['data_url'],
149
+ 'content_type' => original_image['content_type']
150
+ }
151
+ if !path_hash
152
+ # not zipping
153
+ image['data'] ||= OBF::Utils.image_base64(image['url']) if image['url']
154
+ if image['data'] && (!image['content_type'] || !image['width'] || !image['height'])
155
+ attrs = OBF::Utils.image_attrs(image['data'])
156
+ image['content_type'] ||= attrs['content_type']
157
+ image['width'] ||= attrs['width']
158
+ image['height'] ||= attrs['height']
159
+ end
142
160
  else
143
- image_fetch = OBF::Utils.image_raw(image['data'] || image['url'])
144
- if image_fetch
145
- if !image['content_type'] || !image['width'] || !image['height']
146
- attrs = OBF::Utils.image_attrs(image_fetch['data'])
147
- image['content_type'] ||= image_fetch['content_type'] || attrs['content_type']
148
- image['width'] ||= attrs['width']
149
- image['height'] ||= attrs['height']
161
+ # zipping, so needs to be downloaded and potentially re-used
162
+ if path_hash['images'] && path_hash['images'][image['id']]
163
+ image['path'] = path_hash['images'][image['id']]['path']
164
+ image['content_type'] ||= path_hash['images'][image['id']]['content_type']
165
+ image['width'] ||= path_hash['images'][image['id']]['width']
166
+ image['height'] ||= path_hash['images'][image['id']]['height']
167
+ else
168
+ image_fetch = OBF::Utils.image_raw(image['data'] || image['url'])
169
+ if image_fetch
170
+ if !image['content_type'] || !image['width'] || !image['height']
171
+ attrs = OBF::Utils.image_attrs(image_fetch['data'])
172
+ image['content_type'] ||= image_fetch['content_type'] || attrs['content_type']
173
+ image['width'] ||= attrs['width']
174
+ image['height'] ||= attrs['height']
175
+ end
176
+ zip_path = "images/image_#{image['id']}#{image_fetch['extension']}"
177
+ path_hash['images'] ||= {}
178
+ path_hash['images'][image['id']] = {
179
+ 'path' => zip_path,
180
+ 'content_type' => image['content_type'],
181
+ 'width' => image['width'],
182
+ 'height' => image['height']
183
+ }
184
+ path_hash['zip'].add(zip_path, image_fetch['data'])
185
+ image['path'] = zip_path
186
+ image.delete('data')
150
187
  end
151
- zip_path = "images/image_#{image['id']}#{image_fetch['extension']}"
152
- path_hash['images'] ||= {}
153
- path_hash['images'][image['id']] = {
154
- 'path' => zip_path,
155
- 'content_type' => image['content_type'],
156
- 'width' => image['width'],
157
- 'height' => image['height']
158
- }
159
- path_hash['zip'].add(zip_path, image_fetch['data'])
160
- image['path'] = zip_path
161
188
  end
162
189
  end
190
+ res['images'] << trim_empties(image)
163
191
  end
164
- res['images'] << trim_empties(image)
165
192
  end
166
193
 
167
- sounds.each do |original_sound|
168
- sound = {
169
- 'id' => original_sound['id'],
170
- 'duration' => original_sound['duration'],
171
- 'license' => OBF::Utils.parse_license(original_sound['license']),
172
- 'url' => original_sound['url'],
173
- 'data' => original_sound['data'],
174
- 'data_url' => original_sound['data_url'],
175
- 'content_type' => original_sound['content_type']
176
- }
177
- if !path_hash
178
- sound['data'] = OBF::Utils.sound_base64(sound['url']) if sound['url']
179
- else
180
- if path_hash['sounds'] && path_hash['sounds'][sound['id']]
181
- sound['path'] = path_hash['sounds'][sound['id']]['path']
194
+ if to_include[:sounds]
195
+ sounds.each do |original_sound|
196
+ sound = {
197
+ 'id' => original_sound['id'],
198
+ 'duration' => original_sound['duration'],
199
+ 'license' => OBF::Utils.parse_license(original_sound['license']),
200
+ 'url' => original_sound['url'],
201
+ 'data' => original_sound['data'],
202
+ 'data_url' => original_sound['data_url'],
203
+ 'content_type' => original_sound['content_type']
204
+ }
205
+ if !path_hash
206
+ sound['data'] = OBF::Utils.sound_base64(sound['url']) if sound['url']
182
207
  else
183
- sound_fetch = OBF::Utils.sound_raw(sound['url'] || sound['data'])
184
- if sound_fetch
185
- zip_path = "sounds/sound_#{sound['id']}#{sound_fetch['extension']}"
186
- path_hash['sounds'] ||= {}
187
- path_hash['sounds'][sound['id']] = {
188
- 'path' => zip_path
189
- }
190
- path_hash['zip'].add(zip_path, sound_fetch['data'])
191
- sound['path'] = zip_path
208
+ if path_hash['sounds'] && path_hash['sounds'][sound['id']]
209
+ sound['path'] = path_hash['sounds'][sound['id']]['path']
210
+ else
211
+ sound_fetch = OBF::Utils.sound_raw(sound['url'] || sound['data'])
212
+ if sound_fetch
213
+ zip_path = "sounds/sound_#{sound['id']}#{sound_fetch['extension']}"
214
+ path_hash['sounds'] ||= {}
215
+ path_hash['sounds'][sound['id']] = {
216
+ 'path' => zip_path
217
+ }
218
+ path_hash['zip'].add(zip_path, sound_fetch['data'])
219
+ sound['path'] = zip_path
220
+ end
192
221
  end
222
+ sound['path'] = zip_path
193
223
  end
194
- sound['path'] = zip_path
224
+
225
+ res['sounds'] << trim_empties(sound)
195
226
  end
196
-
197
- res['sounds'] << trim_empties(sound)
198
227
  end
199
228
 
200
229
  res['grid'] = OBF::Utils.parse_grid(hash['grid']) # TODO: more robust parsing here
@@ -208,6 +237,7 @@ module OBF::External
208
237
  else
209
238
  File.open(dest_path, 'w') {|f| f.write(JSON.pretty_generate(res)) }
210
239
  end
240
+ OBF::Utils.log(" done compressing board #{res['name'] || res['id']}")
211
241
  return dest_path
212
242
  end
213
243
 
@@ -274,7 +304,7 @@ module OBF::External
274
304
  if b
275
305
  b['images'] = content['images'] || []
276
306
  b['sounds'] = content['sounds'] || []
277
- to_obf(b, nil, paths)
307
+ to_obf(b, nil, paths, opts[:to_include])
278
308
  end
279
309
  end
280
310
  manifest = {
@@ -355,6 +385,7 @@ module OBF::External
355
385
  tmp_path = OBF::Utils.temp_path("stash")
356
386
  if opts['packet']
357
387
  OBF::Utils.as_progress_percent(0, 0.3) do
388
+ opts[:to_include] = {images: true}
358
389
  OBF::External.to_obz(board, tmp_path, opts)
359
390
  end
360
391
  OBF::Utils.as_progress_percent(0.3, 1.0) do
@@ -362,7 +393,7 @@ module OBF::External
362
393
  end
363
394
  else
364
395
  OBF::Utils.as_progress_percent(0, 0.5) do
365
- self.to_obf(board, tmp_path)
396
+ self.to_obf(board, tmp_path, {images: true})
366
397
  end
367
398
  OBF::Utils.as_progress_percent(0.5, 1.0) do
368
399
  OBF::OBF.to_pdf(tmp_path, dest_path, opts)
@@ -190,21 +190,25 @@ module OBF::PDF
190
190
  grabs = []
191
191
  obj['buttons'].each do |btn|
192
192
  image = (obj['images_hash'] || {})[btn['image_id']]
193
- if image && image['url'] && !image['data'] && !(image['path'] && optionns['zipper'])
193
+ if image && image['url'] && !image['data'] && !(image['path'] && options['zipper'])
194
194
  url = image['url']
195
- uri = url.match(/\%/) ? url : URI.escape(url)
196
- uri = OBF::Utils.sanitize_url(uri)
197
- req = Typhoeus::Request.new(uri, followlocation: true)
198
- hydra.queue(req)
199
- grabs << {url: url, req: req, image: image, fill: btn['background_color'] ? OBF::Utils.fix_color(btn['background_color'], 'hex') : "ffffff"}
195
+ res = OBF::Utils.get_url(url, true)
196
+ if res['request']
197
+ hydra.queue(res['request'])
198
+ grabs << {url: url, req: res['request'], image: image, fill: btn['background_color'] ? OBF::Utils.fix_color(btn['background_color'], 'hex') : "ffffff"}
199
+ end
200
+ elsif image && image['data'] && !(image['path'] && options['zipper'])
201
+ grabs << {image: image, fill: btn['background_color'] ? OBF::Utils.fix_color(btn['background_color'], 'hex') : "ffffff"}
200
202
  end
201
203
  end
202
204
  hydra.run
203
205
  threads = []
204
206
  grabs.each do |grab|
205
- grab[:image]['raw_data'] = grab[:req].response.body
207
+ if grab[:req]
208
+ grab[:image]['raw_data'] = grab[:req].response.body
209
+ grab[:image]['content_type'] ||= grab[:req].response.headers['Content-Type'] if grab[:req].response.headers['Content-Type']
210
+ end
206
211
  grab[:image]['threadable'] = true
207
- grab[:image]['content_type'] = grab[:req].response.headers['Content-Type'] if grab[:req].response.headers['Content-Type']
208
212
  bg = 'white'
209
213
  if options['transparent_background'] || options['symbol_background'] == 'transparent'
210
214
  bg = "\##{grab[:fill]}"
@@ -212,10 +216,10 @@ module OBF::PDF
212
216
  bg = 'black'
213
217
  end
214
218
  res = OBF::Utils.save_image(grab[:image], options['zipper'], bg)
215
- threads << res unless res.is_a?(String)
219
+ threads << res if res && !res.is_a?(String)
216
220
  end
217
221
  threads.each{|t| t.join }
218
- grabs.each{|g| g[:image]['threadable'] = false }
222
+ grabs.each{|g| g[:image].delete('threadable') }
219
223
  OBF::Utils.log " done with #{grabs.length} remote images!"
220
224
 
221
225
  obj['grid']['order'].each_with_index do |buttons, row|
@@ -1,7 +1,8 @@
1
1
  require 'cfpropertylist'
2
2
  module OBF::Utils
3
- def self.get_url(url)
3
+ def self.get_url(url, hydra_wait=false)
4
4
  return nil unless url
5
+ res = {}
5
6
  content_type = nil
6
7
  data = nil
7
8
  if url.match(/^data:/)
@@ -10,10 +11,36 @@ module OBF::Utils
10
11
  else
11
12
  uri = url.match(/\%/) ? url : URI.escape(url)
12
13
  uri = self.sanitize_url(uri)
13
- res = Typhoeus.get(uri, followlocation: true)
14
- content_type = res.headers['Content-Type']
15
- data = res.body
14
+ if hydra_wait
15
+ req = Typhoeus::Request.new(uri, followlocation: true)
16
+ req.on_complete do |response|
17
+ res.delete('request')
18
+ res['content_type'] = response.headers['Content-Type']
19
+ res['data'] = response.body
20
+ res['extension'] = extension_for(res['content_type'])
21
+ end
22
+ res['request'] = req
23
+ else
24
+ req = Typhoeus.get(uri, followlocation: true)
25
+ content_type = req.headers['Content-Type']
26
+ data = req.body
27
+ end
28
+ end
29
+ res['content_type'] = content_type
30
+ res['data'] = data
31
+ res['extension'] = extension_for(content_type)
32
+ if res['request']
33
+ if hydra_wait
34
+ # do nothing
35
+ else
36
+ res['request'].run
37
+ res.delete('request')
38
+ end
16
39
  end
40
+ res
41
+ end
42
+
43
+ def self.extension_for(content_type)
17
44
  type = MIME::Types[content_type]
18
45
  type = type && type[0]
19
46
  extension = ""
@@ -22,12 +49,7 @@ module OBF::Utils
22
49
  elsif type.respond_to?(:extensions)
23
50
  extension = ("." + type.extensions[0]) if type && type.extensions && type.extensions.length > 0
24
51
  end
25
- res = {
26
- 'content_type' => content_type,
27
- 'data' => data,
28
- 'extension' => extension
29
- }
30
- res
52
+ extension
31
53
  end
32
54
 
33
55
  def self.sanitize_url(url)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8.8
4
+ version: 0.9.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Whitmer