bbs_uploader 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16b663d5ba712829ab142fa9ecab043e7d8e77c3
4
- data.tar.gz: 5b2bd2ca3c304aa247298e18c1d8dd56b3ca9ef2
3
+ metadata.gz: 15e274d18ec55de0c89e135fdd64961d1e943762
4
+ data.tar.gz: 872be5c72d36b8b9715db4c16662898723b24955
5
5
  SHA512:
6
- metadata.gz: ed92c7efbbb058a2e8063814311215265d9757eb681e705c0624415c3d2b7fdc96965b3bae7e9ec19a8968bba5c82e4fd2c2637295bd099b2aa9a791d8023609
7
- data.tar.gz: 25e27f4976fa6da8ec0882acb62106925626e5babf308f9fec6d7b6034bfca0b756dc8104f5d6acf6a246c8229ccf0ad24103331f9d523787f69e66262c79e17
6
+ metadata.gz: a16f5c146174f9800a1f71dc8cc768eb9bd6fbf84c0399af1ef57b56a17f70eca1eaeed756307753dc88da55cd2a15279e2ecc9b4f4c597b9e023507f8d94535
7
+ data.tar.gz: 4dcc08008f20839f9baca4e6d8e24f610387e9c2ae35debc24f085ab89dd70da9c284ddd490ab3603d00304002d57a63a7f9246734a5c8d836fcb4b84bc34465
data/README.md CHANGED
@@ -19,6 +19,17 @@ And then execute:
19
19
  Or install it yourself as:
20
20
 
21
21
  $ gem install bbs_uploader
22
+
23
+ 或者单独安装:
24
+
25
+ ```
26
+ \curl -sSL https://get.rvm.io | bash -s stable # 安装 rvm,ubuntu/centos 上的 ruby 版本太老
27
+ source /etc/profile.d/rvm.sh # 运行 rvm.sh 的脚本
28
+ rvm install 2.3.1
29
+ rvm 2.3.1 --default
30
+ gem install bundler
31
+ gem install bbs_uploader # gem 的创建时通过 bundler 创建的
32
+ ```
22
33
 
23
34
  ## Usage
24
35
 
@@ -6,15 +6,19 @@ require "bbs_uploader/ruby_regex"
6
6
 
7
7
  module BbsUploader
8
8
  # Your code goes here...
9
- IMAGE_URL_REGEXP = /http:\/\/.*[jpg|png|jpeg]$/
9
+ IMAGE_URL_REGEXP = /http:\/\/.*(jpg|png|jpeg)$/
10
10
 
11
- IMAGE_FILE_REGEXP = /.*[jpg|png|jpeg|gif|bmp]$/
11
+ IMAGE_FILE_REGEXP = /.*(jpg|png|jpeg|gif|bmp)$/
12
12
 
13
13
  module FileType
14
14
  IMAGE = 'image'.freeze
15
15
 
16
16
  FILE = 'file'.freeze
17
17
  end
18
+
19
+ module Code
20
+ SUCCESS = 200
21
+ end
18
22
 
19
23
  class << self
20
24
  @@qiniu = {}
@@ -71,35 +75,49 @@ module BbsUploader
71
75
  ''
72
76
  end
73
77
 
74
- def upload_file(file_path, file_type = FileType::FILE)
75
- Qiniu.establish_connection! access_key: self.qiniu[:access_key],
76
- secret_key: self.qiniu[:secret_key]
78
+ # 返回文件的状态码
79
+ def get_file_info(key)
80
+ establish_connection
81
+
82
+ # 获取文件信息
83
+ code, result, response_headers = Qiniu::Storage.stat(
84
+ self.qiniu[:bucket], # 存储空间
85
+ key # 资源名
86
+ )
87
+
88
+ code
89
+ end
77
90
 
91
+ def upload_file(file_path, file_type = FileType::FILE)
78
92
  key = if file_type == FileType::FILE
79
93
  "bbs_file/#{File.basename(file_path)}"
80
94
  elsif file_type == FileType::IMAGE
81
95
  "bbs_image/#{File.basename(file_path)}"
82
96
  end
83
-
84
- put_policy = Qiniu::Auth::PutPolicy.new(
85
- self.qiniu[:bucket], # 存储空间
86
- key, # 指定上传的资源名,如果传入 nil,就表示不指定资源名,将使用默认的资源名
87
- 3600 # token 过期时间,默认为 3600 秒,即 1 小时
88
- )
89
-
90
- uptoken = Qiniu::Auth.generate_uptoken(put_policy)
91
-
92
- # 调用 upload_with_token_2 方法上传
93
- code, result = Qiniu::Storage.upload_with_token_2(
94
- uptoken,
95
- file_path,
96
- key,
97
- nil, # 可以接受一个 Hash 作为自定义变量,请参照 http://developer.qiniu.com/article/kodo/kodo-developer/up/vars.html#xvar
98
- bucket: self.qiniu[:bucket]
99
- )
100
-
101
- if code == 200
102
- file_url = "http://#{self.qiniu[:bucket_domain]}/#{result['key']}"
97
+
98
+ unless (code = get_file_info(key)) == Code::SUCCESS
99
+ establish_connection
100
+
101
+ put_policy = Qiniu::Auth::PutPolicy.new(
102
+ self.qiniu[:bucket], # 存储空间
103
+ key, # 指定上传的资源名,如果传入 nil,就表示不指定资源名,将使用默认的资源名
104
+ 3600 # token 过期时间,默认为 3600 秒,即 1 小时
105
+ )
106
+
107
+ uptoken = Qiniu::Auth.generate_uptoken(put_policy)
108
+
109
+ # 调用 upload_with_token_2 方法上传
110
+ code, result = Qiniu::Storage.upload_with_token_2(
111
+ uptoken,
112
+ file_path,
113
+ key,
114
+ nil, # 可以接受一个 Hash 作为自定义变量,请参照 http://developer.qiniu.com/article/kodo/kodo-developer/up/vars.html#xvar
115
+ bucket: self.qiniu[:bucket]
116
+ )
117
+ end
118
+
119
+ if code == Code::SUCCESS
120
+ file_url = "http://#{self.qiniu[:bucket_domain]}/#{key}"
103
121
 
104
122
  puts "上传成功! \n链接为: #{file_url}"
105
123
 
@@ -195,5 +213,12 @@ module BbsUploader
195
213
  generate_markdown_format options[:input_directory]
196
214
  end
197
215
  end
216
+
217
+ protected
218
+
219
+ def establish_connection
220
+ Qiniu.establish_connection! access_key: self.qiniu[:access_key],
221
+ secret_key: self.qiniu[:secret_key]
222
+ end
198
223
  end
199
224
  end
@@ -1,3 +1,3 @@
1
1
  module BbsUploader
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bbs_uploader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - xiajian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-10 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: qiniu