upyun 0.7.0 → 0.7.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in upyun.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 lg2046
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Upyun
2
+
3
+ 又拍云 ruby api
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'upyun'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install upyun
18
+
19
+ ## Usage
20
+
21
+ up_client = UpYun::Bucket.new("by-test-upload", "ichihuo", "********")
22
+ res = up_client.writeFile("/test.png", File.new("test.png"))
23
+ res['x-upyun-width']
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,111 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require "base64"
4
+ require 'digest/md5'
5
+
6
+ module Upyun
7
+ class Bucket
8
+ attr_accessor :bucketname, :username, :password
9
+ attr_accessor :api_domain, :api_form_secret
10
+
11
+ def initialize(bucketname, username, password, options = {})
12
+ options = { :api_domain => "v0.api.upyun.com", :api_form_secret => "" }.merge(options)
13
+ @bucketname = bucketname
14
+ @username = username
15
+ @bpwd = password
16
+ @password = Digest::MD5.hexdigest(password)
17
+ @api_domain = options[:api_domain]
18
+ @api_form_secret = options[:api_form_secret]
19
+ end
20
+
21
+ def write_file(filepath, fd, mkdir='true')
22
+ url = "http://#{api_domain}/#{bucketname}#{filepath}"
23
+ uri = URI.parse(URI.encode(url))
24
+
25
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
26
+ date = get_gmt_date
27
+ length = File.size(fd)
28
+ method = 'PUT'
29
+ headers = {
30
+ 'Date' => date,
31
+ 'Content-Length' => length.to_s,
32
+ 'Authorization' => sign(method, get_gmt_date, "/#{@bucketname}#{filepath}", length),
33
+ 'mkdir' => mkdir
34
+ }
35
+
36
+ http.send_request(method, uri.request_uri, fd.read, headers)
37
+ end
38
+ response.body
39
+ end
40
+
41
+ def check_space
42
+ url = "http://#{api_domain}/#{bucketname}/?usage"
43
+ uri = URI.parse(URI.encode(url))
44
+ req = Net::HTTP::Get.new(url)
45
+ req.basic_auth @username, @bpwd
46
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
47
+ http.request(req)
48
+ end
49
+ response.body
50
+ end
51
+
52
+ def checkout(file)
53
+ url = "http://#{api_domain}/#{bucketname}/#{file}"
54
+ uri = URI.parse(URI.encode(url))
55
+ req = Net::HTTP::Get.new(url)
56
+ req.basic_auth @username, @bpwd
57
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
58
+ http.request(req)
59
+ end
60
+ response.body
61
+ end
62
+
63
+ # 生成api使用的policy 以及 signature 可以是图片或者是文件附件 图片最大为1M 文件附件最大为5M
64
+ def api_form_params(file_type = "pic", notify_url = "", return_url = "", expire_date = 1.days)
65
+ policy_doc = {
66
+ "bucket" => bucketname,
67
+ "expiration" => (DateTime.now + expire_date).to_i,
68
+ "save-key" => "/{year}/{mon}/{random}{.suffix}",
69
+ "notify-url" => notify_url,
70
+ "return-url" => return_url
71
+ }
72
+
73
+ policy_doc = policy_doc.merge({"allow-file-type" => "jpg,jpeg,gif,png", "content-length-range" => "0,1048576"}) if file_type == "pic"
74
+ policy_doc = policy_doc.merge({"allow-file-type" => "doc docx xls xlsx ppt txt zip rar", "content-length-range" => "0,5242880"}) if file_type == "file"
75
+
76
+ policy = Base64.encode64(policy_doc.to_json).gsub("\n", "").strip
77
+ signature = Digest::MD5.hexdigest(policy + "&" + api_form_secret)
78
+
79
+ {:policy => policy, :signature => signature}
80
+ end
81
+
82
+ def parse_notify_params(params)
83
+ params = params.with_indifferent_access
84
+ if params[:code].to_s == "200" && (params[:sign] == Digest::MD5.hexdigest("#{params[:code]}&#{params[:message]}&#{params[:url]}&#{params[:time]}&#{api_form_secret}"))
85
+ url = "http://#{bucketname}.b0.upaiyun.com#{params[:url]}"
86
+ mid = Digest::MD5.hexdigest(url)
87
+ image_attributes = { :mid => mid, :url => url }
88
+
89
+ if params["image-width"] && params["image-height"] && params["image-frames"] && params["image-type"]
90
+ image_attributes[:width] = params["image-width"].to_i
91
+ image_attributes[:height] = params["image-height"].to_i
92
+ image_attributes[:frames] = params["image-frames"].to_i
93
+ image_attributes[:file_type] = params["image-type"]
94
+ end
95
+ image_attributes
96
+ else
97
+ nil
98
+ end
99
+ end
100
+
101
+ private
102
+ def get_gmt_date
103
+ DateTime.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
104
+ end
105
+
106
+ def sign(method, date, url, length)
107
+ sign = "#{method}&#{url}&#{date}&#{length}&#{password}"
108
+ "UpYun #{@username}:#{Digest::MD5.hexdigest(sign)}"
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,3 @@
1
+ module Upyun
2
+ VERSION = "0.7.1"
3
+ end
data/lib/upyun.rb ADDED
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/upyun/version")
2
+ require File.expand_path(File.dirname(__FILE__) + "/upyun/bucket")
3
+
4
+
5
+ module Upyun
6
+ def self.get_config_for_bucket(bucket)
7
+ UP_BUCKETS.find {|k, v| v.bucket_name.to_s == bucket.to_s}.try(:last)
8
+ end
9
+
10
+ # 为给定的路径与文件名生成保存后的远程路径
11
+ def self.rand_save_path(filename)
12
+ "/#{Date.today.year}/#{'%2s' % Date.today.month.to_s.rjust(2, "0")}/#{RandomCode.mix_string(16).downcase}#{File.basename(filename)}"
13
+ end
14
+ end
data/upyun.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/upyun/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["veggie"]
6
+ gem.email = ["kkxlkkxllb@gmail.com"]
7
+ gem.description = %q{又拍云存储ruby api}
8
+ gem.summary = %q{又拍云存储ruby api}
9
+ gem.homepage = "https://github.com/kkxlkkxllb/upyun"
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 = "upyun"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Upyun::VERSION
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,8 +17,17 @@ email:
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
- files: []
21
- homepage: ''
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/upyun.rb
27
+ - lib/upyun/bucket.rb
28
+ - lib/upyun/version.rb
29
+ - upyun.gemspec
30
+ homepage: https://github.com/kkxlkkxllb/upyun
22
31
  licenses: []
23
32
  post_install_message:
24
33
  rdoc_options: []