resizing 1.0.3 → 1.1.0.pre
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 +4 -4
- data/.github/workflows/test.yml +2 -13
- data/.gitignore +0 -6
- data/Gemfile +2 -10
- data/README.md +32 -59
- data/lib/resizing/active_storage/service/resizing_service.rb +2 -6
- data/lib/resizing/carrier_wave/storage/file.rb +24 -68
- data/lib/resizing/carrier_wave/storage/remote.rb +3 -7
- data/lib/resizing/carrier_wave.rb +11 -19
- data/lib/resizing/client.rb +24 -40
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +16 -6
- data/lib/resizing/http_clientable.rb +3 -3
- data/lib/resizing/mock_client.rb +5 -6
- data/lib/resizing/public_id.rb +4 -5
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing/video/client.rb +116 -0
- data/lib/resizing/video.rb +8 -0
- data/lib/resizing.rb +12 -24
- data/resizing.gemspec +11 -9
- data/test/resizing/carrier_wave/storage/file_test.rb +8 -149
- data/test/resizing/carrier_wave/storage/remote_test.rb +0 -75
- data/test/resizing/carrier_wave_test.rb +32 -373
- data/test/resizing/client_test.rb +11 -96
- data/test/resizing/configuration_test.rb +2 -118
- data/test/resizing/public_id_test.rb +1 -1
- data/test/resizing/video/client_test.rb +158 -0
- data/test/resizing_test.rb +0 -16
- data/test/test_helper.rb +9 -148
- data/test/vcr/video/metadata/success.yml +47 -0
- data/test/vcr/video/prepare/success.yml +47 -0
- data/test/vcr/video/upload_completed/success.yml +47 -0
- metadata +43 -56
- data/lib/resizing/active_storage/service.rb +0 -9
- data/lib/resizing/active_storage.rb +0 -7
- data/test/resizing/active_storage_service_test.rb +0 -98
- data/test/resizing/configurable_test.rb +0 -82
- data/test/resizing/constants_test.rb +0 -25
- data/test/resizing/error_test.rb +0 -73
- data/test/resizing/http_clientable_test.rb +0 -84
- data/test/resizing/mock_client_test.rb +0 -75
- data/test/resizing_module_test.rb +0 -206
- data/test/vcr/carrier_wave_test/update_image.yml +0 -63
- /data/{bin → exe}/console +0 -0
- /data/{bin → exe}/generate-changelog +0 -0
- /data/{bin → exe}/setup +0 -0
|
@@ -27,8 +27,12 @@ module Resizing
|
|
|
27
27
|
def initialize(*attrs)
|
|
28
28
|
case attr = attrs.first
|
|
29
29
|
when Hash
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
if attr[:project_id].nil? || attr[:secret_token].nil?
|
|
31
|
+
raise_configiration_error
|
|
32
|
+
end
|
|
33
|
+
if attr[:host].present?
|
|
34
|
+
raise_configiration_error
|
|
35
|
+
end
|
|
32
36
|
|
|
33
37
|
initialize_by_hash attr
|
|
34
38
|
return
|
|
@@ -37,6 +41,11 @@ module Resizing
|
|
|
37
41
|
raise_configiration_error
|
|
38
42
|
end
|
|
39
43
|
|
|
44
|
+
def host
|
|
45
|
+
Kernel.warn "[DEPRECATED] The Configuration#host is deprecated. Use Configuration#image_host."
|
|
46
|
+
self.image_host
|
|
47
|
+
end
|
|
48
|
+
|
|
40
49
|
def generate_auth_header
|
|
41
50
|
current_timestamp = Time.now.to_i
|
|
42
51
|
data = [current_timestamp, secret_token].join('|')
|
|
@@ -93,11 +102,13 @@ module Resizing
|
|
|
93
102
|
raise ConfigurationError, 'need hash and some keys like :image_host, video_host, :project_id, :secret_token'
|
|
94
103
|
end
|
|
95
104
|
|
|
96
|
-
# rubocop:disable Metrics/AbcSize
|
|
97
105
|
def initialize_by_hash(attr)
|
|
98
|
-
raise 'The host on configuration is deprecated. Use image_host, video_host' if attr[:host].present?
|
|
99
|
-
|
|
100
106
|
@image_host = attr[:image_host].dup.freeze || DEFAULT_IMAGE_HOST
|
|
107
|
+
if attr[:host].present?
|
|
108
|
+
Kernel.warn "[DEPRECATED] The host on configration is deprecated. Use image_host, video_host" if attr[:host].present?
|
|
109
|
+
@image_host ||= attr[:host].dup.freeze || DEFAULT_HOST # for backward compatible
|
|
110
|
+
end
|
|
111
|
+
|
|
101
112
|
@video_host = attr[:video_host].dup.freeze || DEFAULT_VIDEO_HOST
|
|
102
113
|
@project_id = attr[:project_id].dup.freeze
|
|
103
114
|
@secret_token = attr[:secret_token].dup.freeze
|
|
@@ -105,6 +116,5 @@ module Resizing
|
|
|
105
116
|
@response_timeout = attr[:response_timeout] || DEFAULT_RESPONSE_TIMEOUT
|
|
106
117
|
@enable_mock = attr[:enable_mock] || false
|
|
107
118
|
end
|
|
108
|
-
# rubocop:enable Metrics/AbcSize
|
|
109
119
|
end
|
|
110
120
|
end
|
|
@@ -12,14 +12,14 @@ module Resizing
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
def handle_faraday_error
|
|
15
|
+
def handle_faraday_error &block
|
|
16
16
|
yield
|
|
17
17
|
rescue Faraday::TimeoutError => e
|
|
18
18
|
handle_timeout_error e
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def handle_timeout_error
|
|
22
|
-
raise APIError
|
|
21
|
+
def handle_timeout_error error
|
|
22
|
+
raise APIError.new("TimeoutError: #{error.inspect}")
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
end
|
data/lib/resizing/mock_client.rb
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Resizing
|
|
4
4
|
class MockClient
|
|
5
|
-
def post(
|
|
5
|
+
def post(file_or_binary, options = {})
|
|
6
6
|
r = load_yaml('test/vcr/client/post.yml')
|
|
7
7
|
JSON.parse(r['string'])
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
def put(name,
|
|
10
|
+
def put(name, file_or_binary, options)
|
|
11
11
|
r = load_yaml('test/vcr/client/put.yml')
|
|
12
12
|
result = JSON.parse(r['string'])
|
|
13
13
|
# replace name, public_id and version by name argument
|
|
@@ -26,7 +26,7 @@ module Resizing
|
|
|
26
26
|
result
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def metadata(name
|
|
29
|
+
def metadata(name)
|
|
30
30
|
r = load_yaml('test/vcr/client/metadata.yml')
|
|
31
31
|
result = JSON.parse(r['string'])
|
|
32
32
|
# replace name and public_id by name argument
|
|
@@ -34,16 +34,15 @@ module Resizing
|
|
|
34
34
|
result['public_id'].gsub!(/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/, name)
|
|
35
35
|
result
|
|
36
36
|
end
|
|
37
|
-
|
|
38
37
|
private
|
|
39
38
|
|
|
40
|
-
def load_yaml
|
|
39
|
+
def load_yaml filename
|
|
41
40
|
path = "#{library_root}/#{filename}"
|
|
42
41
|
YAML.load_file(path)['http_interactions'].first['response']['body']
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
def library_root
|
|
46
|
-
@library_root ||= File.expand_path('
|
|
45
|
+
@library_root ||= File.expand_path('../../../', __FILE__)
|
|
47
46
|
end
|
|
48
47
|
end
|
|
49
48
|
end
|
data/lib/resizing/public_id.rb
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
1
|
module Resizing
|
|
4
2
|
class PublicId
|
|
5
|
-
def initialize
|
|
3
|
+
def initialize public_id
|
|
6
4
|
@public_id = public_id
|
|
7
5
|
parsed
|
|
8
6
|
end
|
|
@@ -40,12 +38,13 @@ module Resizing
|
|
|
40
38
|
|
|
41
39
|
def parsed
|
|
42
40
|
return nil if @public_id.nil?
|
|
43
|
-
|
|
44
41
|
unless defined? @parsed
|
|
45
42
|
@parsed = Resizing.separate_public_id(@public_id)
|
|
46
|
-
raise "type error #{@public_id}" if @parsed
|
|
43
|
+
raise "type error #{@public_id}" if @parsed == nil
|
|
47
44
|
end
|
|
48
45
|
@parsed
|
|
49
46
|
end
|
|
47
|
+
|
|
48
|
+
private
|
|
50
49
|
end
|
|
51
50
|
end
|
data/lib/resizing/version.rb
CHANGED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Resizing
|
|
4
|
+
module Video
|
|
5
|
+
class Client
|
|
6
|
+
include Resizing::Constants
|
|
7
|
+
include Resizing::Configurable
|
|
8
|
+
include Resizing::HttpClientable
|
|
9
|
+
|
|
10
|
+
def initialize(*attrs)
|
|
11
|
+
initialize_config(*attrs)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def prepare
|
|
15
|
+
url = build_prepare_url
|
|
16
|
+
|
|
17
|
+
response = handle_faraday_error do
|
|
18
|
+
http_client.post(url) do |request|
|
|
19
|
+
request.headers['X-ResizingToken'] = config.generate_auth_header
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
handle_prepare_response response
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def upload_completed response_or_url
|
|
26
|
+
url = url_from response_or_url, 'upload_completed_url'
|
|
27
|
+
|
|
28
|
+
response = handle_faraday_error do
|
|
29
|
+
http_client.put(url) do |request|
|
|
30
|
+
request.headers['X-ResizingToken'] = config.generate_auth_header
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
handle_upload_completed_response response
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def delete response_or_url
|
|
37
|
+
url = url_from response_or_url, 'destroy_url'
|
|
38
|
+
|
|
39
|
+
response = handle_faraday_error do
|
|
40
|
+
http_client.put(url) do |request|
|
|
41
|
+
request.headers['X-ResizingToken'] = config.generate_auth_header
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
handle_upload_completed_response response
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def metadata response_or_url
|
|
48
|
+
url = url_from response_or_url, 'self_url'
|
|
49
|
+
|
|
50
|
+
response = handle_faraday_error do
|
|
51
|
+
http_client.get(url) do |request|
|
|
52
|
+
request.headers['X-ResizingToken'] = config.generate_auth_header
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
handle_metadata_response response
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def build_prepare_url
|
|
59
|
+
"#{config.video_host}/projects/#{config.project_id}/upload/videos/prepare"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def url_from response_or_url, name
|
|
65
|
+
if response_or_url.kind_of? String
|
|
66
|
+
response_or_url
|
|
67
|
+
elsif response_or_url.kind_of? Hash
|
|
68
|
+
response_or_url[name.to_s] || response_or_url[name.intern]
|
|
69
|
+
else
|
|
70
|
+
raise ArgumentError, "upload_completed is require Hash or String"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def handle_prepare_response response
|
|
76
|
+
raise APIError, "no response is returned" if response.nil?
|
|
77
|
+
|
|
78
|
+
case response.status
|
|
79
|
+
when HTTP_STATUS_OK, HTTP_STATUS_CREATED
|
|
80
|
+
JSON.parse(response.body)
|
|
81
|
+
else
|
|
82
|
+
handle_error_response response
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def handle_upload_completed_response response
|
|
87
|
+
raise APIError, "no response is returned" if response.nil?
|
|
88
|
+
|
|
89
|
+
case response.status
|
|
90
|
+
when HTTP_STATUS_OK
|
|
91
|
+
JSON.parse(response.body)
|
|
92
|
+
else
|
|
93
|
+
handle_error_response response
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def handle_metadata_response response
|
|
98
|
+
raise APIError, "no response is returned" if response.nil?
|
|
99
|
+
|
|
100
|
+
case response.status
|
|
101
|
+
when HTTP_STATUS_OK
|
|
102
|
+
JSON.parse(response.body)
|
|
103
|
+
else
|
|
104
|
+
handle_error_response response
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def handle_error_response response
|
|
109
|
+
result = JSON.parse(response.body) rescue {}
|
|
110
|
+
err = APIError.new("invalid http status code #{response.status}")
|
|
111
|
+
err.decoded_body = result
|
|
112
|
+
raise err
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
data/lib/resizing.rb
CHANGED
|
@@ -6,15 +6,6 @@ require 'faraday/multipart'
|
|
|
6
6
|
require 'json'
|
|
7
7
|
|
|
8
8
|
module Resizing
|
|
9
|
-
# Faraday 1.x uses Faraday::UploadIO, Faraday 2.x uses Faraday::Multipart::FilePart
|
|
10
|
-
def self.file_part_class
|
|
11
|
-
if defined?(Faraday::Multipart::FilePart)
|
|
12
|
-
Faraday::Multipart::FilePart
|
|
13
|
-
else
|
|
14
|
-
Faraday::UploadIO
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
9
|
autoload :Constants, 'resizing/constants'
|
|
19
10
|
autoload :Configurable, 'resizing/configurable'
|
|
20
11
|
autoload :HttpClientable, 'resizing/http_clientable'
|
|
@@ -24,19 +15,16 @@ module Resizing
|
|
|
24
15
|
autoload :CarrierWave, 'resizing/carrier_wave'
|
|
25
16
|
autoload :PublicId, 'resizing/public_id'
|
|
26
17
|
autoload :Video, 'resizing/video'
|
|
27
|
-
autoload :ActiveStorage, 'resizing/active_storage'
|
|
28
18
|
|
|
29
19
|
class Error < StandardError; end
|
|
30
20
|
class ConfigurationError < Error; end
|
|
31
|
-
|
|
32
|
-
class APIError < Error
|
|
21
|
+
class APIError < Error;
|
|
33
22
|
def decoded_body
|
|
34
23
|
@decoded_body ||= {}
|
|
35
24
|
end
|
|
36
25
|
|
|
37
26
|
def decoded_body=(value)
|
|
38
27
|
raise ArgumentError, 'The decoded_body is expected to be passed a Hash.' unless value.is_a? Hash
|
|
39
|
-
|
|
40
28
|
@decoded_body = value
|
|
41
29
|
end
|
|
42
30
|
end
|
|
@@ -61,39 +49,39 @@ module Resizing
|
|
|
61
49
|
end
|
|
62
50
|
|
|
63
51
|
def self.post(file_or_binary, options)
|
|
64
|
-
client.post file_or_binary, options
|
|
52
|
+
self.client.post file_or_binary, options
|
|
65
53
|
end
|
|
66
54
|
|
|
67
55
|
def self.put(name, file_or_binary, options)
|
|
68
|
-
client.put name, file_or_binary, options
|
|
56
|
+
self.client.put name, file_or_binary, options
|
|
69
57
|
end
|
|
70
58
|
|
|
71
59
|
def self.delete(name)
|
|
72
|
-
client.delete name
|
|
60
|
+
self.client.delete name
|
|
73
61
|
end
|
|
74
62
|
|
|
75
63
|
def self.metadata(name, options)
|
|
76
|
-
client.metadata name, options
|
|
64
|
+
self.client.metadata name, options
|
|
77
65
|
end
|
|
78
66
|
|
|
79
67
|
# TODO: refactoring
|
|
80
68
|
#
|
|
81
69
|
# identifier:
|
|
82
|
-
# public_id: /projects
|
|
83
|
-
# identifier:
|
|
84
|
-
# project_id:
|
|
85
|
-
# image_id:
|
|
86
|
-
# version:
|
|
70
|
+
# public_id: /projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850..
|
|
71
|
+
# identifier: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
72
|
+
# project_id: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
73
|
+
# image_id: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
74
|
+
# version: ^^^^^^^^^
|
|
87
75
|
def self.generate_identifier
|
|
88
76
|
Resizing.configure.generate_identifier
|
|
89
77
|
end
|
|
90
78
|
|
|
91
|
-
def self.separate_public_id
|
|
79
|
+
def self.separate_public_id public_id
|
|
92
80
|
public_id.match('/projects/(?<project_id>[0-9a-f-]+)/upload/images/(?<image_id>[^/]+)(/v(?<version>[^/]+))?')
|
|
93
81
|
end
|
|
94
82
|
|
|
95
83
|
def self.client
|
|
96
|
-
if configure.enable_mock
|
|
84
|
+
if self.configure.enable_mock
|
|
97
85
|
Resizing::MockClient.new
|
|
98
86
|
else
|
|
99
87
|
Resizing::Client.new
|
data/resizing.gemspec
CHANGED
|
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
|
12
12
|
spec.description = 'Client and utilities for Resizing '
|
|
13
13
|
spec.homepage = 'https://github.com/jksy/resizing-gem'
|
|
14
14
|
spec.license = 'MIT'
|
|
15
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 3.0
|
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
|
16
16
|
|
|
17
17
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
18
18
|
|
|
@@ -25,18 +25,20 @@ Gem::Specification.new do |spec|
|
|
|
25
25
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
26
26
|
`git ls-files -z`.split("\x0")
|
|
27
27
|
end
|
|
28
|
+
spec.bindir = 'exe'
|
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
30
|
spec.require_paths = ['lib']
|
|
29
|
-
spec.add_runtime_dependency 'faraday', '
|
|
30
|
-
spec.add_runtime_dependency 'faraday-multipart'
|
|
31
|
-
spec.add_development_dependency 'rails',
|
|
32
|
-
spec.add_development_dependency 'carrierwave', '~>
|
|
31
|
+
spec.add_runtime_dependency 'faraday', '~> 2.3'
|
|
32
|
+
spec.add_runtime_dependency 'faraday-multipart'
|
|
33
|
+
spec.add_development_dependency 'rails', '~> 6.0'
|
|
34
|
+
spec.add_development_dependency 'carrierwave', '~> 2.2.5'
|
|
33
35
|
spec.add_development_dependency 'fog-aws'
|
|
34
|
-
spec.add_development_dependency 'minitest'
|
|
36
|
+
spec.add_development_dependency 'minitest'
|
|
35
37
|
spec.add_development_dependency 'minitest-ci'
|
|
36
|
-
spec.add_development_dependency 'mysql2'
|
|
37
38
|
spec.add_development_dependency 'rubocop'
|
|
38
|
-
spec.add_development_dependency 'simplecov'
|
|
39
|
-
spec.add_development_dependency 'simplecov-cobertura'
|
|
40
39
|
spec.add_development_dependency 'timecop'
|
|
41
40
|
spec.add_development_dependency 'vcr'
|
|
41
|
+
spec.add_development_dependency 'mysql2'
|
|
42
|
+
spec.add_development_dependency 'simplecov'
|
|
43
|
+
spec.add_development_dependency 'simplecov-cobertura'
|
|
42
44
|
end
|
|
@@ -7,163 +7,22 @@ module Resizing
|
|
|
7
7
|
module Storage
|
|
8
8
|
class FileTest < Minitest::Test
|
|
9
9
|
def setup
|
|
10
|
-
|
|
11
|
-
image_host: 'http://192.168.56.101:5000',
|
|
12
|
-
video_host: 'http://192.168.56.101:5000',
|
|
13
|
-
project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
|
|
14
|
-
secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
|
|
15
|
-
open_timeout: 10,
|
|
16
|
-
response_timeout: 20
|
|
17
|
-
}
|
|
18
|
-
Resizing.configure = @configuration_template
|
|
10
|
+
# NOP
|
|
19
11
|
end
|
|
20
12
|
|
|
21
13
|
def teardown
|
|
22
14
|
# NOP
|
|
23
15
|
end
|
|
24
16
|
|
|
25
|
-
def
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
assert_instance_of Resizing::CarrierWave::Storage::File, file
|
|
31
|
-
assert file.public_id.empty?
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def test_initialize_with_identifier
|
|
35
|
-
model = TestModel.new
|
|
36
|
-
uploader = model.resizing_picture
|
|
37
|
-
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
38
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
39
|
-
|
|
40
|
-
assert_instance_of Resizing::CarrierWave::Storage::File, file
|
|
41
|
-
assert_equal identifier, file.public_id.to_s
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def test_retrieve_sets_public_id
|
|
45
|
-
model = TestModel.new
|
|
46
|
-
uploader = model.resizing_picture
|
|
47
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
48
|
-
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
49
|
-
|
|
50
|
-
file.retrieve(identifier)
|
|
51
|
-
|
|
52
|
-
assert_equal identifier, file.public_id.to_s
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def test_delete_does_nothing_when_public_id_empty
|
|
56
|
-
model = TestModel.new
|
|
57
|
-
uploader = model.resizing_picture
|
|
58
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
59
|
-
|
|
60
|
-
# Should not raise any error and should return early
|
|
61
|
-
result = file.delete
|
|
62
|
-
assert_nil result
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def test_current_path_returns_nil_for_new_model
|
|
66
|
-
model = TestModel.new
|
|
67
|
-
uploader = model.resizing_picture
|
|
68
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
69
|
-
|
|
70
|
-
assert_nil file.current_path
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def test_path_alias
|
|
74
|
-
model = TestModel.new
|
|
75
|
-
uploader = model.resizing_picture
|
|
76
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
77
|
-
|
|
78
|
-
assert_nil file.current_path
|
|
79
|
-
assert_nil file.path
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
def test_authenticated_url_returns_nil
|
|
83
|
-
model = TestModel.new
|
|
84
|
-
uploader = model.resizing_picture
|
|
85
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
86
|
-
|
|
87
|
-
assert_nil file.authenticated_url
|
|
17
|
+
def test_store
|
|
18
|
+
# f = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
19
|
+
# upload_file = File.new(uploader, path)
|
|
20
|
+
# f.store(upload_file)
|
|
21
|
+
# f.save!
|
|
88
22
|
end
|
|
89
23
|
|
|
90
|
-
def
|
|
91
|
-
|
|
92
|
-
uploader = model.resizing_picture
|
|
93
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
94
|
-
|
|
95
|
-
assert_nil file.authenticated_url(expires_in: 3600)
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def test_extension_raises_not_implemented_error
|
|
99
|
-
model = TestModel.new
|
|
100
|
-
uploader = model.resizing_picture
|
|
101
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
102
|
-
|
|
103
|
-
assert_raises(NotImplementedError) do
|
|
104
|
-
file.extension
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def test_name_returns_image_id_from_public_id
|
|
109
|
-
model = TestModel.new
|
|
110
|
-
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
111
|
-
# Use write_attribute to set the column directly
|
|
112
|
-
model.send(:write_attribute, :resizing_picture, identifier)
|
|
113
|
-
uploader = model.resizing_picture
|
|
114
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
115
|
-
|
|
116
|
-
# name returns image_id (UUID) from public_id
|
|
117
|
-
assert_equal '14ea7aac-a194-4330-931f-6b562aec413d', file.name
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def test_store_uploads_file_and_sets_public_id
|
|
121
|
-
VCR.use_cassette 'carrier_wave_test/save', record: :once do
|
|
122
|
-
model = TestModel.new
|
|
123
|
-
uploader = model.resizing_picture
|
|
124
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
125
|
-
|
|
126
|
-
source_file = ::File.open('test/data/images/sample1.jpg', 'r')
|
|
127
|
-
uploaded_file = ActionDispatch::Http::UploadedFile.new(
|
|
128
|
-
filename: ::File.basename(source_file.path),
|
|
129
|
-
type: 'image/jpeg',
|
|
130
|
-
tempfile: source_file
|
|
131
|
-
)
|
|
132
|
-
|
|
133
|
-
result = file.store(uploaded_file)
|
|
134
|
-
|
|
135
|
-
assert result
|
|
136
|
-
refute file.public_id.empty?
|
|
137
|
-
assert_equal 'image/jpeg', file.content_type
|
|
138
|
-
end
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
def test_store_with_file_object
|
|
142
|
-
VCR.use_cassette 'carrier_wave_test/save', record: :once do
|
|
143
|
-
model = TestModel.new
|
|
144
|
-
uploader = model.resizing_picture
|
|
145
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
146
|
-
|
|
147
|
-
source_file = ::File.open('test/data/images/sample1.jpg', 'r')
|
|
148
|
-
|
|
149
|
-
result = file.store(source_file)
|
|
150
|
-
|
|
151
|
-
assert result
|
|
152
|
-
refute file.public_id.empty?
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def test_delete_with_valid_public_id
|
|
157
|
-
VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
|
|
158
|
-
model = TestModel.new
|
|
159
|
-
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
160
|
-
model.send(:write_attribute, :resizing_picture, identifier)
|
|
161
|
-
uploader = model.resizing_picture
|
|
162
|
-
file = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
163
|
-
|
|
164
|
-
# This should call delete on Resizing API
|
|
165
|
-
file.delete
|
|
166
|
-
end
|
|
24
|
+
def uploader
|
|
25
|
+
MiniTest::Mock.expect(:cache_path)
|
|
167
26
|
end
|
|
168
27
|
end
|
|
169
28
|
end
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'test_helper'
|
|
4
|
-
|
|
5
|
-
module Resizing
|
|
6
|
-
module CarrierWave
|
|
7
|
-
module Storage
|
|
8
|
-
class RemoteTest < Minitest::Test
|
|
9
|
-
def setup
|
|
10
|
-
@configuration_template = {
|
|
11
|
-
image_host: 'http://192.168.56.101:5000',
|
|
12
|
-
video_host: 'http://192.168.56.101:5000',
|
|
13
|
-
project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
|
|
14
|
-
secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
|
|
15
|
-
open_timeout: 10,
|
|
16
|
-
response_timeout: 20
|
|
17
|
-
}
|
|
18
|
-
Resizing.configure = @configuration_template
|
|
19
|
-
|
|
20
|
-
model = TestModel.new
|
|
21
|
-
@uploader = model.resizing_picture
|
|
22
|
-
@storage = Remote.new(@uploader)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def teardown
|
|
26
|
-
# NOP
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def test_storage_has_required_methods
|
|
30
|
-
assert_respond_to @storage, :store!
|
|
31
|
-
assert_respond_to @storage, :remove!
|
|
32
|
-
assert_respond_to @storage, :cache!
|
|
33
|
-
assert_respond_to @storage, :retrieve_from_cache!
|
|
34
|
-
assert_respond_to @storage, :delete_dir!
|
|
35
|
-
assert_respond_to @storage, :clean_cache!
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def test_retrieve_from_cache_returns_nil
|
|
39
|
-
result = @storage.retrieve_from_cache!('identifier')
|
|
40
|
-
assert_nil result
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def test_delete_dir_does_nothing
|
|
44
|
-
# Should not raise any exception
|
|
45
|
-
assert_nil @storage.delete_dir!('/path/to/dir')
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def test_clean_cache_does_nothing
|
|
49
|
-
# Should not raise any exception
|
|
50
|
-
assert_nil @storage.clean_cache!(3600)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def test_storage_initialization
|
|
54
|
-
assert_instance_of Remote, @storage
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def test_retrieve_returns_nil_for_blank_identifier
|
|
58
|
-
result = @storage.retrieve!(nil)
|
|
59
|
-
assert_nil result
|
|
60
|
-
|
|
61
|
-
result = @storage.retrieve!('')
|
|
62
|
-
assert_nil result
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def test_retrieve_returns_file_for_valid_identifier
|
|
66
|
-
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
67
|
-
result = @storage.retrieve!(identifier)
|
|
68
|
-
|
|
69
|
-
assert_instance_of Resizing::CarrierWave::Storage::File, result
|
|
70
|
-
assert_equal identifier, result.public_id.to_s
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|