mini_paperclip 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +40 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +3 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +12 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +175 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/gemfiles/rails_52.gemfile +10 -0
  13. data/gemfiles/rails_52.gemfile.lock +98 -0
  14. data/gemfiles/rails_60.gemfile +10 -0
  15. data/gemfiles/rails_60.gemfile.lock +98 -0
  16. data/lib/mini_paperclip.rb +41 -0
  17. data/lib/mini_paperclip/attachment.rb +190 -0
  18. data/lib/mini_paperclip/class_methods.rb +62 -0
  19. data/lib/mini_paperclip/config.rb +32 -0
  20. data/lib/mini_paperclip/interpolator.rb +41 -0
  21. data/lib/mini_paperclip/shoulda/matchers.rb +7 -0
  22. data/lib/mini_paperclip/shoulda/matchers/have_attached_file_matcher.rb +46 -0
  23. data/lib/mini_paperclip/shoulda/matchers/validate_attachment_content_type_matcher.rb +84 -0
  24. data/lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb +111 -0
  25. data/lib/mini_paperclip/shoulda/matchers/validate_attachment_presence_matcher.rb +52 -0
  26. data/lib/mini_paperclip/shoulda/matchers/validate_attachment_size_matcher.rb +62 -0
  27. data/lib/mini_paperclip/storage.rb +5 -0
  28. data/lib/mini_paperclip/storage/base.rb +39 -0
  29. data/lib/mini_paperclip/storage/filesystem.rb +34 -0
  30. data/lib/mini_paperclip/storage/s3.rb +49 -0
  31. data/lib/mini_paperclip/validators.rb +7 -0
  32. data/lib/mini_paperclip/validators/content_type_validator.rb +17 -0
  33. data/lib/mini_paperclip/validators/file_size_validator.rb +21 -0
  34. data/lib/mini_paperclip/validators/geometry_validator.rb +46 -0
  35. data/lib/mini_paperclip/validators/media_type_spoof_validator.rb +25 -0
  36. data/lib/mini_paperclip/validators/presence_validator.rb +13 -0
  37. data/lib/mini_paperclip/version.rb +5 -0
  38. data/mini_paperclip.gemspec +32 -0
  39. metadata +152 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 934c31a21e7ff961a58d0d22023b0d17095b83087a22845b30e85102bba1667b
4
+ data.tar.gz: 003a3f115224cf645d81fe98dfd1664ea3386aa48f7436791a1bf6b14c0b864a
5
+ SHA512:
6
+ metadata.gz: 60d2cb3ddfcff65b7edcf3519d7752f5a5aafb03bdcf15ba487b6c423af234feb13224b609d5bba1cd3bba3939f70f9aaa8b7a9209ecdef8e414e829a946ea4a
7
+ data.tar.gz: d16ef4a7638188af8963b87ab41c0045cf1a73547558b0d9864ba7efa713f30e86164c81ae8cb19ecc998c1b250dafa99d2c5556cf27e80eddf8cd53d8a867a4
@@ -0,0 +1,40 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches:
13
+ - main
14
+ pull_request:
15
+ branches:
16
+ - main
17
+
18
+ jobs:
19
+ test:
20
+ strategy:
21
+ fail-fast: false
22
+ matrix:
23
+ ruby:
24
+ - 2.5
25
+ - 2.6
26
+ - 2.7
27
+ gemfile:
28
+ - gemfiles/rails_52.gemfile
29
+ - gemfiles/rails_60.gemfile
30
+ runs-on: ubuntu-20.04
31
+ steps:
32
+ - uses: actions/checkout@v2
33
+ - name: Set up Ruby
34
+ uses: ruby/setup-ruby@v1
35
+ with:
36
+ ruby-version: ${{ matrix.ruby }}
37
+ - name: Install dependencies
38
+ run: bundle install --gemfile=${{ matrix.gemfile }}
39
+ - name: Run tests
40
+ run: bundle exec --gemfile=${{ matrix.gemfile }} rake
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/temp/
9
+ /tmp/
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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 co000ri@gmail.com. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mini_paperclip.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rack-test"
9
+ gem "webmock"
10
+ gem "tapp"
11
+ gem "activerecord"
12
+ gem "sqlite3"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Repro Inc.
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,175 @@
1
+ # MiniPaperclip
2
+
3
+ mini_paperclip is a subset of [paperclip](https://github.com/thoughtbot/paperclip)
4
+
5
+ # Guidelines
6
+
7
+ - Major API follow paperclip.
8
+ - Minor API and configuration changed from paperclip.
9
+ - Keep DB columns, S3 Objects and application code.
10
+ - Keep maintenable.
11
+ - Keep MINI.
12
+
13
+ # Usage
14
+
15
+ ```
16
+ class Book < ActiveRecord::Base
17
+ extend MiniPaperclip::ClassMethods
18
+ has_attached_file :image
19
+ end
20
+
21
+ book = Book.find(id)
22
+ book.update!(book_params) # { image: ... }
23
+ book.image # #<MiniPaperclip::Attachment >
24
+ book.image.url # "http://..."
25
+ ```
26
+
27
+ # Needed
28
+
29
+ ## Columns
30
+
31
+ - \<attachment\>_file_name
32
+ - \<attachment\>_file_size
33
+ - \<attachment\>_content_type
34
+ - \<attachment\>_updated_at
35
+
36
+ ## Command
37
+
38
+ [imagemagick](https://imagemagick.org/index.php)
39
+
40
+ # Writable data
41
+
42
+ ```
43
+ book.image = ? # assign value to columns
44
+ book.save # write file to storage
45
+ ```
46
+
47
+ `?` is ...
48
+
49
+ - MiniPaperclip::Attachment # copy file
50
+ - ActionDispatch::Http::UploadedFile # in rails simple case
51
+ - url string e.g. "https://s3/bucket/key.png" # download contents from url
52
+ - data-uri string # read by base64 encoded string. but \<attachment\>_file_name could not set
53
+
54
+ # Configuration
55
+
56
+ You can set configuration e.g initializers or environments
57
+
58
+ ```
59
+ MiniPaperclip.config.tap do |config|
60
+ config.storage # default storage. `:filesystem` or `:s3`
61
+ config.filesystem_path # saving file path
62
+ config.hash_data # interpolated `:hash` base data
63
+ config.hash_secret # interpolated `:hash` secret
64
+ config.styles # default styles
65
+ config.url_scheme # 'http' or 'https'
66
+ config.url_host # host name for `url`
67
+ config.url_path # path for `url` and S3 object
68
+ config.url_missing_path # path when not attached
69
+ config.s3_host_alias # CDN host name
70
+ config.s3_bucket_name # should set when storage = :s3
71
+ config.s3_acl # s3 object acl
72
+ config.s3_cache_control # Set this value to Cache-Control header when put-object
73
+ config.interpolates # minimum templates using by `String#gsub!`
74
+ config.read_timeout # timeout when attachment set url
75
+ config.logger # You can set logger object.
76
+ end
77
+ ```
78
+
79
+ And any configuration can overwrite by attachment.
80
+
81
+ ```
82
+ class Book < ActiveRecord::Base
83
+ has_attached_file :image,
84
+ styles: { medium: "500x500#" },
85
+ s3_host_alias: ENV['CLOUD_FRONT_DOMAIN'],
86
+ hash_data: ':attachment/:id/:updated_at'
87
+ ...
88
+ ```
89
+
90
+ # Validation
91
+
92
+ ```
93
+ class Book < ActiveRecord::Base
94
+ extend MiniPaperclip::ClassMethods
95
+ has_attached_file :image
96
+ validates_attachment :image,
97
+ content_type: { content_type: ["image/jpeg", "image/png"] },
98
+ size: { less_than: 1.megabytes },
99
+ if: :need_validation?
100
+ end
101
+ ```
102
+
103
+ # Interpolate
104
+
105
+ Interpolate is a simple template system like this.
106
+
107
+ template: `:class/:attribute/:id/:hash.:extension`
108
+ result: `books/images/1234/abcdef1234567.png`
109
+
110
+ You can check default interpolates.
111
+
112
+ ```
113
+ p MiniPaperclip.config.interpolaters
114
+ ```
115
+
116
+ You can add any interpolate key and process.
117
+
118
+ ```
119
+ MiniPaperclip.config.interpolates[/:custom_style/] = -> (attachment, style) {
120
+ "-#{style}"
121
+ }
122
+ ```
123
+
124
+ # Security
125
+
126
+ Paperclip had a security issue.
127
+
128
+ http://homakov.blogspot.com/2014/02/paperclip-vulnerability-leading-to-xss.html
129
+
130
+ Security and Performance has a serious performance tradeoff.
131
+
132
+ mini_paperclip take security very seriously.
133
+
134
+ ## Force validate spoof media type
135
+
136
+ mini_paperclip force validate content-type both metadata and content same as paperclip.
137
+
138
+ ## Read content-type from content only
139
+
140
+ mini_paperclip don't read metadata from HTTP responce or data-uri.
141
+
142
+ # Installation
143
+
144
+ Add this line to your application's Gemfile:
145
+
146
+ ```ruby
147
+ gem 'mini_paperclip'
148
+ ```
149
+
150
+ And then execute:
151
+
152
+ $ bundle install
153
+
154
+ Or install it yourself as:
155
+
156
+ $ gem install mini_paperclip
157
+
158
+ # Development
159
+
160
+ 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.
161
+
162
+ 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).
163
+
164
+ # Contributing
165
+
166
+ Bug reports and pull requests are welcome on GitHub at https://github.com/reproio/mini_paperclip. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/reproio/mini_paperclip/blob/master/CODE_OF_CONDUCT.md).
167
+
168
+
169
+ # License
170
+
171
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
172
+
173
+ # Code of Conduct
174
+
175
+ Everyone interacting in the MiniPaperclip project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/reproio/mini_paperclip/blob/master/CODE_OF_CONDUCT.md).
@@ -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 "mini_paperclip"
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__)
@@ -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,10 @@
1
+ source "https://rubygems.org"
2
+ gemspec path: '../'
3
+
4
+ gem "rake", "~> 12.0"
5
+ gem "rspec", "~> 3.0"
6
+ gem "rack-test"
7
+ gem "webmock"
8
+ gem "tapp"
9
+ gem 'activerecord', '~> 5.2.0'
10
+ gem "sqlite3"
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ mini_paperclip (0.1.0)
5
+ activemodel
6
+ activesupport
7
+ aws-sdk-s3
8
+ mimemagic
9
+ mini_magick
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activemodel (5.2.4.4)
15
+ activesupport (= 5.2.4.4)
16
+ activerecord (5.2.4.4)
17
+ activemodel (= 5.2.4.4)
18
+ activesupport (= 5.2.4.4)
19
+ arel (>= 9.0)
20
+ activesupport (5.2.4.4)
21
+ concurrent-ruby (~> 1.0, >= 1.0.2)
22
+ i18n (>= 0.7, < 2)
23
+ minitest (~> 5.1)
24
+ tzinfo (~> 1.1)
25
+ addressable (2.7.0)
26
+ public_suffix (>= 2.0.2, < 5.0)
27
+ arel (9.0.0)
28
+ aws-eventstream (1.1.0)
29
+ aws-partitions (1.399.0)
30
+ aws-sdk-core (3.109.3)
31
+ aws-eventstream (~> 1, >= 1.0.2)
32
+ aws-partitions (~> 1, >= 1.239.0)
33
+ aws-sigv4 (~> 1.1)
34
+ jmespath (~> 1.0)
35
+ aws-sdk-kms (1.39.0)
36
+ aws-sdk-core (~> 3, >= 3.109.0)
37
+ aws-sigv4 (~> 1.1)
38
+ aws-sdk-s3 (1.85.0)
39
+ aws-sdk-core (~> 3, >= 3.109.0)
40
+ aws-sdk-kms (~> 1)
41
+ aws-sigv4 (~> 1.1)
42
+ aws-sigv4 (1.2.2)
43
+ aws-eventstream (~> 1, >= 1.0.2)
44
+ concurrent-ruby (1.1.7)
45
+ crack (0.4.4)
46
+ diff-lcs (1.4.4)
47
+ hashdiff (1.0.1)
48
+ i18n (1.8.5)
49
+ concurrent-ruby (~> 1.0)
50
+ jmespath (1.4.0)
51
+ mimemagic (0.3.5)
52
+ mini_magick (4.11.0)
53
+ minitest (5.14.2)
54
+ public_suffix (4.0.6)
55
+ rack (2.2.3)
56
+ rack-test (1.1.0)
57
+ rack (>= 1.0, < 3)
58
+ rake (12.3.3)
59
+ rspec (3.10.0)
60
+ rspec-core (~> 3.10.0)
61
+ rspec-expectations (~> 3.10.0)
62
+ rspec-mocks (~> 3.10.0)
63
+ rspec-core (3.10.0)
64
+ rspec-support (~> 3.10.0)
65
+ rspec-expectations (3.10.0)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.10.0)
68
+ rspec-mocks (3.10.0)
69
+ diff-lcs (>= 1.2.0, < 2.0)
70
+ rspec-support (~> 3.10.0)
71
+ rspec-support (3.10.0)
72
+ sqlite3 (1.4.2)
73
+ tapp (1.5.1)
74
+ thor
75
+ thor (1.0.1)
76
+ thread_safe (0.3.6)
77
+ tzinfo (1.2.8)
78
+ thread_safe (~> 0.1)
79
+ webmock (3.10.0)
80
+ addressable (>= 2.3.6)
81
+ crack (>= 0.3.2)
82
+ hashdiff (>= 0.4.0, < 2.0.0)
83
+
84
+ PLATFORMS
85
+ ruby
86
+
87
+ DEPENDENCIES
88
+ activerecord (~> 5.2.0)
89
+ mini_paperclip!
90
+ rack-test
91
+ rake (~> 12.0)
92
+ rspec (~> 3.0)
93
+ sqlite3
94
+ tapp
95
+ webmock
96
+
97
+ BUNDLED WITH
98
+ 2.1.4