fclay 1.49.0 → 1.50.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: add0decdc4f678145b9ddc714b3ae55add7f39cd
4
- data.tar.gz: 2030edd4a18c7a3d9570cf1d58af8076ae3822ef
3
+ metadata.gz: 0cd4d68a9d0de6f8479283f6dd5abb91ef0322ed
4
+ data.tar.gz: b1f5fb353758cc9dd143421f670ae745492489fc
5
5
  SHA512:
6
- metadata.gz: 060d5315197a69415598616fbf73355d715225da9df0c98b8bebfbc4649904a71ad928606720d45bcfb264e152d4278b3d6fc350b1269d63d714138fe2b09841
7
- data.tar.gz: 1d976d08a30b4cada18df30fa492f6ea05cac905c298f6b1482953ffc327024a11e6167a785964fc13efd1888be9ca1ab9938d838e4c8bc210c899b53aa75ec6
6
+ metadata.gz: 527bae9df327302246fda7d59fd1c0c1fa0b3bde9e79d752acf06b811e48f93fd37ffc32644ae13d66993478038864ac679e553306d39b06166bbc14d1e8fe7a
7
+ data.tar.gz: 011fd5a2a2d059637686282f7e6d7cd2f7e800414d48d5a1a29fedded072458b0a642849bae89a0fc86144ec1eeb9d2909e2bd310a5013d3205eab2ee9616738
@@ -4,53 +4,51 @@ require 'fclay/upload_job'
4
4
  require "fclay/attachment"
5
5
  require "fclay/includer"
6
6
  require "fclay/remote_storage"
7
+ require 'fclay/remote_storage/provider'
7
8
 
8
9
  module Fclay
9
- class << self
10
+ class << self
10
11
  def configure(&block)
11
12
  yield(configuration)
12
13
  validate_configuration
13
14
  configuration
14
15
  end
15
-
16
+
16
17
  def remote_storage
17
18
  @_remote_storage ||= RemoteStorage.new(configuration.storage_policy,configuration.remote_storages[configuration.storage_policy])
18
19
  end
19
-
20
+
20
21
  def configuration
21
22
  @_configuration ||= Configuration.new
22
23
  end
23
-
24
+
24
25
  def validate_configuration
25
- validate_remote_storages unless configuration.storage_policy == :local
26
+ validate_remote_storages unless configuration.storage_policy == :local
26
27
  end
27
-
28
+
28
29
  def validate_remote_storages
29
30
  raise ArgumentError, "remote storage '#{configuration.storage_policy}' not set" unless configuration.remote_storages[configuration.storage_policy].present?
30
-
31
+
31
32
  validate_s3 if configuration.remote_storages[configuration.storage_policy][:kind] == "s3"
32
-
33
-
33
+
34
+
34
35
  end
35
-
36
+
36
37
  def validate_s3
37
-
38
+
38
39
  raise ArgumentError, "Aws constant not definded. Missed aws-sdk gem?" unless defined? Aws
39
40
  %w(AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION).each do |key|
40
41
  raise ArgumentError, "Missed ENV[\"#{key}\"]" unless ENV[key]
41
42
  end
42
-
43
-
43
+
44
+
44
45
  end
45
-
46
-
46
+
47
+
47
48
  ActiveSupport.on_load(:active_record) do
48
49
  extend Fclay::Includer
49
50
  end
50
-
51
- end
52
-
53
- end
54
-
55
51
 
52
+ end
56
53
 
54
+ end
@@ -28,14 +28,7 @@ module Fclay
28
28
  attr_accessor :file
29
29
 
30
30
  def delete_files
31
-
32
- case self.file_location
33
- when 's3'
34
- delete_remote_files
35
- when 'local'
36
- delete_local_files
37
- end
38
-
31
+ self.file_location == 'local' ? delete_local_files : delete_remote_files
39
32
  end
40
33
 
41
34
  def safe_delete_files
@@ -77,30 +70,14 @@ module Fclay
77
70
  Fclay::Attachment.upload self.class.name,self.id
78
71
  end
79
72
 
80
- def self.upload type,id
73
+ def self.upload(type, id)
81
74
  type = type.safe_constantize
82
75
  return unless type
83
76
  uploading_object = type.find_by_id(id)
84
77
  uploading_object.try(:log,"Fclay::upload() called, uploading_object: #{uploading_object}, uploading_object.need_upload: #{uploading_object.try(:need_upload)}")
85
78
  return if !uploading_object || !uploading_object.need_upload
86
- content_type = uploading_object.try(:content_type)
87
- bucket = Fclay.remote_storage.bucket_object
88
-
89
- uploading_object.try(:log,"Start uploading")
90
- (uploading_object.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
91
- obj = bucket.object(uploading_object.remote_file_path(style))
92
- obj.put({
93
- body: File.read(uploading_object.local_file_path(style)),
94
- acl: "public-read",
95
- content_type: content_type
96
- })
97
- end
98
-
99
- type.where(:id => id).update_all(:file_status => 'idle', :file_location => Fclay.remote_storage.name)
100
- uploading_object.try(:log,"Sucessful uploaded! file_status: 'idle', file_location: #{Fclay.remote_storage.name}")
101
- uploading_object.delete_local_files
102
- uploading_object.try(:uploaded)
103
79
 
80
+ Fclay::RemoteStorage::Provider.upload(uploading_object)
104
81
 
105
82
  end
106
83
 
@@ -115,15 +92,17 @@ module Fclay
115
92
  end
116
93
 
117
94
  def file_url(style=nil)
118
- return "" unless self.file_location
95
+ return '' unless self.file_location
96
+
119
97
  case self.file_location
120
- when "external_link"
121
- self.file_name
122
- when "local"
123
- local_file_url(style)
124
- else
125
- remote_file_url(style)
126
- end
98
+ when "external_link"
99
+ self.file_name
100
+ when "local"
101
+ local_file_url(style)
102
+ else
103
+ remote_file_url(style)
104
+ end
105
+
127
106
  end
128
107
 
129
108
  def final_file_url(style=nil)
@@ -136,8 +115,9 @@ module Fclay
136
115
  end
137
116
 
138
117
  def remote_file_url(style=nil)
139
- host = Fclay.remote_storage.host || "https://#{Fclay.remote_storage.bucket_name}.s3.amazonaws.com"
140
- "#{host}/#{remote_file_path(style)}"
118
+ # host = Fclay.remote_storage.host || "https://#{Fclay.remote_storage.bucket_name}.s3.amazonaws.com"
119
+ # "#{host}/#{remote_file_path(style)}"
120
+ Fclay::RemoteStorage::Provider.remote_file_url(self, style)
141
121
  end
142
122
 
143
123
  def local_file_path(style=nil)
@@ -164,7 +144,7 @@ module Fclay
164
144
  dir
165
145
  end
166
146
 
167
- def remote_file_path(style=nil)
147
+ def remote_file_path(style = nil)
168
148
  path = ""
169
149
  path += "#{self.class.name.tableize}"
170
150
  path += "/#{style.to_s}" if style
@@ -179,7 +159,7 @@ module Fclay
179
159
 
180
160
  def create_dirs
181
161
 
182
- (self.class.fclay_options[:styles] || [nil]).each do |style|
162
+ (self.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
183
163
  FileUtils.mkdir_p(local_file_dir(style))
184
164
  end
185
165
 
@@ -206,7 +186,7 @@ module Fclay
206
186
  create_dirs
207
187
  fetch_file_name
208
188
 
209
- (self.class.fclay_options[:styles] || [nil]).each do |style|
189
+ (self.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
210
190
  FileUtils.cp(path,local_file_path(style))
211
191
  `chmod 777 #{local_file_path(style)}`
212
192
  end
@@ -240,7 +220,7 @@ module Fclay
240
220
  def delete_local_files
241
221
 
242
222
  begin
243
- (self.class.fclay_options[:styles] || [nil]).each do |style|
223
+ (self.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
244
224
  FileUtils.rm(local_file_path(style),{:force => true})
245
225
  end
246
226
  rescue
@@ -251,17 +231,14 @@ module Fclay
251
231
  end
252
232
 
253
233
  def delete_remote_files
254
-
255
- (self.class.fclay_options[:styles] || [nil]).each do |style|
256
- Fclay.remote_storage.bucket_object.object(remote_file_path(style)).delete
257
- end
234
+ Fclay::RemoteStorage::Provider.delete_files(self)
258
235
  end
259
236
 
260
237
  def set_file_size style=nil
261
238
  self.file_size = File.size local_file_path(style)
262
239
  end
263
240
 
264
- def self.resolve_file_url navigation_complex_id,type,file_name,style=nil
241
+ def self.resolve_file_url(navigation_complex_id, type, file_name, style = nil)
265
242
 
266
243
  return "" if file_name.nil? || type.nil?
267
244
 
@@ -0,0 +1,17 @@
1
+ class Fclay::RemoteStorage::Base
2
+
3
+ attr_accessor :name, :uploading_object, :storage, :options
4
+
5
+ def upload(options = {})
6
+ uploading_object.update(file_status: 'idle', file_location: name)
7
+ uploading_object.try(:log, "Sucessful uploaded! file_status: 'idle', file_location: #{name}")
8
+ uploading_object.try(:uploaded)
9
+ end
10
+
11
+ def delete_files
12
+ end
13
+
14
+ def self.url
15
+ end
16
+
17
+ end
@@ -0,0 +1,64 @@
1
+ require_relative './ssh'
2
+ require_relative './s3'
3
+
4
+ class Fclay::RemoteStorage::Provider
5
+
6
+ def self.get_provider_class(storage)
7
+ case storage
8
+ when 'ssh'
9
+ Fclay::RemoteStorage::SSH
10
+ when 's3'
11
+ Fclay::RemoteStorage::S3
12
+ else
13
+ raise 'Unsupported storage type'
14
+ end
15
+ end
16
+
17
+ def self.provider_for(storage, uploading_object)
18
+ klass = get_provider_class(storage)
19
+ klass.new(uploading_object)
20
+ end
21
+
22
+
23
+ def self.upload(uploading_object)
24
+ options = uploading_object.class.fclay_options
25
+
26
+ if options[:primary].present?
27
+ storage = provider_for(options[:primary], uploading_object)
28
+ storage.upload
29
+
30
+ if options[:secondary].present?
31
+ storage = provider_for(options[:secondary], uploading_object)
32
+ storage.upload(without_update: true)
33
+ end
34
+ else
35
+ storage = provider_for(Fclay.configuration.storage_policy, uploading_object)
36
+ storage.upload
37
+ end
38
+
39
+ uploading_object.delete_local_files
40
+ end
41
+
42
+ def self.delete_files(model)
43
+ options = model.class.fclay_options
44
+
45
+ if options[:primary].present?
46
+ storage = provider_for(options[:primary], model)
47
+ storage.delete_files
48
+
49
+ if options[:secondary].present?
50
+ storage = provider_for(options[:secondary], model)
51
+ storage.delete_files
52
+ end
53
+ else
54
+ storage = provider_for(Fclay.configuration.storage_policy, model)
55
+ storage.delete_files
56
+ end
57
+ end
58
+
59
+ def self.remote_file_url(obj, style = nil)
60
+ klass = get_provider_class(obj.file_location)
61
+ "#{klass.url}/#{obj.remote_file_path(style)}"
62
+ end
63
+
64
+ end
@@ -0,0 +1,48 @@
1
+ require_relative './base'
2
+
3
+ class Fclay::RemoteStorage::S3 < Fclay::RemoteStorage::Base
4
+
5
+ def initialize(uploading_object)
6
+ @name = 's3'
7
+ @uploading_object = uploading_object
8
+ @storage = Fclay.configuration.remote_storages['s3']
9
+ @options = @uploading_object.class.fclay_options
10
+ @bucket = Aws::S3::Resource.new.bucket(bucket_name)
11
+ end
12
+
13
+ def upload(options = {})
14
+ (@options[:styles].try(:keys) || [nil]).each do |style|
15
+ obj = bucket_object(style)
16
+ obj.put({
17
+ body: File.read(uploading_object.local_file_path(style)),
18
+ acl: "public-read",
19
+ content_type: content_type
20
+ })
21
+ end
22
+
23
+ super unless options[:without_update]
24
+ end
25
+
26
+ def delete_files
27
+ (@options[:styles].try(:keys) || [nil]).each do |style|
28
+ bucket_object(style).delete
29
+ end
30
+ end
31
+
32
+ def bucket_object(style = nil)
33
+ @bucket.object(uploading_object.remote_file_path(style))
34
+ end
35
+
36
+ def self.url
37
+ "https://#{Fclay.configuration.remote_storages['s3'][:bucket]}.s3.amazonaws.com"
38
+ end
39
+
40
+ def content_type
41
+ uploading_object.try(:content_type)
42
+ end
43
+
44
+ def bucket_name
45
+ Fclay.configuration.remote_storages['s3'][:bucket]
46
+ end
47
+
48
+ end
@@ -0,0 +1,57 @@
1
+ require_relative './base'
2
+
3
+ class Fclay::RemoteStorage::SSH < Fclay::RemoteStorage::Base
4
+
5
+ def initialize(uploading_object)
6
+ @name = 'ssh'
7
+ @uploading_object = uploading_object
8
+ @storage = Fclay.configuration.remote_storages['ssh']
9
+ @options = @uploading_object.class.fclay_options
10
+ end
11
+
12
+ def upload(options = {})
13
+ (@options[:styles].try(:keys) || [nil]).each do |style|
14
+ command = "ssh #{hosting} 'mkdir -p #{remote_dir(style)}'"
15
+ system(command)
16
+ command = "scp -r #{local_file(style)} #{hosting}:#{remote_file(style)}"
17
+ system(command)
18
+ end
19
+
20
+ super unless options[:without_update]
21
+ end
22
+
23
+ def delete_files
24
+ (@options[:styles].try(:keys) || [nil]).each do |style|
25
+ command = "ssh #{hosting} 'rm #{remote_file(style)}'"
26
+ system(command)
27
+ end
28
+ end
29
+
30
+ def self.url
31
+ opts = Fclay.configuration.remote_storages['ssh']
32
+ "https://#{opts[:host]}"
33
+ end
34
+
35
+ def hosting
36
+ "#{storage[:user]}@#{storage[:host]}"
37
+ end
38
+
39
+ def hosting_path
40
+ "#{storage[:path]}/"
41
+ end
42
+
43
+ def local_file(style = nil)
44
+ @uploading_object.local_file_path(style)
45
+ end
46
+
47
+ def remote_dir(style = nil)
48
+ dir = "#{hosting_path}#{@uploading_object.class.name.tableize}/"
49
+ dir += "#{style}/" if style
50
+ dir
51
+ end
52
+
53
+ def remote_file(style = nil)
54
+ "#{hosting_path}#{@uploading_object.remote_file_path(style)}"
55
+ end
56
+
57
+ end
@@ -1,3 +1,3 @@
1
1
  module Fclay
2
- VERSION = "1.49.0"
2
+ VERSION = "1.50.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fclay
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.49.0
4
+ version: 1.50.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Galiev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-31 00:00:00.000000000 Z
11
+ date: 2019-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,6 +74,10 @@ files:
74
74
  - lib/fclay/configuration.rb
75
75
  - lib/fclay/includer.rb
76
76
  - lib/fclay/remote_storage.rb
77
+ - lib/fclay/remote_storage/base.rb
78
+ - lib/fclay/remote_storage/provider.rb
79
+ - lib/fclay/remote_storage/s3.rb
80
+ - lib/fclay/remote_storage/ssh.rb
77
81
  - lib/fclay/upload_job.rb
78
82
  - lib/fclay/version.rb
79
83
  - lib/generators/fclay/config/config_generator.rb