bbs_uploader 0.1.5 → 0.1.6
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/README.md +11 -0
- data/lib/bbs_uploader.rb +50 -25
- data/lib/bbs_uploader/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15e274d18ec55de0c89e135fdd64961d1e943762
|
4
|
+
data.tar.gz: 872be5c72d36b8b9715db4c16662898723b24955
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
data/lib/bbs_uploader.rb
CHANGED
@@ -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:\/\/.*
|
9
|
+
IMAGE_URL_REGEXP = /http:\/\/.*(jpg|png|jpeg)$/
|
10
10
|
|
11
|
-
IMAGE_FILE_REGEXP = /.*
|
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
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
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
|
data/lib/bbs_uploader/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2016-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: qiniu
|