s3_thumbnail 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed66d63e2b2fdeae7cf841a0b5c9a01f54ed7a32
4
+ data.tar.gz: 9574e2dcf23540a71ee4703e97f11b6044e8eefa
5
+ SHA512:
6
+ metadata.gz: e0936af6fc01b6f6710fec8dc71cba410c6b5d6b4f0658ac45c361135600782f55810a8386f93270bffc3b376280e48c2d5e097110e674b83059c23079e2eb7b
7
+ data.tar.gz: 7b5e58dc250543a15409c3e4055786fefb1c5813d9a39fa02bfcc9d9875d5c29436bfdbb50123e3e4df9aedb5e4cfc719710c08b61b75f12ab6192767dc4544b
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3_thumbnails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jeremy Saenz
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,39 @@
1
+ # S3Thumbnail
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_thumbnail`. 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_thumbnail'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install s3_thumbnail
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. Then, 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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/s3_thumbnail/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "s3_thumbnails"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ require "mini_magick"
2
+
3
+ module S3Thumbnail
4
+ class Generation
5
+ def initialize(input_file)
6
+ @input_file = input_file
7
+ end
8
+
9
+ def write(output_file, width, height, quality)
10
+ resized = resize_with_crop(image, width, height, quality)
11
+ resized.write(output_file.path)
12
+ end
13
+
14
+ private
15
+
16
+ def image
17
+ @input_file.rewind
18
+ MiniMagick::Image.read(@input_file).tap do |i|
19
+ i.format('jpg')
20
+ end
21
+ end
22
+
23
+ def resize_with_crop(img, width, height, quality)
24
+ cols, rows = img[:dimensions]
25
+ img.combine_options do |cmd|
26
+ if width != cols || height != rows
27
+ scale_x = width/cols.to_f
28
+ scale_y = height/rows.to_f
29
+ if scale_x >= scale_y
30
+ cols = (scale_x * (cols + 0.5)).round
31
+ rows = (scale_x * (rows + 0.5)).round
32
+ cmd.resize "#{cols}"
33
+ else
34
+ cols = (scale_y * (cols + 0.5)).round
35
+ rows = (scale_y * (rows + 0.5)).round
36
+ cmd.resize "x#{rows}"
37
+ end
38
+ end
39
+ cmd.quality quality
40
+ cmd.gravity 'Center'
41
+ cmd.background "rgba(255,255,255,0.0)"
42
+ cmd.extent "#{width}x#{height}" if cols != width || rows != height
43
+ end
44
+ img
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,65 @@
1
+ require "s3_style"
2
+
3
+ module S3Thumbnail
4
+ module Thumbnailable
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def has_thumbnails(attribute)
9
+ after_commit do
10
+ if previous_changes.include?("#{attribute}_file")
11
+ regenerate_thumbnails_for(attribute)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def regenerate_thumbnails_for(attribute)
18
+ styles_method = "#{attribute}_styles".to_sym
19
+ s3_file = public_send(attribute.to_sym)
20
+ styles = public_send(styles_method)
21
+
22
+ generate_thumbnails(s3_file, styles)
23
+ end
24
+
25
+ private
26
+
27
+ def generate_thumbnails(s3_file, styles)
28
+ return unless s3_file.exists?
29
+
30
+ s3 = AWS::S3.new
31
+ bucket = s3.buckets[S3Thumbnail.config.bucket]
32
+ original = bucket.objects[s3_file.key]
33
+
34
+ begin
35
+ ext = '.jpg'
36
+ # Grab original from S3 and store in a tmpfile
37
+ infile = Tempfile.new('image', "tmp/", encoding: 'binary')
38
+ original.read { |chunk| infile.write(chunk) }
39
+ thumbnail_generation = Generation.new(infile)
40
+
41
+ styles.each do |style, config|
42
+ begin
43
+ # Write the thumbnail to a tempfile
44
+ outfile = Tempfile.new(['s3_file', ext])
45
+
46
+ thumbnail_generation.write(outfile,
47
+ config.fetch(:width),
48
+ config.fetch(:height),
49
+ config.fetch(:quality, 80))
50
+
51
+ # Ensure the keyname is like _style.jpg
52
+ key = S3Style::Url.new(s3_file.key, ext).style(style)
53
+ # Upload the styled file back to S3
54
+ obj = bucket.objects[key]
55
+ obj.write(outfile, acl: :public_read, content_type: 'image/jpeg')
56
+ ensure
57
+ outfile.close!
58
+ end
59
+ end
60
+ ensure
61
+ infile.close!
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module S3Thumbnail
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "active_support/all"
2
+
3
+ require "s3_thumbnail/version"
4
+ require "s3_thumbnail/generation"
5
+ require "s3_thumbnail/thumbnailable"
6
+
7
+ module S3Thumbnail
8
+ def self.configure
9
+ yield config
10
+ end
11
+
12
+ def self.config
13
+ @@config ||= Config.new
14
+ end
15
+
16
+ class Config
17
+ attr_accessor :bucket
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3_thumbnail/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3_thumbnail"
8
+ spec.version = S3Thumbnail::VERSION
9
+ spec.authors = ["Jeremy Saenz"]
10
+ spec.email = ["jeremy.saenz@gmail.com"]
11
+
12
+ spec.summary = %q{Easy image thumbnails for the s3direct gem}
13
+ spec.description = %q{s3_thumbnail will generate thumbnails for s3direct files}
14
+ spec.homepage = "http://github.com/codegangsta/s3_thumbnail"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'mini_magick', '~> 3.7.0'
23
+ spec.add_dependency 'activesupport', '>= 3.2.0'
24
+ spec.add_dependency 's3_style', '>= 0.1.0'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.8"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "s3direct"
30
+ spec.add_development_dependency "aws-sdk", "~> 1.36.0"
31
+ spec.add_development_dependency "fakes3", "~> 0.2.1"
32
+ spec.add_development_dependency "activemodel"
33
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_thumbnail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Saenz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mini_magick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: s3_style
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: s3direct
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: aws-sdk
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.36.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.36.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: fakes3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.2.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.2.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: activemodel
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: s3_thumbnail will generate thumbnails for s3direct files
154
+ email:
155
+ - jeremy.saenz@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".travis.yml"
163
+ - Gemfile
164
+ - LICENSE.txt
165
+ - README.md
166
+ - Rakefile
167
+ - bin/console
168
+ - bin/setup
169
+ - lib/s3_thumbnail.rb
170
+ - lib/s3_thumbnail/generation.rb
171
+ - lib/s3_thumbnail/thumbnailable.rb
172
+ - lib/s3_thumbnail/version.rb
173
+ - s3_thumbnails.gemspec
174
+ homepage: http://github.com/codegangsta/s3_thumbnail
175
+ licenses:
176
+ - MIT
177
+ metadata: {}
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 2.4.5
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: Easy image thumbnails for the s3direct gem
198
+ test_files: []