paperclip-new-qiniu 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: 8415e571d5ecd665c1b71d18468822fa2fc650cb
4
+ data.tar.gz: acd84183495ddd26160821018d8bbcc621fcd3fd
5
+ SHA512:
6
+ metadata.gz: '094d414e2f13e106153bbe9c60a8da3fa65d2c94cee7fab6a4ea02593789780a58fefa2af7cacbba5b6e88d85a39dd1c1ff6249a0a109d1d6b28fe66c146100a'
7
+ data.tar.gz: 39ff406423763484b13ec06d82198de231806dee43c23eaf522f0e85a3cf344178be65a82437302f9d0912e624d248c8f39e30673eed4f140b68eea134d637f5
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-qiniu.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paperclip-qiniu (0.2.0)
5
+ paperclip
6
+ qiniu (~> 6.4.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.0)
12
+ activesupport (= 4.2.0)
13
+ builder (~> 3.1)
14
+ activesupport (4.2.0)
15
+ i18n (~> 0.7)
16
+ json (~> 1.7, >= 1.7.7)
17
+ minitest (~> 5.1)
18
+ thread_safe (~> 0.3, >= 0.3.4)
19
+ tzinfo (~> 1.1)
20
+ builder (3.2.2)
21
+ climate_control (0.0.3)
22
+ activesupport (>= 3.0)
23
+ cocaine (0.5.5)
24
+ climate_control (>= 0.0.3, < 1.0)
25
+ diff-lcs (1.2.5)
26
+ i18n (0.7.0)
27
+ json (1.8.2)
28
+ mime-types (2.4.3)
29
+ minitest (5.5.1)
30
+ netrc (0.10.2)
31
+ paperclip (4.2.1)
32
+ activemodel (>= 3.0.0)
33
+ activesupport (>= 3.0.0)
34
+ cocaine (~> 0.5.3)
35
+ mime-types
36
+ qiniu (6.4.1)
37
+ json (~> 1.7)
38
+ mime-types (~> 2.4.3)
39
+ rest-client (~> 1.6)
40
+ ruby-hmac (~> 0.4)
41
+ rest-client (1.7.2)
42
+ mime-types (>= 1.16, < 3.0)
43
+ netrc (~> 0.7)
44
+ rspec (3.2.0)
45
+ rspec-core (~> 3.2.0)
46
+ rspec-expectations (~> 3.2.0)
47
+ rspec-mocks (~> 3.2.0)
48
+ rspec-core (3.2.0)
49
+ rspec-support (~> 3.2.0)
50
+ rspec-expectations (3.2.0)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.2.0)
53
+ rspec-mocks (3.2.0)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.2.0)
56
+ rspec-support (3.2.1)
57
+ ruby-hmac (0.4.0)
58
+ thread_safe (0.3.4)
59
+ tzinfo (1.2.2)
60
+ thread_safe (~> 0.1)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ paperclip-qiniu!
67
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+
2
+ LICENSE
3
+
4
+ The MIT License
5
+
6
+ Copyright (c) 2008-2016 Jon Yurek and thoughtbot, inc.
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Paperclip::Qiniu
2
+
3
+ storage [paperclip](https://github.com/thoughtbot/paperclip/) attachments to http://qiniutek.com
4
+
5
+ example project: https://github.com/lidaobing/paperclip-qiniu-example
6
+
7
+ example site: http://paperclip-qiniu-example.herokuapp.com/
8
+
9
+ if you are using Rails 3, please use version 0.1.0: https://rubygems.org/gems/paperclip-qiniu/versions/0.1.0
10
+
11
+ ## Usage
12
+
13
+ * confirm you are working on a rails app
14
+
15
+ * add following line to `Gemfile`
16
+
17
+ ```ruby
18
+ gem 'paperclip'
19
+ gem 'paperclip-qiniu'
20
+ # or get the latest version
21
+ # gem 'paperclip-qiniu', :git => "git://github.com/lidaobing/paperclip-qiniu"
22
+ ```
23
+
24
+ * create `config/initializers/paperclip.rb`
25
+
26
+ ```ruby
27
+ Paperclip::Attachment.default_options[:storage] = :qiniu
28
+ Paperclip::Attachment.default_options[:qiniu_credentials] = {
29
+ :access_key => ENV['QINIU_ACCESS_KEY'] || raise("set env QINIU_ACCESS_KEY"),
30
+ :secret_key => ENV['QINIU_SECRET_KEY'] || raise("set env QINIU_SECRET_KEY")
31
+ }
32
+ Paperclip::Attachment.default_options[:bucket] = 'paperclip-qiniu-example'
33
+ Paperclip::Attachment.default_options[:use_timestamp] = false
34
+ Paperclip::Attachment.default_options[:qiniu_host] =
35
+ 'http://cdn.example.com'
36
+ ```
37
+
38
+ for more information on `qiniu_host`, read http://docs.qiniutek.com/v2/sdk/ruby/#publish
39
+
40
+ * add a model like this
41
+
42
+ ```ruby
43
+ class Image < ActiveRecord::Base
44
+ attr_accessible :file
45
+ has_attached_file :file, :path => ":class/:attachment/:id/:basename.:extension"
46
+ validates :file, :attachment_presence => true
47
+ validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
48
+ end
49
+ ```
50
+
51
+ * show image in your view
52
+
53
+ ```erb
54
+ <%= qiniu_image_tag @image.file.url, :thumbnail => '300x300>', :quality => 80 %>
55
+ or
56
+ <%= image_tag qiniu_image_path(@image.file.url, :thumbnail => '300x300>', :quality => 80) %>
57
+ ```
58
+
59
+ support options: `thumbnail`, `gravity`, `crop`, `quality`, `rotate`, `format`, `auto_orient`. for more information on these options, check the document: http://docs.qiniutek.com/v3/api/foimg/#fo-imageMogr
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
68
+
69
+ ## CHANGELOG
70
+
71
+ ### 0.1.1 (2015-01-06)
72
+
73
+ * upgrade Ruby SDK from `qiniu-rs` to `6.4.1`
74
+
75
+ ### 0.1.0 (2012-09-06)
76
+
77
+ * add view helper: qiniu_image_tag, qiniu_image_path
78
+ * update demo code, style is no longer needed.
79
+
80
+ ### 0.0.3 (2012-09-06)
81
+
82
+ * support qiniu api v3.
83
+
84
+ ### 0.0.2 (2012-07-17)
85
+
86
+ * support public host.
87
+
88
+ ### 0.0.1
89
+
90
+ * it works.
91
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
data/lib/.DS_Store ADDED
Binary file
Binary file
Binary file
@@ -0,0 +1,34 @@
1
+ module Paperclip
2
+ module Qiniu
3
+ module ActionViewExtensions
4
+ module QiniuImagePath
5
+ def qiniu_image_path(source, options={})
6
+ options = options.clone
7
+ thumbnail = options.delete(:thumbnail)
8
+ gravity = options.delete(:gravity)
9
+ crop = options.delete(:crop)
10
+ quality = options.delete(:quality)
11
+ rotate = options.delete(:rotate)
12
+ format = options.delete(:format)
13
+ auto_orient = options.delete(:auto_orient)
14
+ res = source
15
+ res += "?imageMogr"
16
+ res += "/thumbnail/#{CGI.escape thumbnail}" if thumbnail
17
+ res += "/gravity/#{CGI.escape gravity}" if gravity
18
+ res += "/crop/#{CGI.escape crop}" if crop
19
+ res += "/quality/#{CGI.escape quality.to_s}" if quality
20
+ res += "/rotate/#{CGI.escape rotate.to_s}" if rotate
21
+ res += "/format/#{CGI.escape format.to_s}" if format
22
+ res += "/auto-orient" if auto_orient
23
+ if res.end_with? '?imageMogr'
24
+ source
25
+ else
26
+ res
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ ActionView::Base.send :include, Paperclip::Qiniu::ActionViewExtensions::QiniuImagePath
@@ -0,0 +1,31 @@
1
+ module Paperclip
2
+ module Qiniu
3
+ module ActionViewExtensions
4
+ module QiniuImageTag
5
+ def qiniu_image_tag(source, options={})
6
+ options.symbolize_keys!
7
+
8
+ src = path_to_image(source)
9
+ options[:src] = qiniu_image_path(src, options)
10
+
11
+ unless src =~ /^(?:cid|data):/ || src.blank?
12
+ options[:alt] = options.fetch(:alt){ image_alt(src) }
13
+ end
14
+
15
+ if size = options.delete(:size)
16
+ options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
17
+ end
18
+
19
+ if mouseover = options.delete(:mouseover)
20
+ options[:onmouseover] = "this.src='#{path_to_image(mouseover)}'"
21
+ options[:onmouseout] = "this.src='#{options[:src]}'"
22
+ end
23
+
24
+ tag("img", options)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ ActionView::Base.send :include, Paperclip::Qiniu::ActionViewExtensions::QiniuImageTag
@@ -0,0 +1 @@
1
+ require 'paperclip-qiniu'
Binary file
@@ -0,0 +1,109 @@
1
+ module Paperclip
2
+ module Storage
3
+ module Qin
4
+ def self.extended base
5
+ begin
6
+ require 'qiniu'
7
+ rescue LoadError => e
8
+ e.message << " (You may need to install the qiniu gem)#{Qiniu}"
9
+ raise e
10
+ end
11
+
12
+ base.instance_eval do
13
+ unless @options[:url].to_s.match(/^:fog.*url$/)
14
+ @options[:path] = @options[:path].gsub(/:url/, @options[:url])
15
+ @options[:url] = ':qiniu_public_url'
16
+ end
17
+ Paperclip.interpolates(:qiniu_public_url) do |attachment, style|
18
+ attachment.public_url(style)
19
+ end unless Paperclip::Interpolations.respond_to? :qiniu_public_url
20
+ end
21
+
22
+ end
23
+
24
+ def exists?(style = default_style)
25
+ init
26
+ !!Qiniu::Storage.stat(bucket,path(style))
27
+ end
28
+
29
+ def flush_writes
30
+ init
31
+ for style, file in @queued_for_write do
32
+ log("saving #{path(style)}")
33
+ retried = false
34
+ begin
35
+ upload(file, path(style))
36
+ ensure
37
+ file.rewind
38
+ end
39
+ end
40
+
41
+ after_flush_writes # allows attachment to clean up temp files
42
+
43
+ @queued_for_write = {}
44
+ end
45
+
46
+ def flush_deletes
47
+ init
48
+ for path in @queued_for_delete do
49
+ Qiniu::Storage.delete(bucket, path)
50
+ end
51
+ @queued_for_delete = []
52
+ end
53
+
54
+ def public_url(style = default_style)
55
+ init
56
+ if @options[:qiniu_host]
57
+ "#{@public_url}"
58
+ else
59
+ code, result, response_headers = Qiniu::Storage.stat(bucket, path(style))
60
+ if code==200
61
+ "http://orh1veryp.bkt.clouddn.com/#{path(style)}"
62
+ else
63
+ nil
64
+ end
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def init
71
+ return if @inited
72
+ Qiniu.establish_connection! access_key: 'POaiuPPUJN9cxtakBp3Iar9Ur9WidG2PyHYtIGk9',
73
+ secret_key: 'JcGKzcLcOg2IU_KW4RilS_g5LKESS5TSXjp5YH_G'
74
+ inited = true
75
+ end
76
+
77
+ def upload(file, path)
78
+ init
79
+ put_policy = Qiniu::Auth::PutPolicy.new(
80
+ bucket, # 存储空间
81
+ path, # 指定上传的资源名,如果传入 nil,就表示不指定资源名,将使用默认的资源名
82
+ 3600 # token 过期时间,默认为 3600 秒,即 1 小时
83
+ )
84
+ uptoken = Qiniu::Auth.generate_uptoken(put_policy)
85
+ code, result, response_headers = Qiniu::Storage.upload_with_token_2(
86
+ uptoken,
87
+ file.path,
88
+ path,
89
+ nil,
90
+ bucket: bucket
91
+ )
92
+
93
+ @public_url= "http://orh1veryp.bkt.clouddn.com/#{path}"
94
+ end
95
+
96
+ def bucket
97
+ @options[:bucket]
98
+ end
99
+
100
+ def dynamic_fog_host_for_style(style)
101
+ if @options[:qiniu_host].respond_to?(:call)
102
+ @options[:qiniu_host].call(self)
103
+ else
104
+ @options[:qiniu_host]
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,6 @@
1
+ module Paperclip
2
+ module Qiniu
3
+ class Error < StandardError; end
4
+ class UploadFailed < Error; end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Paperclip
2
+ module Qiniu
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require "paperclip-qiniu/version"
2
+ require 'paperclip/storage/qin'
3
+ require 'paperclip/qiniu/action_view_extensions/qiniu_image_path' if defined?(ActionView)
4
+ require 'paperclip/qiniu/action_view_extensions/qiniu_image_tag' if defined?(ActionView)
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/paperclip-qiniu/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["gyzclw"]
6
+ gem.email = ["gyzclw@hotmail.com"]
7
+ gem.description = %q{new paperclip plugin for qiniu}
8
+ gem.summary = %q{new paperclip plugin for qiniu}
9
+ gem.homepage = "https://github.com/gyzclw/paperclip-new-qiniu"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "paperclip-new-qiniu"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Paperclip::Qiniu::VERSION
17
+ gem.add_dependency "paperclip",'~> 5.0', '>= 5.0.0'
18
+ gem.add_dependency 'qiniu', '~> 6.9.0', '>= 6.9.0'
19
+ gem.add_development_dependency 'rspec'
20
+ end
data/spec/.DS_Store ADDED
Binary file
Binary file
Binary file
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'paperclip/storage/qiniu'
3
+
4
+ module Paperclip::Storage
5
+ describe Qiniu do
6
+ context 'exists?' do
7
+ pending "it's hard to test paperclip plugin"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-new-qiniu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gyzclw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: qiniu
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 6.9.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 6.9.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 6.9.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 6.9.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ description: new paperclip plugin for qiniu
68
+ email:
69
+ - gyzclw@hotmail.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - ".DS_Store"
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/.DS_Store
83
+ - lib/paperclip-qiniu.rb
84
+ - lib/paperclip-qiniu/exceptions.rb
85
+ - lib/paperclip-qiniu/version.rb
86
+ - lib/paperclip/.DS_Store
87
+ - lib/paperclip/qiniu.rb
88
+ - lib/paperclip/qiniu/.DS_Store
89
+ - lib/paperclip/qiniu/action_view_extensions/qiniu_image_path.rb
90
+ - lib/paperclip/qiniu/action_view_extensions/qiniu_image_tag.rb
91
+ - lib/paperclip/storage/.DS_Store
92
+ - lib/paperclip/storage/qin.rb
93
+ - paperclip-qiniu.gemspec
94
+ - spec/.DS_Store
95
+ - spec/lib/.DS_Store
96
+ - spec/lib/paperclip/.DS_Store
97
+ - spec/lib/paperclip/storage/qiniu_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: https://github.com/gyzclw/paperclip-new-qiniu
100
+ licenses: []
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.6.11
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: new paperclip plugin for qiniu
122
+ test_files:
123
+ - spec/.DS_Store
124
+ - spec/lib/.DS_Store
125
+ - spec/lib/paperclip/.DS_Store
126
+ - spec/lib/paperclip/storage/qiniu_spec.rb
127
+ - spec/spec_helper.rb