s3_assets 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/s3_assets/model.rb +78 -0
- data/lib/s3_assets/relations.rb +84 -0
- data/lib/s3_assets/uploader.rb +145 -0
- data/lib/s3_assets/utility.rb +59 -0
- data/lib/s3_assets/version.rb +3 -0
- data/lib/s3_assets.rb +13 -0
- data/s3_assets.gemspec +41 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: af411a79f576acaa039d841d70ee2130cfc53b2cbdde69d4f8940e04912887f3
|
4
|
+
data.tar.gz: 223b10fe3d2cbd8e56544e2960db4577667e81a3c5057d554c35c8ae81c6113a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 21d03fc789045db8bb185976d1d649c2006d8c6154d118daeb9921e3b5694d211854e506dafe1555515f1cf6b822e381084f285e48ee453badfe7e1dbfe80ccf
|
7
|
+
data.tar.gz: 08abcede4a4f734b8f926749ef592942d029d74a4af33be6638d2f29d41214d2c4e5eb486b1bf8cda28b1b561a70cbd49ec6cf8cb7b25838aa92f1392b53867f
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 TODO: Write your name
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# S3Assets
|
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/s3_assets`. 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 's3_assets'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install s3_assets
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/s3_assets. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the S3Assets project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/s3_assets/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "s3_assets"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
class ::S3Assets::Model
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps::Created
|
4
|
+
|
5
|
+
mount_uploader :asset, ::S3Assets::Uploader, mount_on: :asset_filename
|
6
|
+
|
7
|
+
field :content_type, type: String
|
8
|
+
field :absolute_url, type: String, default: nil
|
9
|
+
|
10
|
+
belongs_to :uploader, class_name: 'User'
|
11
|
+
field :ip_address, type: String
|
12
|
+
field :parent, type: ShallowDocument
|
13
|
+
field :temp, type: Boolean
|
14
|
+
|
15
|
+
after_save do |doc|
|
16
|
+
doc.delay(priority: ::S3Assets.dj_priority).fetch_and_store_from_url! if doc.processable?
|
17
|
+
end
|
18
|
+
|
19
|
+
scope :images, ->{where(content_type: /image/i)}
|
20
|
+
|
21
|
+
def image?
|
22
|
+
self.content_type.to_s.downcase.include? "image" unless self.content_type.nil?
|
23
|
+
end
|
24
|
+
|
25
|
+
def download
|
26
|
+
::S3Assets::Utility.download(self.asset.url)
|
27
|
+
end
|
28
|
+
|
29
|
+
def original_filename
|
30
|
+
return nil if filename.blank?
|
31
|
+
URI.unescape(filename)
|
32
|
+
end
|
33
|
+
|
34
|
+
def processable?
|
35
|
+
self.absolute_url.present? && self.asset.blank?
|
36
|
+
end
|
37
|
+
|
38
|
+
def processed?
|
39
|
+
!(processable?)
|
40
|
+
end
|
41
|
+
|
42
|
+
def filename
|
43
|
+
return nil if self.asset.url.blank?
|
44
|
+
file_name = self.asset.url.split("?").first
|
45
|
+
file_name = file_name.split("/").last
|
46
|
+
file_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def dj_priority
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.from_s3_params(bucket, key)
|
53
|
+
return nil if bucket.blank? || key.blank?
|
54
|
+
|
55
|
+
key = URI.encode(key, "!@#$%^&*()+=[]{} ")
|
56
|
+
url = "https://s3.amazonaws.com/#{bucket}/#{key}"
|
57
|
+
self.new(absolute_url: url)
|
58
|
+
end
|
59
|
+
|
60
|
+
def fetch_and_store_from_url!
|
61
|
+
return unless self.processable?
|
62
|
+
|
63
|
+
begin
|
64
|
+
self.remote_asset_url = self.absolute_url
|
65
|
+
self.save!
|
66
|
+
rescue CarrierWave::DownloadError => ex
|
67
|
+
if ex.message.include?("Invalid Location URI")
|
68
|
+
# "http://www.hiretale.com/files/resize_logo/13126logo original.png"
|
69
|
+
# is not working without doing this due to redirect URL not encoded properly
|
70
|
+
page = Mechanize.new.head(self.absolute_url)
|
71
|
+
self.remote_asset_url = page.uri.to_s
|
72
|
+
self.save!
|
73
|
+
else
|
74
|
+
raise ex
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module ::S3Assets::Relations
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module Helpers
|
5
|
+
def self.proper_asset_id(asset_id, parent)
|
6
|
+
return if asset_id.blank?
|
7
|
+
return asset_id if BSON::ObjectId.legal?(asset_id)
|
8
|
+
|
9
|
+
asset_id = "http:#{asset_id}" if asset_id.starts_with?("//")
|
10
|
+
|
11
|
+
if asset_id.starts_with?("http")
|
12
|
+
return ::S3Assets::Model.create!(absolute_url: asset_id, uploader: RequestStore.store[:current_user],
|
13
|
+
ip_address: RequestStore.store[:ip_address], parent: parent).id
|
14
|
+
else
|
15
|
+
return ::S3Assets::Utility.create!(asset_id, parent: parent).id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def asset_belongs_to(relation_name, options = {})
|
22
|
+
options[:class_name] = ::S3Assets::Model.to_s
|
23
|
+
related_doc_klass = options[:class_name].constantize
|
24
|
+
field_name = "#{relation_name}_id".to_sym
|
25
|
+
|
26
|
+
self.instance_eval do
|
27
|
+
self.belongs_to(relation_name, options)
|
28
|
+
|
29
|
+
self.send(:define_method, "#{field_name}=".to_sym) do |asset_id|
|
30
|
+
asset_id = ::S3Assets::Relations::Helpers.proper_asset_id(asset_id, self)
|
31
|
+
super(asset_id)
|
32
|
+
end
|
33
|
+
|
34
|
+
after_save do |doc|
|
35
|
+
if doc.send("#{field_name}_changed?")
|
36
|
+
old_related_doc_id = doc.send("#{field_name}_was")
|
37
|
+
if old_related_doc_id.present? && old_related_doc_id.to_s != doc.send(field_name).to_s
|
38
|
+
related_doc_klass.where(:_id => old_related_doc_id).destroy_all
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
after_destroy do |doc|
|
44
|
+
related_doc_id = doc.send(field_name)
|
45
|
+
related_doc_klass.where(:_id => related_doc_id).destroy_all if related_doc_id.present?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def asset_has_and_belongs_to_many(relation_name, options = {})
|
51
|
+
options[:class_name] = ::S3Assets::Model.to_s
|
52
|
+
related_doc_klass = options[:class_name].constantize
|
53
|
+
field_name = "#{relation_name.to_s.singularize}_ids".to_sym
|
54
|
+
|
55
|
+
self.instance_eval do
|
56
|
+
self.has_and_belongs_to_many(relation_name, options)
|
57
|
+
|
58
|
+
self.send(:define_method, "#{field_name}=".to_sym) do |asset_ids|
|
59
|
+
if asset_ids.present?
|
60
|
+
asset_ids = asset_ids.reject(&:blank?).map do |asset_id|
|
61
|
+
::S3Assets::Relations::Helpers.proper_asset_id(asset_id, self)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
super(asset_ids)
|
65
|
+
end
|
66
|
+
|
67
|
+
after_save do |doc|
|
68
|
+
if doc.send("#{field_name}_changed?")
|
69
|
+
old_related_doc_ids = [doc.send("#{field_name}_was")].flatten.compact.map(&:to_s)
|
70
|
+
new_related_doc_ids = [doc.send(field_name)].flatten.compact.map(&:to_s)
|
71
|
+
removed_ids = old_related_doc_ids - new_related_doc_ids
|
72
|
+
related_doc_klass.where(:_id.in => removed_ids).destroy_all if removed_ids.present?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
after_destroy do |doc|
|
77
|
+
related_doc_ids = doc.send(field_name)
|
78
|
+
related_doc_klass.where(:_id.in => related_doc_ids).destroy_all if related_doc_ids.present?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
class ::S3Assets::Uploader < CarrierWave::Uploader::Base
|
2
|
+
include ActiveModel::Conversion
|
3
|
+
extend ActiveModel::Naming
|
4
|
+
|
5
|
+
UPLOAD_EXPIRATION = 2.hours
|
6
|
+
MAX_FILE_SIZE = 20.megabytes
|
7
|
+
|
8
|
+
process :set_model_content_type
|
9
|
+
|
10
|
+
def original_filename
|
11
|
+
name = super
|
12
|
+
return URI.decode(name) if name.present?
|
13
|
+
end
|
14
|
+
|
15
|
+
def sanitize_regexp
|
16
|
+
/[^[:word:]\.\-\+]/
|
17
|
+
end
|
18
|
+
|
19
|
+
def fog_public
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
def fog_credentials
|
24
|
+
{
|
25
|
+
:provider => 'AWS',
|
26
|
+
:aws_access_key_id => ::S3Assets.aws_access_key_id,
|
27
|
+
:aws_secret_access_key => ::S3Assets.aws_secret_access_key,
|
28
|
+
:region => ::S3Assets.aws_region,
|
29
|
+
:host => ::S3Assets.fog_host,
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def storage
|
34
|
+
return :fog
|
35
|
+
end
|
36
|
+
|
37
|
+
def fog_directory
|
38
|
+
::S3Assets.fog_permanent_bucket
|
39
|
+
end
|
40
|
+
|
41
|
+
def asset_host
|
42
|
+
"https://#{::S3Assets.fog_permanent_bucket}.s3.amazonaws.com"
|
43
|
+
end
|
44
|
+
|
45
|
+
def fog_authenticated_url_expiration
|
46
|
+
10.hours
|
47
|
+
end
|
48
|
+
|
49
|
+
def ignore_integrity_errors
|
50
|
+
false
|
51
|
+
end
|
52
|
+
|
53
|
+
def ignore_processing_errors
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
57
|
+
def ignore_download_errors
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
61
|
+
def upload_url
|
62
|
+
"https://#{::S3Assets.fog_temp_bucket}.s3.amazonaws.com"
|
63
|
+
end
|
64
|
+
|
65
|
+
def fog_attributes
|
66
|
+
# cached for 1 year
|
67
|
+
{'Cache-Control' => "public, max-age=#{60*60*24*365}"}
|
68
|
+
end
|
69
|
+
|
70
|
+
def store_dir
|
71
|
+
return "prod/#{model.id.to_s}" if Rails.env.production?
|
72
|
+
return "dev/#{model.id.to_s}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def temp_store_dir
|
76
|
+
return "prod/#{model.id.to_s}" if Rails.env.production?
|
77
|
+
return "dev/#{model.id.to_s}"
|
78
|
+
end
|
79
|
+
|
80
|
+
# override the url to return absolute url if available and
|
81
|
+
# revert back to standard functionality if it is not available
|
82
|
+
def url
|
83
|
+
if model.processable?
|
84
|
+
model.absolute_url
|
85
|
+
else
|
86
|
+
super
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def acl
|
91
|
+
'public-read'
|
92
|
+
end
|
93
|
+
|
94
|
+
def policy_doc(options={})
|
95
|
+
options[:expiration] ||= UPLOAD_EXPIRATION
|
96
|
+
options[:max_file_size] ||= MAX_FILE_SIZE
|
97
|
+
success_action_status = options[:success_action_status]
|
98
|
+
store_dir = self.temp_store_dir
|
99
|
+
|
100
|
+
doc = {
|
101
|
+
'expiration' => Time.now.utc + options[:expiration],
|
102
|
+
'conditions' => [
|
103
|
+
["starts-with", "$key", store_dir],
|
104
|
+
{"bucket" => ::S3Assets.fog_temp_bucket},
|
105
|
+
{"acl" => acl},
|
106
|
+
["content-length-range", 1, options[:max_file_size]]
|
107
|
+
]
|
108
|
+
}
|
109
|
+
doc['conditions'] << {"success_action_status" => success_action_status.to_s} unless success_action_status.blank?
|
110
|
+
doc['conditions'] << ["starts-with", "$Content-type", ""]
|
111
|
+
doc
|
112
|
+
end
|
113
|
+
|
114
|
+
def policy(options={})
|
115
|
+
Base64.encode64(policy_doc(options).to_json).gsub("\n", "")
|
116
|
+
end
|
117
|
+
|
118
|
+
def signature policy=nil
|
119
|
+
policy = self.policy unless policy
|
120
|
+
Base64.encode64(
|
121
|
+
OpenSSL::HMAC.digest(
|
122
|
+
OpenSSL::Digest.new('sha1'),
|
123
|
+
AWS_SECRET_ACCESS_KEY, policy
|
124
|
+
)
|
125
|
+
).gsub("\n", "")
|
126
|
+
end
|
127
|
+
|
128
|
+
def set_model_content_type
|
129
|
+
return if file.blank?
|
130
|
+
|
131
|
+
type = File.magic_number_type(file.path.to_s) rescue nil
|
132
|
+
content_types = MIME::Types.type_for(type.to_s)
|
133
|
+
|
134
|
+
type = File.extname(file.path)
|
135
|
+
content_types |= MIME::Types.type_for(type.to_s)
|
136
|
+
|
137
|
+
model.content_type = content_types.first.to_s
|
138
|
+
end
|
139
|
+
|
140
|
+
protected
|
141
|
+
def image?(new_file)
|
142
|
+
model.content_type.nil? ? false : model.content_type.include?('image')
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module S3Assets::Utility
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def json(doc, type: nil, processing: nil)
|
5
|
+
file_url = url(doc, type: type, processing: processing)
|
6
|
+
return if file_url.blank?
|
7
|
+
|
8
|
+
if doc.present? && doc.processed?
|
9
|
+
filepath = doc.asset.path
|
10
|
+
content_type = doc.content_type
|
11
|
+
end
|
12
|
+
{
|
13
|
+
id: doc.try(:_id),
|
14
|
+
url: file_url,
|
15
|
+
filepath: filepath,
|
16
|
+
content_type: content_type,
|
17
|
+
name: doc.try(:original_filename)
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def url(doc, type: nil, processing: nil)
|
22
|
+
return if doc.blank?
|
23
|
+
return "https://#{::S3Assets.cloudfront_host}/#{doc.asset.path}" unless ::S3Assets.processing_enabled
|
24
|
+
|
25
|
+
return doc.asset.url if !(doc.processed?)
|
26
|
+
|
27
|
+
raw_url = "https://#{::S3Assets.cloudfront_host}/raw/#{doc.asset.path}"
|
28
|
+
return raw_url if !(doc.image?)
|
29
|
+
return raw_url if doc.content_type.include?("gif") && !(processing)
|
30
|
+
|
31
|
+
processing ||= {}
|
32
|
+
processing[:size] ||= "1500x1500"
|
33
|
+
processing[:type] ||= "cover"
|
34
|
+
processing_str = "/size:#{processing[:size]}"
|
35
|
+
processing_str += "/extend:#{processing[:extend]}" if processing[:extend].present?
|
36
|
+
processing_str += "/blur:#{processing[:blur].to_i}" if processing[:blur].present?
|
37
|
+
processing_str += "/type:#{processing[:type]}"
|
38
|
+
|
39
|
+
|
40
|
+
return "https://#{::S3Assets.cloudfront_host}/images#{processing_str}/#{doc.asset.path}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def download(url)
|
44
|
+
temp_doc = ::S3Assets::Model.new
|
45
|
+
temp_doc.asset.download!(url)
|
46
|
+
return temp_doc.asset.file.file
|
47
|
+
end
|
48
|
+
|
49
|
+
def create!(filepath, parent: nil)
|
50
|
+
doc = nil
|
51
|
+
File.open(filepath) do |file|
|
52
|
+
doc = ::S3Assets::Model.new(parent: parent)
|
53
|
+
doc.asset = file
|
54
|
+
doc.save!
|
55
|
+
end
|
56
|
+
return doc
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/s3_assets.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "s3_assets/version"
|
2
|
+
|
3
|
+
module S3Assets
|
4
|
+
class << self
|
5
|
+
attr_accessor :dj_priority, :upload_expiration, :max_file_size, :fog_temp_bucket, :aws_access_key_id,
|
6
|
+
:aws_secret_access_key, :aws_region, :fog_host, :fog_permanent_bucket
|
7
|
+
attr_accessor :cloudfront_host
|
8
|
+
attr_accessor :processing_enabled
|
9
|
+
end
|
10
|
+
|
11
|
+
class Error < StandardError; end
|
12
|
+
# Your code goes here...
|
13
|
+
end
|
data/s3_assets.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "s3_assets/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "s3_assets"
|
8
|
+
spec.version = S3Assets::VERSION
|
9
|
+
spec.authors = ["Amit Chaudhary"]
|
10
|
+
spec.email = ["chaudharyamitiit2007@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Common package build up for storing s3 assets reference.}
|
13
|
+
spec.description = %q{Tou can store s3 assets very easity using this}
|
14
|
+
spec.homepage = "https://github.com/chaudhary/s3_assets"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#
|
22
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
# else
|
26
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
# "public gem pushes."
|
28
|
+
# end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3_assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amit Chaudhary
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-27 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.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
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
|
+
description: Tou can store s3 assets very easity using this
|
42
|
+
email:
|
43
|
+
- chaudharyamitiit2007@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CODE_OF_CONDUCT.md
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/s3_assets.rb
|
58
|
+
- lib/s3_assets/model.rb
|
59
|
+
- lib/s3_assets/relations.rb
|
60
|
+
- lib/s3_assets/uploader.rb
|
61
|
+
- lib/s3_assets/utility.rb
|
62
|
+
- lib/s3_assets/version.rb
|
63
|
+
- s3_assets.gemspec
|
64
|
+
homepage: https://github.com/chaudhary/s3_assets
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.7.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Common package build up for storing s3 assets reference.
|
88
|
+
test_files: []
|