zerofetcher 0.0.52 → 0.0.53

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.
data/lib/zerofetcher.rb CHANGED
@@ -1,729 +1,729 @@
1
- # Libs
2
- require 'yaml'
3
- require 'net/http'
4
- require 'json'
5
- require 'fileutils'
6
- require 'pathname'
7
- require 'open-uri'
8
- require 'logger'
9
- require 'image_optimizer'
10
-
11
- require 'code/JekyllFile.rb'
12
- require 'code/JekyllPost.rb'
13
-
14
- class ZeroFetcher
15
- @@logger
16
- @@jekyll_path
17
- @@image_variants
18
- @@end_point
19
- @@site_id
20
-
21
- def self.run
22
- # Paths
23
- app_path = File.dirname(__FILE__)
24
- jekyll_path = Dir.pwd
25
- @@jekyll_path = jekyll_path
26
-
27
- gitignores = [
28
- '_logs',
29
- '_posts',
30
- '_includes/pages',
31
- '_includes/content_blocks',
32
- ]
33
-
34
- # App Requires
35
- FileUtils::mkdir_p jekyll_path+'/_logs'
36
- FileUtils::mkdir_p jekyll_path+'/_data'
37
- FileUtils::mkdir_p jekyll_path+'/_posts'
38
- FileUtils::mkdir_p jekyll_path+'/assets'
39
- FileUtils::mkdir_p jekyll_path+'/assets/images'
40
- FileUtils::mkdir_p jekyll_path+'/assets/images/pages'
41
- FileUtils::mkdir_p jekyll_path+'/assets/files'
42
- FileUtils::mkdir_p jekyll_path+'/assets/images/tax-images'
43
- FileUtils::mkdir_p jekyll_path+'/_includes'
44
- FileUtils::mkdir_p jekyll_path+'/_includes/pages'
45
- FileUtils::mkdir_p jekyll_path+'/_includes/content_blocks'
46
-
47
- # Setup Logger
48
- @@logger = Logger.new(jekyll_path + '/_logs/logfile.log', 10, 1024000)
49
-
50
- if !Pathname.new(jekyll_path+"/_config.yml").file?
51
- self.log('_config.yml not found')
52
- abort('_config.yml not found')
53
- end
54
-
55
- config = YAML.load_file(jekyll_path+"/_config.yml")
56
-
57
- if !config.has_key?('fetcher')
58
- self.log('No Fetcher Info Found')
59
- abort('No Fetcher Info Found')
60
- end
61
-
62
- end_point = config['fetcher']['end_point']
63
- @@end_point = end_point
64
- site_id = config['fetcher']['site_id'].to_s
65
- @@site_id = site_id;
66
-
67
- # Load data from API
68
- data = self.loadDataFromApi(end_point, config['fetcher']['api_key'], site_id)
69
-
70
- if data == false
71
- return
72
- end
73
-
74
- # Create empty collection hash
75
- collection_config = {};
76
-
77
- # Site Info
78
- if data.key?("site")
79
- File.write(jekyll_path+'/_data/site.json', JSON.pretty_generate(data['site']))
80
- self.log('Writing File /_data/site.json')
81
- end
82
-
83
- # write .htaccess file
84
- htaccess = '<IfModule mod_headers.c>
85
- <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
86
- Header set Access-Control-Allow-Origin "*"
87
- </FilesMatch>
88
- </IfModule>
89
- RewriteEngine On
90
- Redirect 301 /d3panel '+end_point+'/d3panel?sitekey='+data['site']['key']+'
91
- ErrorDocument 404 /404/index.html'
92
- File.write(jekyll_path+'/.htaccess', htaccess)
93
-
94
- # Image Variants
95
- @@image_variants = data['image_variants']
96
-
97
- # Pages
98
- if data.key?("pages")
99
- pages_files_saved = Array.new
100
-
101
- pages_folder = jekyll_path+'/_includes/pages'
102
-
103
- puts 'Pages - ' + data['pages'].length.to_s
104
- self.log('Pages - ' + data['pages'].length.to_s)
105
-
106
- # array to store hash info on all pages
107
- pages = Array.new
108
-
109
- # Loop through each page
110
- data['pages'].each do |page|
111
- puts ' - ' + page['name']
112
- self.log(' - ' + page['name'])
113
-
114
- jpage = JekyllFile.new(jekyll_path, page, 'page')
115
-
116
- jpage.saveContentFile(pages_folder+'/'+page['url']+'.md', page['content'])
117
- jpage.savePageFile
118
- #pages_files_saved.push(jpage.getFileName)
119
- pages_files_saved.push( Pathname.new( jpage.getFileName ).basename.to_s )
120
- self.log(" - Saved:"+jpage.getFileName)
121
-
122
- # Add to pages array
123
- page_info = {
124
- 'id' => page['id'],
125
- 'name' => page['name'],
126
- 'slug' => page['slug'],
127
- 'url' => page['url'],
128
- 'layout' => page['layout'],
129
- 'meta_title' => (page['meta_title']) ? page['meta_title'] : page['name'],
130
- 'meta_description' => page['meta_description'],
131
- 'short_description' => page['short_description'],
132
- 'parent_id' => page['parent_id'],
133
- }
134
- if page['image']
135
- page_info['image'] = 'images/pages/' + page['image']
136
- end
137
- pages.push(page_info)
138
-
139
- # save image
140
- if page['image']
141
- page['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/pages/' + page['image'], jekyll_path+'/assets/images/pages/' + page['image'])
142
- end
143
- end
144
-
145
- File.write(jekyll_path+'/_data/pages.json', JSON.pretty_generate(pages))
146
- self.log("Writing File /_data/pages.json")
147
-
148
- # Clean Unused Pages
149
- existing_pages_files = Dir.glob(File.join(jekyll_path, "*.md"))
150
-
151
- existing_pages_files.each do |file|
152
- file_basename = Pathname.new(file).basename.to_s
153
- if 'README.md' != file_basename
154
- if !pages_files_saved.include?( file_basename )
155
- self.log(" - Deleting:"+file)
156
- FileUtils.rm(file)
157
- end
158
- end
159
- end
160
- end
161
-
162
- # Variables
163
- if data.key?("variables")
164
- File.write(jekyll_path+'/_data/vars.json', JSON.pretty_generate(data['variables']))
165
- self.log("Writing File /_data/vars.json")
166
- end
167
-
168
- # Content Blocks
169
- if data.key?("content_blocks")
170
- puts 'Content Blocks - ' + data['content_blocks'].length.to_s
171
- self.log('Content Blocks - ' + data['content_blocks'].length.to_s)
172
- cb_folder = jekyll_path+'/_includes/content_blocks'
173
-
174
- data['content_blocks'].each do |key, content_block|
175
- self.log('writing File /_includes/content_blocks/'+key+'.md')
176
- File.write(cb_folder+'/'+key+'.md' , content_block.to_s)
177
- end
178
- end
179
-
180
- # Content Images
181
- if data.key?("content_images")
182
- FileUtils::mkdir_p jekyll_path+'/assets/images/content'
183
-
184
- puts 'Content Images - ' + data['content_images'].length.to_s
185
- self.log('Content Images - ' + data['content_images'].length.to_s)
186
-
187
- data['content_images'].each do |image|
188
- image = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/content-blocks/' + image, jekyll_path+'/assets/images/content/' + image)
189
- end
190
-
191
- File.write(jekyll_path+'/_data/content_images.json', JSON.pretty_generate(data['content_images']))
192
- end
193
-
194
- # Taxonomy
195
- if data.key?("taxonomy")
196
- data['taxonomy'].each do |taxonomy_data_type|
197
- taxonomy_data_type['taxonomies'].each do |taxonomy|
198
- if '1' == taxonomy['enable_images']
199
- FileUtils::mkdir_p jekyll_path+'/assets/images/tax-images/' + taxonomy['id']
200
-
201
- if taxonomy.key?("terms")
202
- taxonomy['terms'].each do |term|
203
- if term['image']
204
- image_path = jekyll_path+'/assets/images/tax-images/' + term['taxonomy_id']
205
- src_image = end_point + '/media/images/'+site_id+'/taxonomies/' + term["image"]
206
-
207
- #puts 'A: ' + src_image + ' -> ' + image_path + '/' + term["image"]
208
-
209
- term['image'] = self.getAndSaveImages(src_image, image_path + '/' + term["image"])
210
- end
211
- end
212
- end
213
- end
214
- end
215
- end
216
-
217
- File.write(jekyll_path+'/_data/taxonomy.json', JSON.pretty_generate(data['taxonomy']))
218
- self.log("Writing File /_data/taxonomy.json")
219
- end
220
-
221
- # Nav Menus
222
- if data.key?("nav_menus")
223
- puts 'Nav menus - ' + data['nav_menus'].length.to_s
224
- self.log('Nav Menus - ' + data['nav_menus'].length.to_s)
225
- File.write(jekyll_path+'/_data/nav_menus.json', JSON.pretty_generate(data['nav_menus']))
226
- end
227
-
228
- # Posts
229
- existing_post_files = Dir.glob(File.join(jekyll_path+'/_posts', "*"))
230
-
231
- # Remove all post files
232
- existing_post_files.each do |file|
233
- self.log('Deleting Post File ' + file)
234
- FileUtils.rm(file)
235
- end
236
-
237
- # Post files
238
- if data.key?("posts")
239
- FileUtils::mkdir_p jekyll_path+'/assets/images/posts'
240
-
241
- puts "Posts - " + data['posts'].length.to_s
242
- data['posts'].each do |post|
243
- self.log('Saving Post File ' + post['date']+'-'+post['slug']+'.md')
244
-
245
- # Do Tax
246
- self.taxonomyImages(post)
247
-
248
- # save image
249
- if post['image']
250
- post['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/posts/' + post['image'], jekyll_path+'/assets/images/posts/' + post['image'])
251
- end
252
-
253
- jpost = JekyllPost.new(jekyll_path+'/_posts', post)
254
- jpost.savePageFile
255
- end
256
- end
257
-
258
-
259
- # Galleries
260
- if data.key?("galleries")
261
- FileUtils::mkdir_p jekyll_path+'/assets/images/galleries'
262
-
263
- puts "Galleries - " + data['galleries'].length.to_s
264
- self.log("Galleries - " + data['galleries'].length.to_s)
265
-
266
- # Save Photos
267
- data['galleries'].each_with_index do |gallery,idx|
268
- FileUtils::mkdir_p jekyll_path+'/assets/images/galleries/' + gallery['id']
269
-
270
- puts " - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos'
271
- self.log(" - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos')
272
-
273
- gallery['photos'].each_with_index do |photo,pidx|
274
- photo['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/galleries/photos/' + photo['image'], jekyll_path+'/assets/images/galleries/' + gallery['id'] + '/' + photo['image'])
275
- end
276
- end
277
-
278
- # Save Json Data
279
- File.write(jekyll_path+'/_data/galleries.json', JSON.pretty_generate(data['galleries']))
280
- self.log('Writing file /_data/galleries.json')
281
- end
282
-
283
- # Calendar
284
- if data.key?("calendar")
285
- FileUtils::mkdir_p jekyll_path+'/assets/images/calendar'
286
- FileUtils::mkdir_p jekyll_path+'/assets/files/calendar'
287
-
288
- puts "Calendar - " + data['calendar'].length.to_s
289
- self.log("Calendar - " + data['calendar'].length.to_s)
290
-
291
- # Save Photos / Files
292
- if data['calendar'].key?("events")
293
- data['calendar']['events'].each do |event|
294
- if event['image']
295
- event['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/calendar/' + event['image'], jekyll_path+'/assets/images/calendar/' + event['image'])
296
- end
297
-
298
- if event['file']
299
- self.getAndSaveFile(end_point + '/media/files/'+site_id+'/calendar/' + event['file'], jekyll_path+'/assets/files/calendar/' + event['file'])
300
- end
301
- end
302
- end
303
-
304
- # Save Json Data
305
- File.write(jekyll_path+'/_data/calendar.json', JSON.pretty_generate(data['calendar']))
306
- self.log('Writing File /_data/calendar.json')
307
- end
308
-
309
- # Menus (as json data and images)
310
- if data.key?("menus")
311
- # Save Json Data
312
-
313
- # Save Images
314
- FileUtils::mkdir_p jekyll_path+'/assets/images/menus'
315
- FileUtils::mkdir_p jekyll_path+'/assets/images/menus/items'
316
-
317
- data['menus'].each_with_index do |menu,idx|
318
- if menu['image']
319
- menu_image_path = jekyll_path+'/assets/images/menus'
320
-
321
- src_image = end_point + '/media/images/'+site_id+'/menus/' + menu['image']
322
-
323
- menu['image'] = self.getAndSaveImages(src_image, menu_image_path + '/' + menu['image'])
324
- end
325
- if menu['download']
326
- menu_download_path = jekyll_path+'/assets/files/menus'
327
-
328
- src_image = end_point + '/media/files/'+site_id+'/menu_downloads/' + menu['download']
329
-
330
- self.getAndSaveFile(src_image, menu_download_path + '/' + menu['download'])
331
- end
332
-
333
- menu['items'].each_with_index do |item,iidx|
334
- if item['image']
335
- image_path = jekyll_path+'/assets/images/menus/items/'+menu['id']
336
- FileUtils::mkdir_p image_path
337
-
338
- src_image = end_point + '/media/images/'+site_id+'/menus/items/' + item['image']
339
-
340
- item['image'] = self.getAndSaveImages(src_image, image_path+'/' + item['image'])
341
- end
342
- end
343
- end
344
-
345
- File.write(jekyll_path+'/_data/menus.json', JSON.pretty_generate(data['menus']))
346
-
347
- self.log('Writing File /_data/menus.json')
348
- end
349
-
350
- # Downloads
351
- if data.key?("downloads")
352
- # Save Json Data
353
- File.write(jekyll_path+'/_data/downloads.json', JSON.pretty_generate(data['downloads']))
354
-
355
- # Make Dir
356
- FileUtils::mkdir_p jekyll_path+'/assets/files/downloads'
357
-
358
- # Download Files
359
- data['downloads'].each do |download|
360
- self.taxonomyImages(download)
361
-
362
- download_path = jekyll_path+'/assets/files/downloads'
363
-
364
- src_file = end_point + '/media/files/'+site_id+'/downloads/' + download['file']
365
-
366
- self.getAndSaveFile(src_file, download_path + '/' + download['file'])
367
- end
368
- end
369
-
370
- # Properties
371
- if data.key?("properties")
372
- # Make Dir
373
- FileUtils::mkdir_p jekyll_path+'/assets/images/properties'
374
-
375
- # Directory Check
376
- option_file_path = jekyll_path+'/assets/files/property_files'
377
- FileUtils::mkdir_p option_file_path
378
-
379
- option_image_path = jekyll_path+'/assets/images/property_images'
380
- FileUtils::mkdir_p option_image_path
381
-
382
- # Save Collection Info
383
- collection_config[ 'properties' ] = {'output' => true}
384
-
385
- # Directory Check
386
- collection_path = jekyll_path+'/_properties'
387
- FileUtils::mkdir_p collection_path
388
-
389
- # Create files saved array
390
- files_saved = Array.new
391
-
392
- # Save Individual Collection Files
393
- data['properties']['data'].each_with_index do |row,pidx|
394
- row_data = row.clone;
395
- row_data.delete('id')
396
- id_key = 'property_id'
397
- row_data[ id_key ] = row['id']
398
-
399
- # process taxonomies
400
- self.taxonomyImages(data['properties']['data'][pidx])
401
-
402
- # Save Images
403
- if row.key?("galleries")
404
- row['galleries'].each_with_index do |gallery,gidx|
405
- # Directory Check
406
- image_path = jekyll_path+'/assets/images/properties/'+row['id']
407
- FileUtils::mkdir_p image_path
408
-
409
- if gallery.key?("images")
410
- # save images
411
- gallery['images'].each_with_index do |image,idx|
412
- src_image = end_point + '/media/images/'+site_id+'/properties/'+row['id']+'/' + image['image']
413
-
414
- data['properties']['data'][pidx]['galleries'][gidx]['images'][idx]['image'] = self.getAndSaveImages(src_image, image_path+'/' + image['image'])
415
- end
416
- end
417
- end
418
- end
419
-
420
- if row.key?("images")
421
- # Directory Check
422
- image_path = jekyll_path+'/assets/images/properties/'+row['id']
423
- FileUtils::mkdir_p image_path
424
-
425
- # save images
426
- row['images'].each_with_index do |image,idx|
427
-
428
- src_image = end_point + '/media/images/'+site_id+'/properties/'+row['id']+'/' + image['image']
429
-
430
- data['properties']['data'][pidx]['images'][idx]['image'] = self.getAndSaveImages(src_image, image_path+'/' + image['image'])
431
- end
432
- end
433
-
434
- # image/file option fields
435
- data['properties']['options'].each do |option|
436
- if row[ option['key'] ]
437
- case option['input_type']
438
- when 'file'
439
- src_file = end_point + '/media/files/'+site_id+'/property_files/' + row[ option['key'] ]
440
-
441
- self.getAndSaveFile(src_file, option_file_path+'/' + row[ option['key'] ])
442
- when 'image'
443
- src_image = end_point + '/media/images/'+site_id+'/property_images/' + row[ option['key'] ]
444
-
445
-
446
- data['properties']['data'][pidx][ option['key'] ] = self.getAndSaveImages(src_image, option_image_path+'/' + row[ option['key'] ])
447
- end
448
- end
449
- end
450
-
451
- jfile = JekyllFile.new(collection_path, row_data, 'collection')
452
- jfile.savePageFile
453
-
454
- files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
455
-
456
- self.log('Saving Property File ' + jfile.getFileName)
457
- end
458
-
459
- # Save Json Data
460
- File.write(jekyll_path+'/_data/properties.json', JSON.pretty_generate(data['properties']['data']))
461
- File.write(jekyll_path+'/_data/properties_options.json', JSON.pretty_generate(data['properties']['options']))
462
-
463
- # Clean up unused collection files
464
- self.cleanUpFolder(collection_path, files_saved)
465
- end
466
-
467
- # Custom Collections
468
- if data.key?("collections")
469
- data['collections'].each do |collection_type,collection_data|
470
- puts "Collection - " + collection_type + ' - ' + collection_data.length.to_s
471
- self.log("Collection - " + collection_type + ' - ' + collection_data.length.to_s)
472
-
473
- # Save Images / Files
474
- collection_data['data'].each_with_index do |row,cidx|
475
- collection_data['fields'].each do |fld_key,fld_info|
476
- row.each do |key,val|
477
- #puts 'Row ['+key+']:['+val.to_s+']'
478
- if fld_key == key && val
479
- case fld_info['type']
480
- when 'image'
481
- # Directory Check
482
- image_path = jekyll_path+'/assets/images/'+collection_type
483
- FileUtils::mkdir_p image_path
484
-
485
- src_image = end_point + '/media/images/'+site_id+'/'+collection_type+'/' + val
486
-
487
- collection_data['data'][cidx][fld_key] = self.getAndSaveImages(src_image, image_path+'/' + val)
488
- when 'file'
489
- # Directory Check
490
- file_path = jekyll_path+'/assets/files/'+collection_type
491
- FileUtils::mkdir_p file_path
492
-
493
- src_file = end_point + '/media/files/'+site_id+'/'+collection_type+'/' + val
494
-
495
- self.getAndSaveFile(src_file, file_path+'/' + val)
496
- end
497
- end
498
- end
499
- end
500
-
501
- #hasmany data
502
- if row.key?("hasmany_data")
503
- row['hasmany_data'].each do |hasmany_type,hasmany_data|
504
- puts " - hasmany : " + hasmany_type + " - " + hasmany_data['data'].length.to_s
505
- hasmany_data['fields'].each do |fld_key,fld_info|
506
- hasmany_data['data'].each_with_index do |hsrow,hsidx|
507
- hsrow.each do |key,val|
508
- if fld_key == key && val
509
- case fld_info['type']
510
- when 'image'
511
- # Directory Check
512
- image_path = jekyll_path+'/assets/images/'+collection_type+'/'+hasmany_type+'/'+row['id']
513
- FileUtils::mkdir_p image_path
514
-
515
- src_image = end_point + '/media/images/'+site_id+'/'+collection_type+'/'+row['id']+'/' + val
516
-
517
- collection_data['data'][cidx]['hasmany_data'][hasmany_type]['data'][hsidx][fld_key] = self.getAndSaveImages(src_image, image_path+'/' + val)
518
- when 'file'
519
- # Directory Check
520
- file_path = jekyll_path+'/assets/files/'+collection_type+'/'+hasmany_type+'/'+row['id']
521
- FileUtils::mkdir_p file_path
522
-
523
- src_file = end_point + '/media/files/'+site_id+'/'+collection_type+'/'+row['id']+'/' + val
524
-
525
- self.getAndSaveFile(src_file, file_path+'/' + val)
526
- end
527
- end
528
- end
529
- end
530
- end
531
- end # has many data each
532
- end
533
- end
534
-
535
- # Loop through the collection data
536
- case collection_data['settings']['type']
537
- when 'collection'
538
- # Add to collection hash
539
- collection_config[ collection_type ] = {'output' => true}
540
-
541
- # Directory Check
542
- collection_path = jekyll_path+'/_' + collection_type
543
- FileUtils::mkdir_p collection_path
544
-
545
- # Create files saved array
546
- files_saved = Array.new
547
-
548
- # Save Individual Collection Files
549
- collection_data['data'].each do |row|
550
- col_data = row.clone;
551
- col_data.delete('id')
552
- id_key = collection_type + '_id'
553
- col_data[ id_key ] = row['id']
554
- jfile = JekyllFile.new(collection_path, col_data, 'collection')
555
- jfile.savePageFile
556
-
557
- files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
558
-
559
- self.log('Saving Collection File ' + jfile.getFileName)
560
- end
561
-
562
- # Clean up unused collection files
563
- self.cleanUpFolder(collection_path, files_saved)
564
- end
565
-
566
- # Save Json Data
567
- File.write(jekyll_path+'/_data/'+collection_type+'.json', JSON.pretty_generate(collection_data['data']))
568
- self.log('Writing File /_data/'+collection_type+'.json')
569
-
570
- end
571
-
572
- # Add Collections to _config.tml
573
- config['collections'] = collection_config
574
- File.write(jekyll_path+"/_config.yml", config.to_yaml)
575
- self.log('Writing File /_config.yml')
576
- end
577
-
578
- @@logger.close
579
- end
580
-
581
- def self.taxonomyImages(row)
582
- if row.key?("taxonomy")
583
- ##puts ' - Found taxonomy key ['+row['id']+']'
584
- row['taxonomy'].each_with_index do |tax_row,idx|
585
- if tax_row.key?("image")
586
- if tax_row["image"]
587
- ##puts ' - ' + idx.to_s + ' saving tax image'
588
- ##puts ' - - ['+tax_row["image"]+']'
589
- image_path = @@jekyll_path+'/assets/images/tax-images/' + tax_row['taxonomy_id']
590
- src_image = @@end_point + '/media/images/'+@@site_id+'/taxonomies/' + tax_row["image"]
591
- #puts 'F: ' + src_image + ' -> ' + image_path + '/' + tax_row["image"]
592
-
593
- row['taxonomy'][idx]['image'] = self.getAndSaveImages(src_image, image_path + '/' + tax_row["image"])
594
- end
595
- end
596
- end
597
- end
598
-
599
- #return row
600
- end
601
-
602
- def self.log(data)
603
- @@logger.debug { data }
604
- end
605
-
606
- def self.readFile(file)
607
- file = File.open(file, "r")
608
- data = file.read
609
- file.close
610
- return data
611
- end
612
-
613
- def self.loadDataFromApi(end_point, api_key, site_id)
614
- uri = URI(end_point+'/api/Sites/allcontent')
615
- params = { :key => api_key, :site_id => site_id }
616
- uri.query = URI.encode_www_form(params)
617
-
618
- res = Net::HTTP.get_response(uri)
619
-
620
- begin
621
- json_data = JSON.parse(res.body)
622
- rescue
623
- puts "Server did not return JSON data. Check to make sure your endpoint is valid ["+end_point+"]"
624
- return false
625
- end
626
-
627
- return json_data['data']
628
- end
629
-
630
- def self.getAndSaveFile(source, dest)
631
- if !Pathname.new(dest).file?
632
- self.log('Get/Save ' + dest)
633
- uri = URI.encode(source)
634
-
635
- begin
636
- open(dest, 'wb') do |file|
637
- begin
638
- file << open(uri).read
639
- rescue
640
- puts uri + " could not be read"
641
- return false
642
- end
643
- end
644
- rescue
645
- puts 'source ['+source+'] Could not be opened to save'
646
- self.log('source ['+source+'] Could not be opened to save')
647
- return false
648
- end
649
- end
650
- end
651
-
652
- def self.optimizeImage(dest)
653
- ImageOptimizer.new(dest, quality: 80, quiet: true).optimize
654
- end
655
-
656
- def self.getAndSaveImages(source, dest)
657
- images = {
658
- 'original' => File.basename( dest )
659
- }
660
-
661
- # Save Original
662
- if !File.file?( dest )
663
- if self.getAndSaveFile( source, dest )
664
- self.optimizeImage( dest )
665
- end
666
- end
667
-
668
- # Save Variants
669
- if @@image_variants
670
- @@image_variants.each do |variant|
671
- v_source = self.getImageVariantSource(source, variant)
672
- v_dest = self.getImageVariantDestination(dest, variant)
673
-
674
- if !File.file?( v_dest )
675
- if self.getAndSaveFile( v_source, v_dest )
676
- self.optimizeImage( v_dest )
677
- end
678
- end
679
-
680
- images[ variant['key'] ] = File.basename( v_dest )
681
- end
682
- end
683
-
684
- return images
685
- end
686
-
687
- def self.getImageVariantSource(source, variant)
688
- qs = {}
689
-
690
- if variant['width']
691
- qs['w'] = variant['width']
692
- end
693
- if variant['height']
694
- qs['h'] = variant['height']
695
- end
696
- if variant['crop']
697
- qs['zoomfit'] = "1"
698
- end
699
-
700
- new_source = source + "?" + URI.encode_www_form(qs)
701
-
702
- return new_source
703
- end
704
-
705
- def self.getImageVariantDestination(source, variant)
706
- ext = File.extname( source )
707
- basename = File.basename( source, ext )
708
- dirname = File.dirname( source )
709
-
710
- return dirname + '/' + basename + '_' + variant['key'] + ext
711
- end
712
-
713
- def self.cleanUpFolder(path, files)
714
- self.log('Clean up Path ' + path)
715
- existing_files = Dir.glob(File.join(path, "*"))
716
-
717
- existing_files.each do |file|
718
- file_basename = Pathname.new(file).basename.to_s
719
-
720
- if !files.include?( file_basename )
721
- self.log(' - ' + file)
722
- FileUtils.rm(file)
723
- end
724
- end
725
- end
726
- end
727
-
728
-
729
-
1
+ # Libs
2
+ require 'yaml'
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'fileutils'
6
+ require 'pathname'
7
+ require 'open-uri'
8
+ require 'logger'
9
+ require 'image_optimizer'
10
+
11
+ require 'code/JekyllFile.rb'
12
+ require 'code/JekyllPost.rb'
13
+
14
+ class ZeroFetcher
15
+ @@logger
16
+ @@jekyll_path
17
+ @@image_variants
18
+ @@end_point
19
+ @@site_id
20
+
21
+ def self.run
22
+ # Paths
23
+ app_path = File.dirname(__FILE__)
24
+ jekyll_path = Dir.pwd
25
+ @@jekyll_path = jekyll_path
26
+
27
+ gitignores = [
28
+ '_logs',
29
+ '_posts',
30
+ '_includes/pages',
31
+ '_includes/content_blocks',
32
+ ]
33
+
34
+ # App Requires
35
+ FileUtils::mkdir_p jekyll_path+'/_logs'
36
+ FileUtils::mkdir_p jekyll_path+'/_data'
37
+ FileUtils::mkdir_p jekyll_path+'/_posts'
38
+ FileUtils::mkdir_p jekyll_path+'/assets'
39
+ FileUtils::mkdir_p jekyll_path+'/assets/images'
40
+ FileUtils::mkdir_p jekyll_path+'/assets/images/pages'
41
+ FileUtils::mkdir_p jekyll_path+'/assets/files'
42
+ FileUtils::mkdir_p jekyll_path+'/assets/images/tax-images'
43
+ FileUtils::mkdir_p jekyll_path+'/_includes'
44
+ FileUtils::mkdir_p jekyll_path+'/_includes/pages'
45
+ FileUtils::mkdir_p jekyll_path+'/_includes/content_blocks'
46
+
47
+ # Setup Logger
48
+ @@logger = Logger.new(jekyll_path + '/_logs/logfile.log', 10, 1024000)
49
+
50
+ if !Pathname.new(jekyll_path+"/_config.yml").file?
51
+ self.log('_config.yml not found')
52
+ abort('_config.yml not found')
53
+ end
54
+
55
+ config = YAML.load_file(jekyll_path+"/_config.yml")
56
+
57
+ if !config.has_key?('fetcher')
58
+ self.log('No Fetcher Info Found')
59
+ abort('No Fetcher Info Found')
60
+ end
61
+
62
+ end_point = config['fetcher']['end_point']
63
+ @@end_point = end_point
64
+ site_id = config['fetcher']['site_id'].to_s
65
+ @@site_id = site_id;
66
+
67
+ # Load data from API
68
+ data = self.loadDataFromApi(end_point, config['fetcher']['api_key'], site_id)
69
+
70
+ if data == false
71
+ return
72
+ end
73
+
74
+ # Create empty collection hash
75
+ collection_config = {};
76
+
77
+ # Site Info
78
+ if data.key?("site")
79
+ File.write(jekyll_path+'/_data/site.json', JSON.pretty_generate(data['site']))
80
+ self.log('Writing File /_data/site.json')
81
+ end
82
+
83
+ # write .htaccess file
84
+ htaccess = '<IfModule mod_headers.c>
85
+ <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
86
+ Header set Access-Control-Allow-Origin "*"
87
+ </FilesMatch>
88
+ </IfModule>
89
+ RewriteEngine On
90
+ Redirect 301 /d3panel '+end_point+'/d3panel?sitekey='+data['site']['key']+'
91
+ ErrorDocument 404 /404/index.html'
92
+ File.write(jekyll_path+'/.htaccess', htaccess)
93
+
94
+ # Image Variants
95
+ @@image_variants = data['image_variants']
96
+
97
+ # Pages
98
+ if data.key?("pages")
99
+ pages_files_saved = Array.new
100
+
101
+ pages_folder = jekyll_path+'/_includes/pages'
102
+
103
+ puts 'Pages - ' + data['pages'].length.to_s
104
+ self.log('Pages - ' + data['pages'].length.to_s)
105
+
106
+ # array to store hash info on all pages
107
+ pages = Array.new
108
+
109
+ # Loop through each page
110
+ data['pages'].each do |page|
111
+ puts ' - ' + page['name']
112
+ self.log(' - ' + page['name'])
113
+
114
+ jpage = JekyllFile.new(jekyll_path, page, 'page')
115
+
116
+ jpage.saveContentFile(pages_folder+'/'+page['url']+'.md', page['content'])
117
+ jpage.savePageFile
118
+ #pages_files_saved.push(jpage.getFileName)
119
+ pages_files_saved.push( Pathname.new( jpage.getFileName ).basename.to_s )
120
+ self.log(" - Saved:"+jpage.getFileName)
121
+
122
+ # Add to pages array
123
+ page_info = {
124
+ 'id' => page['id'],
125
+ 'name' => page['name'],
126
+ 'slug' => page['slug'],
127
+ 'url' => page['url'],
128
+ 'layout' => page['layout'],
129
+ 'meta_title' => (page['meta_title']) ? page['meta_title'] : page['name'],
130
+ 'meta_description' => page['meta_description'],
131
+ 'short_description' => page['short_description'],
132
+ 'parent_id' => page['parent_id'],
133
+ }
134
+ if page['image']
135
+ page_info['image'] = 'images/pages/' + page['image']
136
+ end
137
+ pages.push(page_info)
138
+
139
+ # save image
140
+ if page['image']
141
+ page['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/pages/' + page['image'], jekyll_path+'/assets/images/pages/' + page['image'])
142
+ end
143
+ end
144
+
145
+ File.write(jekyll_path+'/_data/pages.json', JSON.pretty_generate(pages))
146
+ self.log("Writing File /_data/pages.json")
147
+
148
+ # Clean Unused Pages
149
+ existing_pages_files = Dir.glob(File.join(jekyll_path, "*.md"))
150
+
151
+ existing_pages_files.each do |file|
152
+ file_basename = Pathname.new(file).basename.to_s
153
+ if 'README.md' != file_basename
154
+ if !pages_files_saved.include?( file_basename )
155
+ self.log(" - Deleting:"+file)
156
+ FileUtils.rm(file)
157
+ end
158
+ end
159
+ end
160
+ end
161
+
162
+ # Variables
163
+ if data.key?("variables")
164
+ File.write(jekyll_path+'/_data/vars.json', JSON.pretty_generate(data['variables']))
165
+ self.log("Writing File /_data/vars.json")
166
+ end
167
+
168
+ # Content Blocks
169
+ if data.key?("content_blocks")
170
+ puts 'Content Blocks - ' + data['content_blocks'].length.to_s
171
+ self.log('Content Blocks - ' + data['content_blocks'].length.to_s)
172
+ cb_folder = jekyll_path+'/_includes/content_blocks'
173
+
174
+ data['content_blocks'].each do |key, content_block|
175
+ self.log('writing File /_includes/content_blocks/'+key+'.md')
176
+ File.write(cb_folder+'/'+key+'.md' , content_block.to_s)
177
+ end
178
+ end
179
+
180
+ # Content Images
181
+ if data.key?("content_images")
182
+ FileUtils::mkdir_p jekyll_path+'/assets/images/content'
183
+
184
+ puts 'Content Images - ' + data['content_images'].length.to_s
185
+ self.log('Content Images - ' + data['content_images'].length.to_s)
186
+
187
+ data['content_images'].each do |image|
188
+ image = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/content-blocks/' + image, jekyll_path+'/assets/images/content/' + image)
189
+ end
190
+
191
+ File.write(jekyll_path+'/_data/content_images.json', JSON.pretty_generate(data['content_images']))
192
+ end
193
+
194
+ # Taxonomy
195
+ if data.key?("taxonomy")
196
+ data['taxonomy'].each do |taxonomy_data_type|
197
+ taxonomy_data_type['taxonomies'].each do |taxonomy|
198
+ if '1' == taxonomy['enable_images']
199
+ FileUtils::mkdir_p jekyll_path+'/assets/images/tax-images/' + taxonomy['id']
200
+
201
+ if taxonomy.key?("terms")
202
+ taxonomy['terms'].each do |term|
203
+ if term['image']
204
+ image_path = jekyll_path+'/assets/images/tax-images/' + term['taxonomy_id']
205
+ src_image = end_point + '/media/images/'+site_id+'/taxonomies/' + term["image"]
206
+
207
+ #puts 'A: ' + src_image + ' -> ' + image_path + '/' + term["image"]
208
+
209
+ term['image'] = self.getAndSaveImages(src_image, image_path + '/' + term["image"])
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ end
216
+
217
+ File.write(jekyll_path+'/_data/taxonomy.json', JSON.pretty_generate(data['taxonomy']))
218
+ self.log("Writing File /_data/taxonomy.json")
219
+ end
220
+
221
+ # Nav Menus
222
+ if data.key?("nav_menus")
223
+ puts 'Nav menus - ' + data['nav_menus'].length.to_s
224
+ self.log('Nav Menus - ' + data['nav_menus'].length.to_s)
225
+ File.write(jekyll_path+'/_data/nav_menus.json', JSON.pretty_generate(data['nav_menus']))
226
+ end
227
+
228
+ # Posts
229
+ existing_post_files = Dir.glob(File.join(jekyll_path+'/_posts', "*"))
230
+
231
+ # Remove all post files
232
+ existing_post_files.each do |file|
233
+ self.log('Deleting Post File ' + file)
234
+ FileUtils.rm(file)
235
+ end
236
+
237
+ # Post files
238
+ if data.key?("posts")
239
+ FileUtils::mkdir_p jekyll_path+'/assets/images/posts'
240
+
241
+ puts "Posts - " + data['posts'].length.to_s
242
+ data['posts'].each do |post|
243
+ self.log('Saving Post File ' + post['date']+'-'+post['slug']+'.md')
244
+
245
+ # Do Tax
246
+ self.taxonomyImages(post)
247
+
248
+ # save image
249
+ if post['image']
250
+ post['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/posts/' + post['image'], jekyll_path+'/assets/images/posts/' + post['image'])
251
+ end
252
+
253
+ jpost = JekyllPost.new(jekyll_path+'/_posts', post)
254
+ jpost.savePageFile
255
+ end
256
+ end
257
+
258
+
259
+ # Galleries
260
+ if data.key?("galleries")
261
+ FileUtils::mkdir_p jekyll_path+'/assets/images/galleries'
262
+
263
+ puts "Galleries - " + data['galleries'].length.to_s
264
+ self.log("Galleries - " + data['galleries'].length.to_s)
265
+
266
+ # Save Photos
267
+ data['galleries'].each_with_index do |gallery,idx|
268
+ FileUtils::mkdir_p jekyll_path+'/assets/images/galleries/' + gallery['id']
269
+
270
+ puts " - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos'
271
+ self.log(" - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos')
272
+
273
+ gallery['photos'].each_with_index do |photo,pidx|
274
+ photo['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/galleries/photos/' + photo['image'], jekyll_path+'/assets/images/galleries/' + gallery['id'] + '/' + photo['image'])
275
+ end
276
+ end
277
+
278
+ # Save Json Data
279
+ File.write(jekyll_path+'/_data/galleries.json', JSON.pretty_generate(data['galleries']))
280
+ self.log('Writing file /_data/galleries.json')
281
+ end
282
+
283
+ # Calendar
284
+ if data.key?("calendar")
285
+ FileUtils::mkdir_p jekyll_path+'/assets/images/calendar'
286
+ FileUtils::mkdir_p jekyll_path+'/assets/files/calendar'
287
+
288
+ puts "Calendar - " + data['calendar'].length.to_s
289
+ self.log("Calendar - " + data['calendar'].length.to_s)
290
+
291
+ # Save Photos / Files
292
+ if data['calendar'].key?("events")
293
+ data['calendar']['events'].each do |event|
294
+ if event['image']
295
+ event['image'] = self.getAndSaveImages(end_point + '/media/images/'+site_id+'/calendar/' + event['image'], jekyll_path+'/assets/images/calendar/' + event['image'])
296
+ end
297
+
298
+ if event['file']
299
+ self.getAndSaveFile(end_point + '/media/files/'+site_id+'/calendar/' + event['file'], jekyll_path+'/assets/files/calendar/' + event['file'])
300
+ end
301
+ end
302
+ end
303
+
304
+ # Save Json Data
305
+ File.write(jekyll_path+'/_data/calendar.json', JSON.pretty_generate(data['calendar']))
306
+ self.log('Writing File /_data/calendar.json')
307
+ end
308
+
309
+ # Menus (as json data and images)
310
+ if data.key?("menus")
311
+ # Save Json Data
312
+
313
+ # Save Images
314
+ FileUtils::mkdir_p jekyll_path+'/assets/images/menus'
315
+ FileUtils::mkdir_p jekyll_path+'/assets/images/menus/items'
316
+
317
+ data['menus'].each_with_index do |menu,idx|
318
+ if menu['image']
319
+ menu_image_path = jekyll_path+'/assets/images/menus'
320
+
321
+ src_image = end_point + '/media/images/'+site_id+'/menus/' + menu['image']
322
+
323
+ menu['image'] = self.getAndSaveImages(src_image, menu_image_path + '/' + menu['image'])
324
+ end
325
+ if menu['download']
326
+ menu_download_path = jekyll_path+'/assets/files/menus'
327
+
328
+ src_image = end_point + '/media/files/'+site_id+'/menu_downloads/' + menu['download']
329
+
330
+ self.getAndSaveFile(src_image, menu_download_path + '/' + menu['download'])
331
+ end
332
+
333
+ menu['items'].each_with_index do |item,iidx|
334
+ if item['image']
335
+ image_path = jekyll_path+'/assets/images/menus/items/'+menu['id']
336
+ FileUtils::mkdir_p image_path
337
+
338
+ src_image = end_point + '/media/images/'+site_id+'/menus/items/' + item['image']
339
+
340
+ item['image'] = self.getAndSaveImages(src_image, image_path+'/' + item['image'])
341
+ end
342
+ end
343
+ end
344
+
345
+ File.write(jekyll_path+'/_data/menus.json', JSON.pretty_generate(data['menus']))
346
+
347
+ self.log('Writing File /_data/menus.json')
348
+ end
349
+
350
+ # Downloads
351
+ if data.key?("downloads")
352
+ # Save Json Data
353
+ File.write(jekyll_path+'/_data/downloads.json', JSON.pretty_generate(data['downloads']))
354
+
355
+ # Make Dir
356
+ FileUtils::mkdir_p jekyll_path+'/assets/files/downloads'
357
+
358
+ # Download Files
359
+ data['downloads'].each do |download|
360
+ self.taxonomyImages(download)
361
+
362
+ download_path = jekyll_path+'/assets/files/downloads'
363
+
364
+ src_file = end_point + '/media/files/'+site_id+'/downloads/' + download['file']
365
+
366
+ self.getAndSaveFile(src_file, download_path + '/' + download['file'])
367
+ end
368
+ end
369
+
370
+ # Properties
371
+ if data.key?("properties")
372
+ # Make Dir
373
+ FileUtils::mkdir_p jekyll_path+'/assets/images/properties'
374
+
375
+ # Directory Check
376
+ option_file_path = jekyll_path+'/assets/files/property_files'
377
+ FileUtils::mkdir_p option_file_path
378
+
379
+ option_image_path = jekyll_path+'/assets/images/property_images'
380
+ FileUtils::mkdir_p option_image_path
381
+
382
+ # Save Collection Info
383
+ collection_config[ 'properties' ] = {'output' => true}
384
+
385
+ # Directory Check
386
+ collection_path = jekyll_path+'/_properties'
387
+ FileUtils::mkdir_p collection_path
388
+
389
+ # Create files saved array
390
+ files_saved = Array.new
391
+
392
+ # Save Individual Collection Files
393
+ data['properties']['data'].each_with_index do |row,pidx|
394
+ row_data = row.clone;
395
+ row_data.delete('id')
396
+ id_key = 'property_id'
397
+ row_data[ id_key ] = row['id']
398
+
399
+ # process taxonomies
400
+ self.taxonomyImages(data['properties']['data'][pidx])
401
+
402
+ # Save Images
403
+ if row.key?("galleries")
404
+ row['galleries'].each_with_index do |gallery,gidx|
405
+ # Directory Check
406
+ image_path = jekyll_path+'/assets/images/properties/'+row['id']
407
+ FileUtils::mkdir_p image_path
408
+
409
+ if gallery.key?("images")
410
+ # save images
411
+ gallery['images'].each_with_index do |image,idx|
412
+ src_image = end_point + '/media/images/'+site_id+'/properties/'+row['id']+'/' + image['image']
413
+
414
+ data['properties']['data'][pidx]['galleries'][gidx]['images'][idx]['image'] = self.getAndSaveImages(src_image, image_path+'/' + image['image'])
415
+ end
416
+ end
417
+ end
418
+ end
419
+
420
+ if row.key?("images")
421
+ # Directory Check
422
+ image_path = jekyll_path+'/assets/images/properties/'+row['id']
423
+ FileUtils::mkdir_p image_path
424
+
425
+ # save images
426
+ row['images'].each_with_index do |image,idx|
427
+
428
+ src_image = end_point + '/media/images/'+site_id+'/properties/'+row['id']+'/' + image['image']
429
+
430
+ data['properties']['data'][pidx]['images'][idx]['image'] = self.getAndSaveImages(src_image, image_path+'/' + image['image'])
431
+ end
432
+ end
433
+
434
+ # image/file option fields
435
+ data['properties']['options'].each do |option|
436
+ if row[ option['key'] ]
437
+ case option['input_type']
438
+ when 'file'
439
+ src_file = end_point + '/media/files/'+site_id+'/property_files/' + row[ option['key'] ]
440
+
441
+ self.getAndSaveFile(src_file, option_file_path+'/' + row[ option['key'] ])
442
+ when 'image'
443
+ src_image = end_point + '/media/images/'+site_id+'/property_images/' + row[ option['key'] ]
444
+
445
+
446
+ data['properties']['data'][pidx][ option['key'] ] = self.getAndSaveImages(src_image, option_image_path+'/' + row[ option['key'] ])
447
+ end
448
+ end
449
+ end
450
+
451
+ jfile = JekyllFile.new(collection_path, row_data, 'collection')
452
+ jfile.savePageFile
453
+
454
+ files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
455
+
456
+ self.log('Saving Property File ' + jfile.getFileName)
457
+ end
458
+
459
+ # Save Json Data
460
+ File.write(jekyll_path+'/_data/properties.json', JSON.pretty_generate(data['properties']['data']))
461
+ File.write(jekyll_path+'/_data/properties_options.json', JSON.pretty_generate(data['properties']['options']))
462
+
463
+ # Clean up unused collection files
464
+ self.cleanUpFolder(collection_path, files_saved)
465
+ end
466
+
467
+ # Custom Collections
468
+ if data.key?("collections")
469
+ data['collections'].each do |collection_type,collection_data|
470
+ puts "Collection - " + collection_type + ' - ' + collection_data.length.to_s
471
+ self.log("Collection - " + collection_type + ' - ' + collection_data.length.to_s)
472
+
473
+ # Save Images / Files
474
+ collection_data['data'].each_with_index do |row,cidx|
475
+ collection_data['fields'].each do |fld_key,fld_info|
476
+ row.each do |key,val|
477
+ #puts 'Row ['+key+']:['+val.to_s+']'
478
+ if fld_key == key && val
479
+ case fld_info['type']
480
+ when 'image'
481
+ # Directory Check
482
+ image_path = jekyll_path+'/assets/images/'+collection_type
483
+ FileUtils::mkdir_p image_path
484
+
485
+ src_image = end_point + '/media/images/'+site_id+'/'+collection_type+'/' + val
486
+
487
+ collection_data['data'][cidx][fld_key] = self.getAndSaveImages(src_image, image_path+'/' + val)
488
+ when 'file'
489
+ # Directory Check
490
+ file_path = jekyll_path+'/assets/files/'+collection_type
491
+ FileUtils::mkdir_p file_path
492
+
493
+ src_file = end_point + '/media/files/'+site_id+'/'+collection_type+'/' + val
494
+
495
+ self.getAndSaveFile(src_file, file_path+'/' + val)
496
+ end
497
+ end
498
+ end
499
+ end
500
+
501
+ #hasmany data
502
+ if row.key?("hasmany_data")
503
+ row['hasmany_data'].each do |hasmany_type,hasmany_data|
504
+ puts " - hasmany : " + hasmany_type + " - " + hasmany_data['data'].length.to_s
505
+ hasmany_data['fields'].each do |fld_key,fld_info|
506
+ hasmany_data['data'].each_with_index do |hsrow,hsidx|
507
+ hsrow.each do |key,val|
508
+ if fld_key == key && val
509
+ case fld_info['type']
510
+ when 'image'
511
+ # Directory Check
512
+ image_path = jekyll_path+'/assets/images/'+collection_type+'/'+hasmany_type+'/'+row['id']
513
+ FileUtils::mkdir_p image_path
514
+
515
+ src_image = end_point + '/media/images/'+site_id+'/'+collection_type+'/'+row['id']+'/' + val
516
+
517
+ collection_data['data'][cidx]['hasmany_data'][hasmany_type]['data'][hsidx][fld_key] = self.getAndSaveImages(src_image, image_path+'/' + val)
518
+ when 'file'
519
+ # Directory Check
520
+ file_path = jekyll_path+'/assets/files/'+collection_type+'/'+hasmany_type+'/'+row['id']
521
+ FileUtils::mkdir_p file_path
522
+
523
+ src_file = end_point + '/media/files/'+site_id+'/'+collection_type+'/'+row['id']+'/' + val
524
+
525
+ self.getAndSaveFile(src_file, file_path+'/' + val)
526
+ end
527
+ end
528
+ end
529
+ end
530
+ end
531
+ end # has many data each
532
+ end
533
+ end
534
+
535
+ # Loop through the collection data
536
+ case collection_data['settings']['type']
537
+ when 'collection'
538
+ # Add to collection hash
539
+ collection_config[ collection_type ] = {'output' => true}
540
+
541
+ # Directory Check
542
+ collection_path = jekyll_path+'/_' + collection_type
543
+ FileUtils::mkdir_p collection_path
544
+
545
+ # Create files saved array
546
+ files_saved = Array.new
547
+
548
+ # Save Individual Collection Files
549
+ collection_data['data'].each do |row|
550
+ col_data = row.clone;
551
+ col_data.delete('id')
552
+ id_key = collection_type + '_id'
553
+ col_data[ id_key ] = row['id']
554
+ jfile = JekyllFile.new(collection_path, col_data, 'collection')
555
+ jfile.savePageFile
556
+
557
+ files_saved.push( Pathname.new( jfile.getFileName ).basename.to_s )
558
+
559
+ self.log('Saving Collection File ' + jfile.getFileName)
560
+ end
561
+
562
+ # Clean up unused collection files
563
+ self.cleanUpFolder(collection_path, files_saved)
564
+ end
565
+
566
+ # Save Json Data
567
+ File.write(jekyll_path+'/_data/'+collection_type+'.json', JSON.pretty_generate(collection_data['data']))
568
+ self.log('Writing File /_data/'+collection_type+'.json')
569
+
570
+ end
571
+
572
+ # Add Collections to _config.tml
573
+ config['collections'] = collection_config
574
+ File.write(jekyll_path+"/_config.yml", config.to_yaml)
575
+ self.log('Writing File /_config.yml')
576
+ end
577
+
578
+ @@logger.close
579
+ end
580
+
581
+ def self.taxonomyImages(row)
582
+ if row.key?("taxonomy")
583
+ ##puts ' - Found taxonomy key ['+row['id']+']'
584
+ row['taxonomy'].each_with_index do |tax_row,idx|
585
+ if tax_row.key?("image")
586
+ if tax_row["image"]
587
+ ##puts ' - ' + idx.to_s + ' saving tax image'
588
+ ##puts ' - - ['+tax_row["image"]+']'
589
+ image_path = @@jekyll_path+'/assets/images/tax-images/' + tax_row['taxonomy_id']
590
+ src_image = @@end_point + '/media/images/'+@@site_id+'/taxonomies/' + tax_row["image"]
591
+ #puts 'F: ' + src_image + ' -> ' + image_path + '/' + tax_row["image"]
592
+
593
+ row['taxonomy'][idx]['image'] = self.getAndSaveImages(src_image, image_path + '/' + tax_row["image"])
594
+ end
595
+ end
596
+ end
597
+ end
598
+
599
+ #return row
600
+ end
601
+
602
+ def self.log(data)
603
+ @@logger.debug { data }
604
+ end
605
+
606
+ def self.readFile(file)
607
+ file = File.open(file, "r")
608
+ data = file.read
609
+ file.close
610
+ return data
611
+ end
612
+
613
+ def self.loadDataFromApi(end_point, api_key, site_id)
614
+ uri = URI(end_point+'/api/Sites/allcontent')
615
+ params = { :key => api_key, :site_id => site_id }
616
+ uri.query = URI.encode_www_form(params)
617
+
618
+ res = Net::HTTP.get_response(uri)
619
+
620
+ begin
621
+ json_data = JSON.parse(res.body)
622
+ rescue
623
+ puts "Server did not return JSON data. Check to make sure your endpoint is valid ["+end_point+"]"
624
+ return false
625
+ end
626
+
627
+ return json_data['data']
628
+ end
629
+
630
+ def self.getAndSaveFile(source, dest)
631
+ if !Pathname.new(dest).file?
632
+ self.log('Get/Save ' + dest)
633
+ uri = URI.encode(source)
634
+
635
+ begin
636
+ open(dest, 'wb') do |file|
637
+ begin
638
+ file << open(uri).read
639
+ rescue
640
+ puts uri + " could not be read"
641
+ return false
642
+ end
643
+ end
644
+ rescue
645
+ puts 'source ['+source+'] Could not be opened to save'
646
+ self.log('source ['+source+'] Could not be opened to save')
647
+ return false
648
+ end
649
+ end
650
+ end
651
+
652
+ def self.optimizeImage(dest)
653
+ ImageOptimizer.new(dest, quality: 80, quiet: true).optimize
654
+ end
655
+
656
+ def self.getAndSaveImages(source, dest)
657
+ images = {
658
+ 'original' => File.basename( dest )
659
+ }
660
+
661
+ # Save Original
662
+ if !File.file?( dest )
663
+ if self.getAndSaveFile( source, dest )
664
+ self.optimizeImage( dest )
665
+ end
666
+ end
667
+
668
+ # Save Variants
669
+ if @@image_variants
670
+ @@image_variants.each do |variant|
671
+ v_source = self.getImageVariantSource(source, variant)
672
+ v_dest = self.getImageVariantDestination(dest, variant)
673
+
674
+ if !File.file?( v_dest )
675
+ if self.getAndSaveFile( v_source, v_dest )
676
+ self.optimizeImage( v_dest )
677
+ end
678
+ end
679
+
680
+ images[ variant['key'] ] = File.basename( v_dest )
681
+ end
682
+ end
683
+
684
+ return images
685
+ end
686
+
687
+ def self.getImageVariantSource(source, variant)
688
+ qs = {}
689
+
690
+ if variant['width']
691
+ qs['w'] = variant['width']
692
+ end
693
+ if variant['height']
694
+ qs['h'] = variant['height']
695
+ end
696
+ if variant['crop']
697
+ qs['zoomfit'] = "1"
698
+ end
699
+
700
+ new_source = source + "?" + URI.encode_www_form(qs)
701
+
702
+ return new_source
703
+ end
704
+
705
+ def self.getImageVariantDestination(source, variant)
706
+ ext = File.extname( source )
707
+ basename = File.basename( source, ext )
708
+ dirname = File.dirname( source )
709
+
710
+ return dirname + '/' + basename + '_' + variant['key'] + ext
711
+ end
712
+
713
+ def self.cleanUpFolder(path, files)
714
+ self.log('Clean up Path ' + path)
715
+ existing_files = Dir.glob(File.join(path, "*"))
716
+
717
+ existing_files.each do |file|
718
+ file_basename = Pathname.new(file).basename.to_s
719
+
720
+ if !files.include?( file_basename )
721
+ self.log(' - ' + file)
722
+ FileUtils.rm(file)
723
+ end
724
+ end
725
+ end
726
+ end
727
+
728
+
729
+