resizing 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 46b4c727294bdd92f33e9fa5cf32ee2cd320987a1b2d65dccba240a5a776f0b4
4
+ data.tar.gz: c9c7b2a9b568c216804afb3f6108e9190644df0ed241eee73a600fe228982eca
5
+ SHA512:
6
+ metadata.gz: d8fc3430aef73a2b20adbc4d6c649c62ff467944e03db06b977d1dc517205644573e5ab87eb891a3539ae9776e1f4adbb3b9fded9eb782b203c244892ad3e2be
7
+ data.tar.gz: e5f0122b6a5d06f04b55ef79f9da102f603cd397941135787ff1d121afedcbc4d18f6d72f472e4552daa7f11099c9dd493a175ef1763145c0828b72073d215b3
@@ -0,0 +1,71 @@
1
+ version: 2
2
+
3
+ jobs:
4
+ build:
5
+ docker:
6
+ - image: circleci/ruby:2.6.2-node
7
+ environment:
8
+ BUNDLE_JOBS: 4
9
+ BUNDLE_RETRY: 3
10
+ BUNDLE_PATH: vendor/bundle
11
+ BUNDLE_APP_CONFIG: "./.bundle"
12
+ TZ: /usr/share/zoneinfo/Asia/Tokyo
13
+ LANG: ja_JP.UTF8
14
+ - image: circleci/mysql:5.7
15
+ environment:
16
+ MYSQL_ROOT_PASSWORD: secret
17
+ MYSQL_DATABASE: resizing_gem_test
18
+ MYSQL_USER: resizing_gem
19
+ MYSQL_PASSWORD: secret
20
+ TZ: /usr/share/zoneinfo/Asia/Tokyo
21
+ LANG: ja_JP.UTF8
22
+ command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin
23
+
24
+ steps:
25
+ - checkout
26
+ - run:
27
+ name: Which bundler?
28
+ command: |
29
+ bundle -v
30
+ bundle config
31
+
32
+ - restore_cache:
33
+ keys:
34
+ - v2-{{ .Environment.CACHE_KEY }}-gem-cache-{{ arch }}-{{ .Branch }}
35
+ - v2-{{ .Environment.CACHE_KEY }}-gem-cache
36
+
37
+ - run:
38
+ name: reinstall rake && bundle install
39
+ command: |
40
+ sudo gem install bundler
41
+ sudo gem uninstall -x rake
42
+ echo "install: --no-document" >> ~/.gemrc
43
+ bundle check --path vendor/bundle || bundle install --path vendor/bundle --clean
44
+
45
+ - save_cache:
46
+ key: v2-{{ .Environment.CACHE_KEY }}-gem-cache-{{ arch }}-{{ .Branch }}
47
+ paths:
48
+ - vendor/bundle
49
+
50
+ - run:
51
+ name: connectable MySQL?
52
+ command: |
53
+ dockerize -wait tcp://127.0.0.1:3306 -timeout 1m
54
+
55
+ # - run:
56
+ # name: rubocop
57
+ # command: bundle exec rubocop --parallel
58
+ # - run:
59
+ # name: brakeman
60
+ # command: bundle exec brakeman --no-exit-on-error -w 1 -z
61
+ - run:
62
+ command: bundle exec rake test
63
+ when: always
64
+ - store_test_results:
65
+ path: test/reports
66
+
67
+ workflows:
68
+ version: 2
69
+ build-deploy:
70
+ jobs:
71
+ - build
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
11
+
12
+ Metrics/MethodLength:
13
+ Max: 20
14
+ Exclude:
15
+ - "test/**/*.rb"
16
+
17
+ Metrics/ClassLength:
18
+ Exclude:
19
+ - "test/**/*.rb"
20
+
21
+ Metrics/AbcSize:
22
+ Exclude:
23
+ - "test/**/*.rb"
24
+
25
+ Layout/LineLength:
26
+ Exclude:
27
+ - "test/**/*.rb"
28
+
29
+ Style/Documentation:
30
+ Enabled: false
31
+
32
+ Layout/EmptyLinesAroundAttributeAccessor:
33
+ Enabled: false
34
+
35
+ Layout/SpaceAroundMethodCallOperator:
36
+ Enabled: false
37
+
38
+ Lint/DeprecatedOpenSSLConstant:
39
+ Enabled: true
40
+
41
+ Lint/MixedRegexpCaptureTypes:
42
+ Enabled: true
43
+
44
+ Lint/RaiseException:
45
+ Enabled: true
46
+
47
+ Lint/StructNewOverride:
48
+ Enabled: true
49
+
50
+ Style/ExponentialNotation:
51
+ Enabled: true
52
+
53
+ Style/HashEachMethods:
54
+ Enabled: false
55
+
56
+ Style/HashTransformKeys:
57
+ Enabled: false
58
+
59
+ Style/HashTransformValues:
60
+ Enabled: false
61
+
62
+ Style/RedundantRegexpCharacterClass:
63
+ Enabled: false
64
+
65
+ Style/RedundantRegexpEscape:
66
+ Enabled: false
67
+
68
+ Style/SlicingWithRange:
69
+ Enabled: false
70
+
71
+ # temporary
72
+ Style/AsciiComments:
73
+ Enabled: false
74
+
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in resizing.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0'
9
+ gem 'rake', '~> 12.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Junichiro Kasuya
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,69 @@
1
+ # Resizing
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/resizing`. 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 'resizing'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install resizing
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ # initialize client
27
+ options = {
28
+ project_id: '098a2a0d-0000-0000-0000-000000000000',
29
+ secret_token: '4g1cshg......rbs6'
30
+ }
31
+ client = Resizing::Client.new(options)
32
+
33
+ # upload image to resizing
34
+ file = File.open('sample.jpg', 'r')
35
+ response = client.post(file)
36
+ => {
37
+ "id"=>"a4ed2bf0-a4cf-44fa-9c82-b53e581cb469",
38
+ "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
39
+ "content_type"=>"image/jpeg",
40
+ "latest_version_id"=>"LJY5bxBF7Ryxfr5kC1F.63W8bzp3pcUm",
41
+ "latest_etag"=>"\"190143614e6c342637584f46f18f8c58\"",
42
+ "created_at"=>"2020-05-15T15:33:10.711Z",
43
+ "updated_at"=>"2020-05-15T15:33:10.711Z",
44
+ "url"=>"/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469"
45
+ }
46
+
47
+ name = response['url']
48
+ # get transformation url
49
+ name = response['url']
50
+ transform = {width: 200, height: 300}
51
+
52
+ transformation_url = Resizing.url(name, transform)
53
+ => "https://www.resizing.net/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469/width_200,height_300"
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'resizing'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,13 @@
1
+ version: '3.4'
2
+
3
+ services:
4
+ mysql:
5
+ image: mysql:5.7.29
6
+ command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_bin
7
+ ports:
8
+ - 3306:3306
9
+ environment:
10
+ MYSQL_ROOT_PASSWORD: secret
11
+ MYSQL_DATABASE: resizing_gem_test
12
+ MYSQL_USER: resizing_gem
13
+ MYSQL_PASSWORD: secret
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resizing/version'
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ module Resizing
8
+ autoload :Client, 'resizing/client'
9
+ autoload :Configuration, 'resizing/configuration'
10
+ autoload :CarrierWave, 'resizing/carrier_wave'
11
+
12
+ class Error < StandardError; end
13
+ class ConfigurationError < Error; end
14
+ class APIError < Error; end
15
+
16
+ def self.configure
17
+ raise ConfigurationError, 'Resizing.configure is not initialized' unless defined? @configure
18
+
19
+ @configure.dup
20
+ end
21
+
22
+ def self.configure=(new_value)
23
+ new_value = Configuration.new(new_value) unless new_value.is_a? Configuration
24
+ @configure = new_value
25
+ end
26
+
27
+ def self.get(name)
28
+ raise NotImplementedError
29
+ end
30
+
31
+ def self.url_from_image_id(image_id, version_id = nil, transformations = [])
32
+ Resizing.configure.generate_image_url(image_id, version_id, transformations)
33
+ end
34
+
35
+ def self.post(file_or_binary, options)
36
+ client = Resizing::Client.new
37
+ client.post file_or_binary, options
38
+ end
39
+
40
+ def self.put(name, file_or_binary, options)
41
+ client = Resizing::Client.new
42
+ client.put name, file_or_binary, options
43
+ end
44
+
45
+ # TODO: refactoring
46
+ #
47
+ # identifier:
48
+ # public_id: /projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850..
49
+ # identifier: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50
+ # project_id: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51
+ # image_id: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52
+ # version: ^^^^^^^^^
53
+ def self.generate_identifier
54
+ Resizing.configure.generate_identifier
55
+ end
56
+
57
+ def self.separate_public_id public_id
58
+ public_id.match('/projects/(?<project_id>[0-9a-f-]+)/upload/images/(?<image_id>[0-9a-f-]+)(/v(?<version>[^/]+))?')
59
+ end
60
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resizing
4
+ module ActiveStorage
5
+ module Service
6
+ # ref.
7
+ # https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/s3_service.rb
8
+ #
9
+ # rubocop:disable Lint/UnusedMethodArgument,Metrics/ParameterLists
10
+ class ResizingService < ::ActiveStorage::Service
11
+ # def initialize(bucket:, upload: {}, public: false, **options)
12
+ def initialize; end
13
+
14
+ def upload(_key, _io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **)
15
+ raise NotImplementedError, 'upload is not implemented'
16
+ end
17
+
18
+ def download(_key)
19
+ raise NotImplementedError, 'download is not implemented'
20
+ end
21
+
22
+ def download_chunk(_key, _range)
23
+ raise NotImplementedError, 'download_chunk is not implemented'
24
+ end
25
+
26
+ def delete(_key)
27
+ raise NotImplementedError, 'delete is not implemented'
28
+ end
29
+
30
+ def exist?(_key)
31
+ raise NotImplementedError, 'exist? is not implemented'
32
+ end
33
+
34
+ def url_for_direct_upload(_key, expires_in:, content_type:, conteont_length:, checksum:)
35
+ raise NotImplementedError, 'url_for_direct_upload is not implemented'
36
+ end
37
+
38
+ def headers_for_direct_upload(_key, content_type:, checksum:, filename: nil, disposition: nil, **)
39
+ raise NotImplementedError, 'headers_for_direct_upload is not implemented'
40
+ end
41
+
42
+ private
43
+
44
+ # call from ActiveStorage::Service.url
45
+ # https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service.rb#L111
46
+ def private_url(_key, expires_in:, filename:, content_type:, disposition:, **)
47
+ raise NotImplementedError, 'private_url is not implemented'
48
+ end
49
+
50
+ def public_url(_key, filename:, content_type: nil, disposition: :attachment, **)
51
+ raise NotImplementedError, 'public_url is not implemented'
52
+ end
53
+ end
54
+ # rubocop:enable Lint/UnusedMethodArgument,Metrics/ParameterLists
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resizing/carrier_wave/storage/file'
4
+ require 'resizing/carrier_wave/storage/remote'
5
+
6
+ module Resizing
7
+ module CarrierWave
8
+ class Railtie < ::Rails::Railtie
9
+ # Railtie skelton codes
10
+ rake_tasks do
11
+ # NOP
12
+ end
13
+
14
+ config.after_initialize do |app|
15
+ # NOP
16
+ end
17
+
18
+ ActiveSupport.on_load(:active_record) do
19
+ # NOP
20
+ end
21
+ end
22
+
23
+ def self.included(base)
24
+ base.storage Resizing::CarrierWave::Storage::Remote
25
+ base.extend ClassMethods
26
+ end
27
+
28
+ def url(*args)
29
+ return nil unless read_column.present?
30
+
31
+ transforms = [transform_string]
32
+
33
+ while (version = args.pop)
34
+ transforms << versions[version].transform_string
35
+ end
36
+ "#{default_url}/#{transforms.join('/')}"
37
+ end
38
+
39
+ def read_column
40
+ model.read_attribute(mounted_as)
41
+ end
42
+
43
+ def default_url
44
+ "#{Resizing.configure.host}#{model.read_attribute(mounted_as)}"
45
+ end
46
+
47
+ def transform_string
48
+ transforms = processors.map do |processor|
49
+ transform_string_from processor
50
+ end
51
+
52
+ transforms.join('/')
53
+ end
54
+
55
+ def rename
56
+ raise NotImplementedError, 'rename is not implemented'
57
+ end
58
+
59
+ def resize_to_limit(*args)
60
+ @transform ||= []
61
+ @transform.push(:resize_to_limit, *args)
62
+ end
63
+
64
+ def resize_to_fill(*args)
65
+ @transform ||= []
66
+ @transform.push(:resize_to_fill, *args)
67
+ end
68
+
69
+ def resize_to_fit(*args)
70
+ @transform ||= []
71
+ @transform.push(:resize_to_fit, *args)
72
+ end
73
+
74
+ def cache!(new_file)
75
+ return if new_file.nil?
76
+
77
+ file = storage.store!(new_file)
78
+ # do not assign @cache_id, bacause resizing do not support cache
79
+ # save to resizing directly
80
+ # @cache_id = file.public_id
81
+
82
+ @filename = file.public_id
83
+ @file = file
84
+ end
85
+
86
+ def store!
87
+ # DO NOTHING
88
+ super
89
+ end
90
+
91
+ module ClassMethods
92
+ end
93
+
94
+ # store_versions! is called after store!
95
+ # Disable on Resizing, because transform the image when browser fetch the image URL
96
+ # https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L18
97
+ def store_versions!(*args)
98
+ # NOP
99
+ end
100
+
101
+ # store_versions! is called after delete
102
+ # Disable on Resizing, because transform the image when browser fetch the image URL
103
+ # https://github.com/carrierwaveuploader/carrierwave/blob/28190e99299a6131c0424a5d10205f471e39f3cd/lib/carrierwave/uploader/versions.rb#L18
104
+ def remove_versions!(*args)
105
+ # NOP
106
+ end
107
+
108
+ private
109
+
110
+ # rubocop:disable Metrics/AbcSize
111
+ def transform_string_from(processor)
112
+ case processor.first
113
+ when :resize_to_fill, :resize_to_limit, :resize_to_fit
114
+ name = processor.first.to_s.gsub(/resize_to_/, '')
115
+ { c: name, w: processor.second.first, h: processor.second.second }
116
+ else
117
+ raise NotImplementedError, "#{processor.first} is not supported. #{processor.inspect}"
118
+ end.map do |key, value|
119
+ next nil if value.nil?
120
+
121
+ "#{key}_#{value}"
122
+ end.compact.join(',')
123
+ end
124
+ # rubocop:enable Metrics/AbcSize
125
+ end
126
+ end