activestorage_upyun 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63a6380c6742a8c295a27ed08fbed538d80888fc
4
+ data.tar.gz: 3f85e171b2bd4b66d19544a4d95190606ab1e0a1
5
+ SHA512:
6
+ metadata.gz: 9f5670c173af4fc03c4017be87ef4496a42e0a868317c938cf3511e50b4dbb8fc316d9e2778c27f6f1105e009daaa42aba97cd049c282dfe91d513ace85bcaf8
7
+ data.tar.gz: 80419e0833f88e05add98ddc96954d422bae9e5c2f968be0053859e5a40ebe348d8dd98d6a0452b1126973ec0ba976fc0b7446ca8629097319fbff1440e2df42
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 doabit
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # ActivestorageUpyun
2
+ Upyun service for activestorage.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'activestorage_upyun'
9
+ ```
10
+
11
+ Set up upyun storage service in config/storage.yml:
12
+
13
+ ```yml
14
+ upyun:
15
+ service: Upyun
16
+ bucket: <%= ENV['UPYUUN_BUCKET'] %>
17
+ operator: <%= ENV['UPYUUN_OPERATOR'] %>
18
+ password: <%= ENV['UPYUUN_PASSWORD'] %>
19
+ host: <%= ENV['UPYUN_HOST'] %>
20
+ folder: <%= ENV['UPYUN_FOLDER'] %>
21
+ ```
22
+
23
+ Set up activestorage service:
24
+
25
+ ```ruby
26
+ config.active_storage.service = :upyun
27
+ ```
28
+
29
+ ## License
30
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActivestorageUpyun'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,139 @@
1
+ module ActiveStorage
2
+ # Wraps the upyun Storage Service as an Active Storage service.
3
+ # See ActiveStorage::Service for the generic API documentation that applies to all services.
4
+ #
5
+ # you can set-up upyun storage service through the generated <tt>config/storage.yml</tt> file.
6
+ # For example:
7
+ #
8
+ # upyun:
9
+ # service: Upyun
10
+ # bucket: <%= ENV['UPYUUN_BUCKET'] %>
11
+ # operator: <%= ENV['UPYUUN_OPERATOR'] %>
12
+ # password: <%= ENV['UPYUUN_PASSWORD'] %>
13
+ # host: <%= ENV['UPYUN_HOST'] %>
14
+ # folder: <%= ENV['UPYUN_FOLDER'] %>
15
+ #
16
+ # Then, in your application's configuration, you can specify the service to
17
+ # use like this:
18
+ #
19
+ # config.active_storage.service = :upyun
20
+ #
21
+ #
22
+ class Service::UpyunService < Service
23
+ ENDPOINT = 'http://v0.api.upyun.com'
24
+
25
+ attr_reader :upyun, :bucket, :operator, :password, :host, :folder, :upload_options
26
+
27
+ def initialize(bucket:, operator:, password:, host:, folder:, **options)
28
+ @bucket = bucket
29
+ @host = host
30
+ @folder = folder
31
+ @operator = operator
32
+ @password = password
33
+ @upload_options = options
34
+ @upyun = Upyun::Rest.new(bucket, operator, password, options)
35
+ end
36
+
37
+ def upload(key, io, checksum: nil)
38
+ instrument :upload, key: key, checksum: checksum do
39
+ begin
40
+ result = @upyun.put(path_for(key), io)
41
+ result
42
+ rescue
43
+ raise ActiveStorage::IntegrityError
44
+ end
45
+ end
46
+ end
47
+
48
+ def delete(key)
49
+ instrument :delete, key: key do
50
+ @upyun.delete(path_for(key))
51
+ end
52
+ end
53
+
54
+ def download(key)
55
+ instrument :download, key: key do
56
+ io = @upyun.get(path_for(key))
57
+ if block_given?
58
+ yield io
59
+ else
60
+ io
61
+ end
62
+ end
63
+ end
64
+
65
+ def exist?(key)
66
+ instrument :exist, key: key do |payload|
67
+ answer = upyun.getinfo(path_for(key))
68
+ result = answer[:error].nil?
69
+ payload[:exist] = result
70
+ result
71
+ end
72
+ end
73
+
74
+ def url(key, **options)
75
+ instrument :url, key: key do |payload|
76
+ url = url_for(key)
77
+ payload[:url] = url
78
+ url
79
+ end
80
+ end
81
+
82
+ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
83
+ instrument :url, key: key do |payload|
84
+ url = [ENDPOINT, @bucket , @folder, key].join('/')
85
+ payload[:url] = url
86
+ url
87
+ end
88
+ end
89
+
90
+ def headers_for_direct_upload(key, content_type:, checksum:, content_length:, **)
91
+ user = @operator
92
+ pwd = md5(@password)
93
+ method = 'PUT'
94
+ uri = ["/#{@bucket}", @folder, key].join('/')
95
+ date = gmdate
96
+
97
+ str = [method, uri, date].join("&")
98
+ signature = Base64.strict_encode64(
99
+ OpenSSL::HMAC.digest('sha1', pwd, str)
100
+ )
101
+ auth = "UPYUN #{@operator}:#{signature}"
102
+ {"Content-Type" => content_type, "Authorization" => auth, "X-Date" => date}
103
+ end
104
+
105
+ def delete_prefixed(prefix)
106
+ instrument :delete_prefixed, prefix: prefix do
107
+ items = @upyun.getlist "/#{@folder}/#{prefix}"
108
+ items.each do |file|
109
+ @upyun.delete("/#{@folder}/#{prefix}#{file[:name]}")
110
+ end
111
+ @upyun.delete("/#{@folder}/#{prefix}")
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def url_for(key)
118
+ [@host, @folder, key].join('/')
119
+ end
120
+
121
+ def path_for(key)
122
+ [@folder, key].join('/')
123
+ end
124
+
125
+ def fullpath(path)
126
+ decoded = URI::encode(URI::decode(path.to_s.force_encoding('utf-8')))
127
+ decoded = decoded.gsub('[', '%5B').gsub(']', '%5D')
128
+ "/#{@bucket}#{decoded.start_with?('/') ? decoded : '/' + decoded}"
129
+ end
130
+
131
+ def md5(str)
132
+ Digest::MD5.hexdigest(str)
133
+ end
134
+
135
+ def gmdate
136
+ Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,6 @@
1
+ require "upyun"
2
+ require "activestorage_upyun/version"
3
+ require 'active_storage/service/upyun_service'
4
+ module ActivestorageUpyun
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module ActivestorageUpyun
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activestorage_upyun do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activestorage_upyun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - doabit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activestorage
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: upyun
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.8
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.8
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Upyun service for activestorage
84
+ email:
85
+ - doinsist@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/active_storage/service/upyun_service.rb
94
+ - lib/activestorage_upyun.rb
95
+ - lib/activestorage_upyun/version.rb
96
+ - lib/tasks/activestorage_upyun_tasks.rake
97
+ homepage: https://github.com/doabit/activestorage_upyun
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.8
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Upyun service for activestorage
121
+ test_files: []