dronejob 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,94 +0,0 @@
1
- require 'dronejob/workspace_dir/archive'
2
-
3
- module Dronejob
4
- class WorkspaceDir
5
- include WorkspaceDir::Archive
6
-
7
- attr_accessor :workspace, :path
8
-
9
- def initialize(workspace, path="")
10
- @workspace = workspace
11
- @path = path
12
- end
13
-
14
- def to_s
15
- File.join(@workspace, @path)
16
- end
17
-
18
- def name
19
- File.basename(to_s)
20
- end
21
-
22
- def create
23
- FileUtils.mkdir_p(to_s)
24
- self
25
- end
26
-
27
- def exists?
28
- File.directory?(to_s)
29
- end
30
-
31
- def copy(target_dir)
32
- target_dir.parent_dir.create if !target_dir.parent_dir.exists?
33
- FileUtils.cp_r(to_s, target_dir.to_s)
34
- self
35
- end
36
-
37
- def move(target_dir)
38
- target_dir.parent_dir.create unless target_dir.parent_dir.exists?
39
- FileUtils.mv(to_s, target_dir.to_s)
40
- self
41
- end
42
-
43
- def delete!
44
- FileUtils.rm_rf(to_s)
45
- end
46
-
47
- def reset!
48
- delete!
49
- create
50
- end
51
-
52
- def file(file_path)
53
- WorkspaceFile.new(@workspace, File.join(@path, file_path))
54
- end
55
-
56
- def dir(dir_path)
57
- WorkspaceDir.new(@workspace, File.join(@path, dir_path))
58
- end
59
-
60
- def root_dir
61
- WorkspaceDir.new(@workspace, "")
62
- end
63
-
64
- def parent_dir
65
- root_dir.dir(File.expand_path("..", @path))
66
- end
67
-
68
- def children(glob="*")
69
- entries = []
70
- Dir.chdir(to_s) do
71
- Dir[glob].each do |path|
72
- entries.push(dir(path))
73
- end
74
- end
75
- entries
76
- end
77
-
78
- def files(glob="*")
79
- entries = []
80
- Dir.chdir(to_s) do
81
- Dir[glob].each do |path|
82
- entries.push(file(path))
83
- end
84
- end
85
- entries
86
- end
87
-
88
- def each_dir(&block)
89
- children.each do |subdir|
90
- block.call(subdir)
91
- end
92
- end
93
- end
94
- end
@@ -1,19 +0,0 @@
1
- module Dronejob
2
- class WorkspaceDir
3
- module Archive
4
- extend ActiveSupport::Concern
5
-
6
- def compress(target_file)
7
- target_file.delete!
8
- require "zip"
9
- Zip::File.open(target_file.to_s, 'w') do |zipfile|
10
- Dir["#{to_s}/**/**"].each do |file|
11
- path = file.sub("#{to_s}/",'')
12
- zipfile.add(path, file) unless block_given? and !yield(path)
13
- end
14
- end
15
- self
16
- end
17
- end
18
- end
19
- end
@@ -1,108 +0,0 @@
1
- require "action_dispatch/http/mime_type"
2
- require 'dronejob/workspace_file/archive'
3
- require 'dronejob/workspace_file/media'
4
- require 'dronejob/workspace_file/net'
5
- require 'dronejob/workspace_file/parse'
6
-
7
- module Dronejob
8
- class WorkspaceFile
9
- include WorkspaceFile::Archive
10
- include WorkspaceFile::Media
11
- include WorkspaceFile::Net
12
- include WorkspaceFile::Parse
13
-
14
- attr_accessor :workspace, :path
15
-
16
- def initialize(workspace, path)
17
- @workspace = workspace
18
- @path = path
19
- end
20
-
21
- def set(data)
22
- @contents = data
23
- self
24
- end
25
-
26
- def contents
27
- @contents ||= read
28
- end
29
-
30
- def replace(key, value)
31
- contents.gsub!(key, value)
32
- self
33
- end
34
-
35
- def write(data=nil)
36
- data ||= @contents
37
- dir.create if !dir.exists?
38
- File.open(to_s, "wb") {|file| file << data}
39
- self
40
- end
41
-
42
- def copy(target_file)
43
- target_file.dir.create if !target_file.dir.exists?
44
- FileUtils.cp(to_s, target_file.to_s)
45
- end
46
-
47
- def to_s
48
- File.join(@workspace, @path)
49
- end
50
-
51
- def relative_path(relative_dir=nil)
52
- if relative_dir
53
- relative_dir = relative_dir.dir if relative_dir.class == WorkspaceFile
54
- first = Pathname.new(relative_dir.path)
55
- second = Pathname.new(path)
56
- second.relative_path_from(first).to_s
57
- else
58
- @path.gsub(/^\//, "")
59
- end
60
- end
61
-
62
- def absolute_path
63
- File.absolute_path(to_s)
64
- end
65
-
66
- def exists?
67
- File.exists?(to_s)
68
- end
69
-
70
- def read
71
- File.open(to_s).read
72
- end
73
-
74
- def dir
75
- WorkspaceDir.new(@workspace, File.dirname(@path))
76
- end
77
-
78
- def delete!
79
- FileUtils.rm_f(to_s)
80
- end
81
-
82
- def name
83
- "#{basename}.#{extension}"
84
- end
85
-
86
- def basename
87
- File.basename(path, ".*")
88
- end
89
-
90
- def extension
91
- File.extname(to_s).gsub(/^\./, "")
92
- end
93
-
94
- def mimetype
95
- Mime::Type.lookup_by_extension(extension).to_s
96
- end
97
-
98
- def rename!(filename)
99
- FileUtils.mv(to_s, dir.file(filename).to_s) if exists?
100
- @path = dir.file(filename).path
101
- end
102
-
103
- def valid_p12?(password="")
104
- require "openssl"
105
- OpenSSL::PKCS12::new(contents, password).certificate.not_after > DateTime.now rescue false
106
- end
107
- end
108
- end
@@ -1,49 +0,0 @@
1
- module Dronejob
2
- class WorkspaceFile
3
- module Archive
4
- extend ActiveSupport::Concern
5
-
6
-
7
- def extract(target_dir)
8
- target_dir.create
9
- if extension == "gz"
10
- extract_gz(target_dir)
11
- else
12
- extract_zip(target_dir)
13
- end
14
- self
15
- end
16
-
17
- private
18
-
19
- def extract_zip(target_dir)
20
- require "zip"
21
- Zip::File.open(to_s) do |archive|
22
- archive.each do |entry|
23
- extract_dir = target_dir.file(entry.name).dir
24
- extract_dir.create unless extract_dir.exists?
25
- entry.extract(File.join(target_dir.to_s, entry.name))
26
- end
27
- end
28
- end
29
-
30
- def extract_gz(target_dir)
31
- require 'rubygems/package'
32
- workspace_dir = target_dir.root_dir
33
- archive = Gem::Package::TarReader.new(Zlib::GzipReader.open(to_s))
34
- archive.rewind
35
- archive.each do |entry|
36
- if entry.directory?
37
- archive_dir = workspace_dir.dir(entry.full_name)
38
- archive_dir.create
39
- elsif entry.file?
40
- archive_file = workspace_dir.file(entry.full_name)
41
- archive_file.write(entry.read)
42
- end
43
- end
44
- archive.close
45
- end
46
-
47
- end
48
- end
49
- end
@@ -1,92 +0,0 @@
1
- require "piet"
2
-
3
- module Dronejob
4
- class WorkspaceFile
5
- module Media
6
- extend ActiveSupport::Concern
7
-
8
- def image?
9
- ["jpg", "jpeg", "gif", "png"].include?(extension)
10
- end
11
-
12
- def video?
13
- ["mp4"].include?(extension)
14
- end
15
-
16
- def audio?
17
- ["mp3"].include?(extension)
18
- end
19
-
20
- def cache_assets(cache_dir, &block)
21
- hydra = Typhoeus::Hydra.new(max_concurrency: 10)
22
- html = contents
23
- html.scan(/["'(]{1}((https:\/\/|http:\/\/|\/\/)[^"')]+\.(jpg|jpeg|png|gif|mp3|mp4))["')?#]{1}/).map{|match| match[0]}.uniq.each do |url|
24
- original_url = url.clone
25
- url = "http:#{url}" if url[0..1] == "//"
26
- ext = File.extname(url).gsub(/^\./, "")
27
- cache_file = cache_dir.file("#{Digest::MD5.hexdigest(url)}.#{ext}")
28
- request = cache_file.queue_download(url) do
29
- cache_file.optimize!
30
- if original_url[0..1] == "//"
31
- html.gsub!("http:#{original_url}", cache_file.relative_path)
32
- html.gsub!("https:#{original_url}", cache_file.relative_path)
33
- end
34
- html.gsub!(original_url, cache_file.relative_path)
35
- end
36
- hydra.queue(request)
37
- end
38
- hydra.run
39
- set(html)
40
- self
41
- end
42
-
43
- def fit(width, height)
44
- require "rmagick"
45
- image = Magick::Image.read(to_s).first
46
- image.change_geometry!("#{width}x^#{height}") { |cols, rows| image.thumbnail!(cols, rows) }
47
- image.crop!(0, 0, width, height)
48
- image.write(to_s) { self.quality = Dronejob::Base.option(:image_quality) }
49
- image.destroy!
50
- end
51
-
52
- def optimize!
53
- return false if !exists? or !image?
54
- optimize_png! if ["png"].include?(extension)
55
- optimize_jpg! if ["jpg", "jpeg"].include?(extension)
56
-
57
- # optimize
58
- Piet.optimize(to_s, verbose: false)
59
-
60
- true
61
- end
62
-
63
- private
64
-
65
- def optimize_jpg!
66
- require "rmagick"
67
- image_max_width = Dronejob::Base.option(:image_max_width)
68
- image = Magick::Image.read(to_s).first
69
- image.change_geometry!("#{image_max_width}>x"){|cols, rows, img| img.resize!(cols, rows)} if image.columns > image_max_width
70
- image.write(to_s) { self.quality = Dronejob::Base.option(:image_quality) }
71
- image.destroy!
72
- end
73
-
74
- def optimize_png!
75
- require "rmagick"
76
- image_max_width = Dronejob::Base.option(:image_max_width)
77
- image = Magick::Image.read(to_s).first
78
- image.change_geometry!("#{image_max_width}>x"){|cols, rows, img| img.resize!(cols, rows)} if image.columns > image_max_width
79
-
80
- # Convert to jpg
81
- if !image.alpha? or image.resize(1,1).pixel_color(0,0).opacity == 0
82
- image.format = "JPG"
83
- rename!("#{basename}.jpg")
84
- end
85
-
86
- image.write(to_s) { self.quality = Dronejob::Base.option(:image_quality) }
87
- image.destroy!
88
- end
89
-
90
- end
91
- end
92
- end
@@ -1,57 +0,0 @@
1
- require "typhoeus"
2
- require "open-uri"
3
- require "open_uri_redirections"
4
-
5
- module Dronejob
6
- class WorkspaceFile
7
- module Net
8
- extend ActiveSupport::Concern
9
-
10
- def cdn_upload(key, options={})
11
- options = {
12
- project_id: Dronejob::Base.option(:google_cloud_project_id),
13
- bucket: Dronejob::Base.option(:cdn_bucket),
14
- content_type: mimetype
15
- }.merge(options)
16
- require "google/cloud/storage"
17
- storage = Google::Cloud::Storage.new(project: options[:project_id], keyfile: File.expand_path("~/gcloud-service-account.json"))
18
- bucket = storage.bucket(options[:bucket])
19
- bucket.create_file(to_s, key, acl: "public", content_type: options[:content_type])
20
- end
21
-
22
- def cdn_download(key, cdn_bucket: nil, project_id: nil, secure: false)
23
- cdn_bucket ||= Dronejob::Base.option(:cdn_bucket)
24
- project_id ||= Dronejob::Base.option(:google_cloud_project_id)
25
- if secure
26
- require "google/cloud/storage"
27
- storage = Google::Cloud::Storage.new(project: project_id, keyfile: File.expand_path("~/gcloud-service-account.json"))
28
- bucket = storage.bucket(cdn_bucket)
29
- bucket.file(key)&.download(to_s)
30
- self
31
- else
32
- download("#{Dronejob::Base.option(:cdn_scheme)}://#{cdn_bucket}/#{key}")
33
- end
34
- end
35
-
36
- def queue_download(url, &block)
37
- url = "http:#{url}" if url[0..1] == "//"
38
- request = Typhoeus::Request.new(url)
39
- request.on_complete do |response|
40
- if response.success?
41
- write(response.body)
42
- block.call(self) if !block.nil?
43
- end
44
- end
45
- request
46
- end
47
-
48
- def download(url, opts={})
49
- url = "http:#{url}" if url[0..1] == "//"
50
- contents = open(url, { ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, allow_redirections: :safe }.merge(opts)).read
51
- dir.create if !dir.exists?
52
- write(contents)
53
- self
54
- end
55
- end
56
- end
57
- end
@@ -1,37 +0,0 @@
1
- module Dronejob
2
- class WorkspaceFile
3
- module Parse
4
- extend ActiveSupport::Concern
5
-
6
- def pbxproj_replace(data)
7
- data.each do |key, value|
8
- replace(/#{key} = .*;/, "#{key} = \"#{value}\";") if !value.nil?
9
- end
10
- write
11
- self
12
- end
13
-
14
- def with_plist(&block)
15
- require "plist"
16
- plist = Plist.parse_xml(to_s)
17
- block.call(plist)
18
- write(Plist::Emit.dump(plist))
19
- end
20
-
21
- def read_json
22
- JSON.parse(read)
23
- end
24
-
25
- def read_yaml
26
- Psych.load_file(to_s)
27
- end
28
-
29
- def read_haml(options)
30
- require "haml"
31
- engine = Haml::Engine.new(read)
32
- engine.render(Object.new, options)
33
- end
34
-
35
- end
36
- end
37
- end