carrierwave-blitline 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: bcdab1946c051e221bdf9f7327168fa69d69a0ef
4
+ data.tar.gz: d8a451c3dba65f7e20090e7aa1479404d521781f
5
+ SHA512:
6
+ metadata.gz: 0f1ea11ece56e432a29ba8e2dac504453a23c8f24c2081d0e4be48d158166e984907dc3dfee535ca57c4a12ca1475a73179bc76476e341e0c3a9270778f7a979
7
+ data.tar.gz: e278d95e4367a0df37580083ec3a76d8534a55aa504ce3ed2a4af5d720e18c5ea7be28cb3af71bfab6c93fee566db54f47f7d46a5f0a9a517bf88fa63b315f49
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-blitline.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Bodacious
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,125 @@
1
+ # Carrierwave::Blitline
2
+
3
+ This gem is still under construction but it basically works in its current form.
4
+
5
+ ## Installation
6
+
7
+ Install it with these other Carrierwave gems:
8
+
9
+ gem "carrierwave"
10
+
11
+ gem "carrierwave-aws"
12
+
13
+ gem "carrierwave-blitline"
14
+
15
+ Then execute
16
+
17
+ $ bundle install
18
+
19
+ Add this to your Carrierwave Uploader files:
20
+
21
+ class ImageUploader < CarrierWave::Uploader::Base
22
+
23
+ # NOTE: We're using MiniMagick here...
24
+ include CarrierWave::MiniMagick
25
+
26
+ require "carrierwave/blitline"
27
+ include CarrierWave::Blitline
28
+
29
+
30
+ # This macro lets your uploader know you're using Carrierwave
31
+ process_via_blitline
32
+
33
+
34
+ # other stuff ...
35
+
36
+ end
37
+
38
+ ## CONFIGURE
39
+
40
+ ### Your Carrierwave setup should be something like this
41
+
42
+ CarrierWave.configure do |config|
43
+
44
+ if Rails.env.test?
45
+ config.storage = :file
46
+ config.enable_processing = false
47
+ config.asset_host = 'http://test.host'
48
+
49
+ else
50
+ config.aws_credentials = {
51
+ :access_key_id => ENV["AWS_ACCESS_KEY"],
52
+ :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
53
+ :region => ENV["S3_BUCKET_REGION"]
54
+ }
55
+ config.storage :aws
56
+ config.aws_bucket = ENV["S3_BUCKET_NAME"]
57
+ config.aws_acl = 'public-read'
58
+ config.aws_attributes = {
59
+ expires: 1.week.from_now.httpdate, cache_control: 'max-age=315576000' }
60
+
61
+ config.asset_host = "https://%s" % ENV["ASSET_HOST"]
62
+ config.enable_processing = true
63
+ config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7
64
+ end
65
+
66
+ end
67
+
68
+ ### ENV Variables
69
+ The following env variables must be set for this to work properly.
70
+
71
+ ENV["BLITLINE_APPLICATION_ID"]
72
+ ENV["S3_BUCKET_NAME"]
73
+ ENV["S3_BUCKET_REGION"]
74
+
75
+ ## Usage
76
+
77
+ Define your carrierwave versions as you normally would.
78
+
79
+ Basic functions (`resize_to_fit`, `resize_to_fill`, etc.) are dealt with automagically.
80
+
81
+ For other functions, you need to write two methods in your uploader: 1) The local processing method, 2) the params for Blitline. For example
82
+
83
+ # Images will be cropped, and then resized to fill
84
+ version :cropped do
85
+ process :crop
86
+ process :resize_to_fill => [200, 200]
87
+ end
88
+
89
+ # Use this when processing locally
90
+ def crop
91
+ manipulate! do |img|
92
+ img = img.crop "#{model.photo_crop_x}x#{model.photo_crop_y}+#{model.photo_crop_width}+#{model.photo_crop_height}"
93
+ img
94
+ end
95
+ end
96
+
97
+ # Use this when processing on Blitline
98
+ def params_for_crop(*)
99
+ return {
100
+ x: model.photo_crop_x,
101
+ y: model.photo_crop_y,
102
+ width: model.photo_crop_width,
103
+ height: model.photo_crop_height
104
+ }
105
+ end
106
+
107
+
108
+ ## Development
109
+
110
+ 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.
111
+
112
+ 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).
113
+
114
+ ## Contributing
115
+
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/katanacode/carrierwave-blitline.
117
+
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
122
+
123
+ ## Created by
124
+
125
+ [Katana — web developers based in Edinburgh, Scotland](https://katanacode.com/)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "carrierwave/blitline"
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,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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/blitline/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carrierwave-blitline"
8
+ spec.version = Carrierwave::Blitline::VERSION
9
+ spec.authors = ["Bodacious"]
10
+ spec.email = ["team@katanacode.com"]
11
+
12
+ spec.summary = %q{Integrates Blitline image processing with Carrierwave}
13
+ spec.description = %q{Integrates the carrierwave gem with Blitline image API. (Still under development)}
14
+ spec.homepage = "https://github.com/KatanaCode/carrierwave-blitline"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "blitline", "~> 2.8"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,143 @@
1
+ # From the Blitline gem
2
+ module CarrierWave
3
+ module Blitline
4
+
5
+ require "blitline"
6
+
7
+ require "carrierwave/blitline/version"
8
+ require "carrierwave/blitline/class_methods"
9
+ require "carrierwave/blitline/image_version"
10
+ require "carrierwave/blitline/function"
11
+ require "carrierwave/blitline/image_version_function_presenter"
12
+
13
+ extend ActiveSupport::Concern
14
+
15
+ # Does the version name come at the start (carrierwave default) or at the
16
+ # end of the filename
17
+ RIP_VERSION_NAMES_AT_START = true
18
+
19
+ # Blitline API version
20
+ BLITLINE_VERSION = 1.21
21
+
22
+
23
+ # Extends the including class with ClassMethods, add an after_store callback
24
+ # and includes ImageMagick if required.
25
+ included do
26
+ after :store, :rip_process_images
27
+ end
28
+
29
+
30
+ # =============
31
+ # = Delegates =
32
+ # =============
33
+
34
+ delegate :blitline_image_versions, to: :class
35
+
36
+ delegate :process_via_blitline?, to: :class
37
+
38
+ # Send a request to Blitline to optimize the original file and create any
39
+ # required versions.
40
+ #
41
+ # This is called by an after_store macro and because Carrier creates virtual
42
+ # instancies for each version would be called 4 times for an image with three
43
+ # versions.
44
+ #
45
+ # Because we only want to do this on completion we check all the versions
46
+ # have been called by testing it is OK to begin processing
47
+ #
48
+ # A hash is created (job_hash) with Blitline's required commands and sent using the
49
+ # Blitline gem.
50
+ #
51
+ # file - not used within the method, but required for the callback to function
52
+ def rip_process_images(file)
53
+ return unless rip_can_begin_processing?
54
+ Rails.logger.tagged("Blitline") { |l| l.debug(job_hash.to_json) }
55
+ blitline_service.add_job_via_hash(job_hash)
56
+ begin
57
+ blitline_service.post_jobs
58
+ rescue => e
59
+ Rails.logger.tagged("Blitline") do |logger|
60
+ logger.error "ERROR: Blitline processing error for #{model.class.name}"
61
+ end
62
+ end
63
+ end
64
+
65
+ # Returns a Hash of params posted off to Blitline API
66
+ def job_hash
67
+ {
68
+ "application_id": ENV["BLITLINE_APPLICATION_ID"],
69
+ "src": url,
70
+ "v": BLITLINE_VERSION,
71
+ "functions": functions
72
+ }.with_indifferent_access
73
+ end
74
+
75
+ # Returns a Hash for each function included in the Blitline API post
76
+ def functions
77
+ blitline_image_versions.map { |version|
78
+ ImageVersionFunctionPresenter.new(version, self).to_hash
79
+ }
80
+ end
81
+
82
+ # sends a request to Blitline to re-process themain image and all versions
83
+ def optimize!
84
+ rip_process_images(true) if process_via_blitline?
85
+ end
86
+
87
+ # Can we post the images to Blitline for processing?
88
+ # CarrierWave creates virtual Uploaders for each version of an image. These
89
+ # versions are processed before the original, so the only way to tell if the
90
+ # versions are all complete is to check the classname for the current call
91
+ # and if there is no '::' it is the original class.
92
+ #
93
+ # Returns a boolean
94
+ def rip_can_begin_processing?
95
+ process_via_blitline? and not self.class.name.include? "::"
96
+ end
97
+
98
+ def filename
99
+ if file
100
+ "#{model.class.to_s.underscore}.#{file.extension}"
101
+ end
102
+ end
103
+
104
+ def unique_identifier
105
+ @unique_identifier ||= "#{Rails.application.class.name}_#{Rails.env}_#{SecureRandom.base64(10)}"
106
+ end
107
+
108
+ def file_name_for_version(version)
109
+ file_name, file_type = filename.split('.')
110
+ name_components = [version.name, file_name].compact
111
+ name_components.reverse! unless RIP_VERSION_NAMES_AT_START
112
+ file_namewith_version = name_components.join("_") + ".#{file_type}"
113
+ File.join(store_dir, file_namewith_version).to_s
114
+ end
115
+
116
+ def params_for_function(function_name, *args)
117
+ send("params_for_#{function_name}", *args)
118
+ end
119
+
120
+ def params_for_no_op(*args)
121
+ return {}
122
+ end
123
+
124
+ def params_for_resize_to_fill(*args)
125
+ args.flatten!
126
+ return { width: args.first, height: args.last }
127
+ end
128
+
129
+ def params_for_resize_to_fit(*args)
130
+ args.flatten!
131
+ return { width: args.first, height: args.last }
132
+ end
133
+
134
+
135
+ private
136
+
137
+
138
+ def blitline_service
139
+ @blitline_service ||= ::Blitline.new
140
+ end
141
+
142
+ end
143
+ end
@@ -0,0 +1,33 @@
1
+ # Class methods to be included in Blitline module
2
+ module CarrierWave
3
+ module Blitline
4
+
5
+ module ClassMethods
6
+
7
+ def version(name, &block)
8
+ blitline_image_versions << ImageVersion.new(name, &block)
9
+ # If process_via_blitline? is true, we still want to register the version with
10
+ # the Uploader, but we don't want to define the conversions.
11
+ if process_via_blitline?
12
+ super(name) {}
13
+ else
14
+ super(name, &block)
15
+ end
16
+ end
17
+
18
+ def blitline_image_versions
19
+ @blitline_versions ||= [ImageVersion.new(nil)]
20
+ end
21
+
22
+ def process_via_blitline(value = true)
23
+ @@process_via_blitline = value
24
+ end
25
+
26
+ def process_via_blitline?
27
+ defined?(@@process_via_blitline) && @@process_via_blitline == true
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,10 @@
1
+ module CarrierWave
2
+ module Blitline
3
+
4
+ # A Struct class for storing name and params for each function parameter.
5
+ # See also: ImageVersionFunctionPresenter
6
+ class Function < Struct.new(:name, :params)
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,71 @@
1
+ module CarrierWave
2
+ module Blitline
3
+
4
+ # An instance of an ImageVersion for Blitline API.
5
+ #
6
+ # When the process() version is called in an Uploader class, we store the version
7
+ # name, and the block to create that version as an ImageVersion
8
+ #
9
+ # So, a 'small' image version that's resized to fit 200x200 looks like:
10
+ #
11
+ # ImageVersion.new("small") do
12
+ # process :resize_to_fit => [200, 200]
13
+ # end
14
+ #
15
+ # NOTE: We need a 'default' image version too. This is created automatically, and
16
+ # the name for the version is 'nil'.
17
+ class ImageVersion
18
+
19
+ attr_accessor :primary_function
20
+
21
+ attr_accessor :secondary_functions
22
+
23
+ attr_reader :name
24
+
25
+ def initialize(name = nil, &block)
26
+ @name = name
27
+ @primary_function = nil
28
+ @secondary_functions = []
29
+ instance_exec(&block) if block_given?
30
+ end
31
+
32
+ # Hijacks the process() method that's called within a CarrierWave uploader.
33
+ #
34
+ # Example:
35
+ #
36
+ # version :thumb do
37
+ # process :crop => [10, 10, 200, 200]
38
+ # process :process_to_fill => [500, 500]
39
+ # end
40
+ #
41
+ # This stores the crop function as the "primary" function, and the process_to_fill
42
+ # function as a "secondary" function.
43
+ def process(function_hash)
44
+ function_hash = { function_hash => nil } unless function_hash.is_a?(Hash)
45
+ function_name = function_hash.keys.first
46
+ function_params = function_hash.values.first
47
+ function = Function.new(function_name, function_params)
48
+ if primary_function.nil?
49
+ self.primary_function = function
50
+ else
51
+ self.secondary_functions << function
52
+ end
53
+ end
54
+
55
+ # Returns a String of the name of the primary function for this version
56
+ # (default: "no_op")
57
+ def primary_function_name
58
+ primary_function.nil? ? "no_op" : primary_function.name
59
+ end
60
+
61
+ # Returns a Hash of the params of the primary function for this
62
+ # version (default: {})
63
+ def primary_function_params
64
+ primary_function.nil? ? {} : primary_function.params
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
@@ -0,0 +1,82 @@
1
+ module CarrierWave
2
+ module Blitline
3
+
4
+ # A presenter class for converting an image version to a JSON param for the Blitline
5
+ # API.
6
+ class ImageVersionFunctionPresenter
7
+
8
+
9
+ ##
10
+ # The ImageVersion we're presenting
11
+ attr_accessor :version
12
+
13
+ ##
14
+ # The Uploader instance we're processing an image for.
15
+ attr_accessor :uploader
16
+
17
+
18
+ # =============
19
+ # = Delegates =
20
+ # =============
21
+
22
+ delegate :params_for_function, to: :uploader
23
+
24
+ delegate :file_name_for_version, to: :uploader
25
+
26
+ delegate :unique_identifier, to: :uploader
27
+
28
+ delegate :primary_function_name, to: :version
29
+
30
+ delegate :primary_function_params, to: :version
31
+
32
+ delegate :secondary_functions, to: :version
33
+
34
+
35
+ # Creates a new presenter.
36
+ #
37
+ # version - The ImageVersion to use
38
+ # uploader - The CarrierWave uploader instance
39
+ def initialize(version, uploader)
40
+ @version = version
41
+ @uploader = uploader
42
+ end
43
+
44
+ # The Hash to be converted to JSON for the Blitline API
45
+ def to_hash
46
+ {
47
+ "name": primary_function_name,
48
+ "params": params_for_function(primary_function_name, primary_function_params),
49
+ "save": {
50
+ "image_identifier": unique_identifier,
51
+ "s3_destination": {
52
+ "bucket": {
53
+ "name": ENV["S3_BUCKET_NAME"],
54
+ "location": ENV["S3_BUCKET_REGION"],
55
+ },
56
+ "key": file_name_for_version(version)
57
+ }
58
+ },
59
+ "functions": secondary_functions.map { |function|
60
+ {
61
+ "name": function.name,
62
+ "params": params_for_function(function.name,function.params),
63
+ "save": {
64
+ "image_identifier": unique_identifier,
65
+ "s3_destination": {
66
+ "bucket": {
67
+ "name": ENV["S3_BUCKET_NAME"],
68
+ "location": ENV["S3_BUCKET_REGION"],
69
+ },
70
+ "key": file_name_for_version(version)
71
+ }
72
+ }
73
+ }
74
+ }
75
+ }
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module Blitline
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-blitline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bodacious
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: blitline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Integrates the carrierwave gem with Blitline image API. (Still under
56
+ development)
57
+ email:
58
+ - team@katanacode.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - carrierwave-blitline.gemspec
71
+ - lib/carrierwave/blitline.rb
72
+ - lib/carrierwave/blitline/class_methods.rb
73
+ - lib/carrierwave/blitline/function.rb
74
+ - lib/carrierwave/blitline/image_version.rb
75
+ - lib/carrierwave/blitline/image_version_function_presenter.rb
76
+ - lib/carrierwave/blitline/version.rb
77
+ homepage: https://github.com/KatanaCode/carrierwave-blitline
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Integrates Blitline image processing with Carrierwave
101
+ test_files: []