fclay 0.1.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d10bc34257d53b487c6d8761fdf7abae71dd2b13
4
+ data.tar.gz: 261755f3588f9bc1e690f1df4bfa848734d2d2c7
5
+ SHA512:
6
+ metadata.gz: 1ea4d99a01dcfb4c1fbc3433236cc2daf7a6840b94943bac2a2f28b4a4d286c0ed34e4a8065f621ce35f465e2963fdde35697399a4a5b61a593e6ed450b65ba8
7
+ data.tar.gz: 703f9e5f14ed0b250f6f212c9112b51a8f82999c1deb65510f0b70b2c7665edc65cf2630403599fe67f96414600555f8715ee99fa670bf2e043b989c0611a5af
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fclay.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rustam Galiev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # Fclay
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fclay`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fclay'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fclay
22
+
23
+ ## Usage
24
+
25
+ Create migration for model:
26
+
27
+ $ rails g fclay User
28
+
29
+ Add `has_fclay_attachment` to model file:
30
+
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ has_fclay_attachment without: [:process, :upload, :delete], content_type: "application/json", extension: 'png', styles: [:thumb,:original]
34
+ end
35
+ ```
36
+
37
+ Now `User` model has `file_url` method
38
+
39
+ ```ruby
40
+ User.last.file_url
41
+ ```
42
+
43
+ ## Configuration
44
+
45
+ Configuring with `config\initializers\fclay.rb`:
46
+
47
+ ```ruby
48
+
49
+ require 'fclay'
50
+ Fclay.configure do |config|
51
+ config.local_storage_host = "http://mysite.com"
52
+ config.storage_policy = "s3"
53
+ config.remote_storages = {
54
+ "s3" => {
55
+ kind: 'aws',
56
+ storage_policy: "storage_policy_name",
57
+ }
58
+ }
59
+ end
60
+ ```
61
+
62
+
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fclay.
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
78
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fclay"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fclay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fclay"
8
+ spec.version = Fclay::VERSION
9
+ spec.authors = ["Rustam Galiev"]
10
+ spec.email = ["qweromail@gmail.com"]
11
+
12
+ spec.summary = "Summary"
13
+ spec.description = "Description"
14
+ spec.homepage = "http://foo.bar"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,56 @@
1
+ require "fclay/version"
2
+ require 'fclay/configuration'
3
+ require 'fclay/upload_job'
4
+ require "fclay/attachment"
5
+ require "fclay/includer"
6
+ require "fclay/remote_storage"
7
+
8
+ module Fclay
9
+ class << self
10
+ def configure(&block)
11
+ yield(configuration)
12
+ validate_configuration
13
+ configuration
14
+ end
15
+
16
+ def remote_storage
17
+ @_remote_storage ||= RemoteStorage.new(configuration.storage_policy,configuration.remote_storages[configuration.storage_policy])
18
+ end
19
+
20
+ def configuration
21
+ @_configuration ||= Configuration.new
22
+ end
23
+
24
+ def validate_configuration
25
+ validate_remote_storages unless configuration.storage_policy == :local
26
+ end
27
+
28
+ def validate_remote_storages
29
+ raise ArgumentError, "remote storage '#{configuration.storage_policy}' not set" unless configuration.remote_storages[configuration.storage_policy].present?
30
+
31
+ validate_s3 if configuration.remote_storages[configuration.storage_policy][:kind] == "s3"
32
+
33
+
34
+ end
35
+
36
+ def validate_s3
37
+
38
+ raise ArgumentError, "Aws constant not definded. Missed aws-sdk gem?" unless defined? Aws
39
+ %w(AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION).each do |key|
40
+ raise ArgumentError, "Missed ENV[\"#{key}\"]" unless ENV[key]
41
+ end
42
+
43
+
44
+ end
45
+
46
+
47
+ ActiveSupport.on_load(:active_record) do
48
+ extend Fclay::Includer
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+
56
+
@@ -0,0 +1,228 @@
1
+ require 'base64'
2
+
3
+ module Fclay
4
+ module Attachment
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ CALLBACKS = [:process,:upload,:delete]
9
+
10
+
11
+ included do
12
+
13
+ callbacks = CALLBACKS
14
+
15
+ case fclay_options[:without].class.name
16
+ when "Symbol"
17
+ callbacks.clear if fclay_options[:without] == :all
18
+ when "Array"
19
+ callbacks -= fclay_options[:without]
20
+ end
21
+
22
+ before_save :process_file if callbacks.include? :process
23
+ after_save :upload_later if callbacks.include? :upload
24
+ before_destroy :delete_files if callbacks.include? :delete
25
+
26
+ end
27
+
28
+ attr_accessor :file
29
+
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
+
39
+ end
40
+
41
+ def need_upload
42
+ Fclay.configuration.storage_policy != :local && self.file_location == "local"
43
+ end
44
+
45
+ def upload_later
46
+ Fclay::UploadJob.perform_later self.class.name,self.id if need_upload
47
+ end
48
+
49
+ def upload
50
+ Fclay::Attachment.upload self.class.name,self.id
51
+ end
52
+
53
+ def self.upload type,id
54
+
55
+ type = type.safe_constantize
56
+ return unless type
57
+ uploading_object = type.find_by_id(id)
58
+ return if !uploading_object || !uploading_object.need_upload
59
+ content_type = uploading_object.try(:content_type)
60
+ bucket = Fclay.remote_storage.bucket_object
61
+
62
+ (uploading_object.class.fclay_options[:styles] || [nil]).each do |style|
63
+ obj = bucket.object(uploading_object.remote_file_path(style))
64
+ obj.put({
65
+ body: File.read(uploading_object.local_file_path(style)),
66
+ acl: "public-read",
67
+ content_type: uploading_object.class.fclay_options[:content_type]
68
+ })
69
+ end
70
+
71
+ uploading_object.update_attributes(:file_status => 'idle', :file_location => Fclay.remote_storage.name)
72
+ uploading_object.delete_local_files
73
+ uploading_object.try(:uploaded)
74
+
75
+ end
76
+
77
+ def fclay_attachment_presence
78
+ errors.add(:file, 'must be present') if id.blank? && !@file
79
+ end
80
+
81
+ def file_size_mb
82
+
83
+ "#{((self.file_size >> 10).to_f / 1024).round(2)} Mb" if self.file_size
84
+
85
+ end
86
+
87
+ def file_url(style=nil)
88
+ case self.file_location
89
+ when "external_link"
90
+ self.file_name
91
+ when "local"
92
+ local_file_url(style)
93
+ else
94
+ remote_file_url(style)
95
+ end
96
+ end
97
+
98
+ def remote_file_url(style=nil)
99
+ "http://#{Fclay.remote_storage.bucket_name}.s3.amazonaws.com/#{remote_file_path(style)}"
100
+ end
101
+
102
+ def local_file_path(style=nil)
103
+
104
+ local_file_dir(style) + "/" + file_name
105
+
106
+ end
107
+
108
+ def local_file_url(style=nil)
109
+ url = Fclay.configuration.local_storage_host
110
+ url += "#{Fclay.configuration.local_url}/#{self.class.name.tableize}"
111
+ url += "/#{style.to_s}" if style
112
+ url += "/#{file_name}"
113
+ url
114
+ end
115
+
116
+ def short_local_file_url(style=nil)
117
+
118
+ end
119
+
120
+ def local_file_dir(style=nil)
121
+ dir = "#{Rails.root.to_s + Fclay.configuration.local_folder}/#{self.class.name.tableize}"
122
+ dir += "/#{style.to_s}" if style
123
+ dir
124
+ end
125
+
126
+ def remote_file_path(style=nil)
127
+ path = ""
128
+ path += "#{self.class.name.tableize}"
129
+ path += "/#{style.to_s}" if style
130
+ path += "/#{file_name}"
131
+ path
132
+ end
133
+
134
+ def delete_tmp_file
135
+ FileUtils.rm(@file.try(:path) || @file[:path],{:force => true}) if @file
136
+ @file = nil
137
+ end
138
+
139
+ def create_dirs
140
+
141
+ (self.class.fclay_options[:styles] || [nil]).each do |style|
142
+ FileUtils.mkdir_p(local_file_dir(style))
143
+ end
144
+
145
+ end
146
+
147
+ def process_file
148
+
149
+ return unless @file
150
+
151
+ delete_files
152
+
153
+ path = @file.try(:path) || @file[:path]
154
+ return unless path
155
+
156
+ if path[0..3] == "http"
157
+ self.file_status = 'idle'
158
+ self.file_location = 'external_link'
159
+ self.file_name = path
160
+ else
161
+ create_dirs
162
+ fetch_file_name
163
+
164
+
165
+ FileUtils.mv(path,local_file_path)
166
+
167
+ `chmod 0755 #{local_file_path}`
168
+
169
+ delete_tmp_file
170
+ set_file_size
171
+ self.file_location = 'local'
172
+ self.file_status = need_upload ? "processing" : "idle"
173
+ end
174
+ end
175
+
176
+ def fetch_file_name
177
+
178
+ ext = self.class.fclay_options[:extension]
179
+ if ext && ext != false && @file.try(:original_filename)
180
+ filename_part = @file.original_filename.split(".")
181
+ ext = filename_part.last if filename_part.size > 1
182
+ end
183
+
184
+ self.file_name = try(:fclay_attachment_filename)
185
+ self.file_name = SecureRandom.hex unless self.file_name
186
+ self.file_name += ".#{ext}" if ext
187
+
188
+ end
189
+
190
+ def delete_local_files
191
+
192
+ begin
193
+ (self.class.fclay_options[:styles] || [nil]).each do |style|
194
+ FileUtils.rm(local_file_path(style),{:force => true})
195
+ end
196
+ rescue
197
+ Rails.logger.info "Deleting Media #{id} sync file not found"
198
+ end
199
+ true
200
+
201
+ end
202
+
203
+ def delete_remote_files
204
+
205
+ (self.class.fclay_options[:styles] || [nil]).each do |style|
206
+ Fclay.remote_storage.bucket_object.object(remote_file_path(style)).delete
207
+ end
208
+ end
209
+
210
+ def set_file_size style=nil
211
+ self.file_size = File.size local_file_path(style)
212
+ end
213
+
214
+ def self.resolve_file_url navigation_complex_id,type,file_name,style=nil
215
+
216
+ return "" if file_name.nil? || type.nil?
217
+
218
+ path = "http://s3.amazonaws.com/#{Fclay.remote_storage.bucket_name}"
219
+ path += "/navigation_complex/#{navigation_complex_id}" if navigation_complex_id
220
+ path += "/#{type}"
221
+ path += "/#{style.to_s}" if style
222
+ path += "/#{file_name}"
223
+ path
224
+ end
225
+
226
+ end
227
+ end
228
+
@@ -0,0 +1,22 @@
1
+ module Fclay
2
+ class Configuration
3
+ attr_accessor :local_storage_host
4
+ attr_accessor :storage_policy
5
+ attr_accessor :remote_storages
6
+ attr_accessor :local_url
7
+ attr_accessor :local_folder
8
+
9
+ def initialize
10
+ @local_storage_host = ""
11
+ @storage_policy = :local
12
+ @remote_storages = {}
13
+ @local_url = "/system/local_storage"
14
+ @local_folder = "/public#{@local_url}"
15
+ end
16
+
17
+ def storages(&block)
18
+ yield(@remote_storages)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Fclay
2
+ module Includer
3
+
4
+ def has_fclay_attachment options = {}
5
+ @fclay_options = options
6
+
7
+ class_eval do
8
+ def self.fclay_options
9
+ @fclay_options
10
+ end
11
+ end
12
+
13
+ include Fclay::Attachment
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ module Fclay
2
+ class RemoteStorage
3
+
4
+ attr_reader :name
5
+
6
+ def initialize name,data
7
+ @name = name.to_s
8
+ @data = data
9
+ end
10
+
11
+ def s3?
12
+ @data[:kind] == "s3"
13
+ end
14
+
15
+ def bucket_object
16
+ return "" unless s3?
17
+ s3 = Aws::S3::Resource.new
18
+ s3.bucket(bucket_name)
19
+ end
20
+
21
+ def bucket_name
22
+ return "" unless s3?
23
+ @data[:bucket]
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module Fclay
2
+ class UploadJob < ActiveJob::Base
3
+ queue_as :default
4
+
5
+ def perform type,id
6
+ Fclay::Attachment.upload(type,id)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Fclay
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class Fclay::ConfigGenerator < Rails::Generators::Base
4
+
5
+ def create_initializer_file
6
+ create_file "config/initializers/fclay.rb" do
7
+ "Fclay.configure do |config|
8
+ #config.local_storage_host = 'http://mysite.com'
9
+ #config.storage_policy = :s3
10
+ #config.storages do |storages|
11
+ # storages[:s3] = {
12
+ # :kind => 's3',
13
+ # :bucket => 'bucket-name'
14
+ # }
15
+ #end
16
+ end"
17
+
18
+ end
19
+ end
20
+
21
+
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class FclayGenerator < ActiveRecord::Generators::Base
4
+
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path('../templates', __FILE__)
8
+ end
9
+
10
+ def generate_migration
11
+ migration_template "fclay_migration.rb.erb", "db/migrate/#{migration_file_name}"
12
+ end
13
+
14
+ def migration_name
15
+ "add_fclay_fields_to_#{name.underscore.pluralize}"
16
+ end
17
+
18
+ def migration_file_name
19
+ "#{migration_name}.rb"
20
+ end
21
+
22
+ def migration_class_name
23
+ migration_name.camelize
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def change
3
+
4
+ add_column :<%= table_name %>, :file_name, :string
5
+ add_column :<%= table_name %>, :file_size, :integer
6
+ add_column :<%= table_name %>, :file_status, :string, :default => "new"
7
+ add_column :<%= table_name %>, :file_location, :string
8
+ add_column :<%= table_name %>, :content_type, :string
9
+
10
+ end
11
+
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fclay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Rustam Galiev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Description
56
+ email:
57
+ - qweromail@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - fclay.gemspec
72
+ - lib/fclay.rb
73
+ - lib/fclay/attachment.rb
74
+ - lib/fclay/configuration.rb
75
+ - lib/fclay/includer.rb
76
+ - lib/fclay/remote_storage.rb
77
+ - lib/fclay/upload_job.rb
78
+ - lib/fclay/version.rb
79
+ - lib/generators/fclay/config/config_generator.rb
80
+ - lib/generators/fclay/fclay_generator.rb
81
+ - lib/generators/fclay/templates/fclay_migration.rb.erb
82
+ homepage: http://foo.bar
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Summary
106
+ test_files: []