bbs_uploader 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8677050c417b473757a3652b759ebe4908f20e73
4
- data.tar.gz: 9e90d6441b50fbed226a2887bbdc765a3fa6ad41
3
+ metadata.gz: 5a6493e66e376c7fc95de0b042ad07fed9513589
4
+ data.tar.gz: cba63d2d2124b7523c985cff4135014e389b7f25
5
5
  SHA512:
6
- metadata.gz: 73fa29aaa3e03b83d196b06391368652b9b3d5a40f3e32d080d46dc6235d10d6066403a1c3e071eb8b3a3bff53f50f5cb36070c2d2851abf7756880249af33f8
7
- data.tar.gz: 50f4cedcd2d7ddc248c31b8144f45d66e5eda9f34c139df3938d0ec919b7374fa121c1fb1e9f66d69d960173079c83514f6d894583b2ba44326b688ceaa70374
6
+ metadata.gz: 71c1a42c39aad4e4a362fb5a2589be96ea6dd3f78e1afc4e0562facc8f62d7f522428947b4c46550974c4b77472a4f471be8ec3242ca9568a28224cd96047b8a
7
+ data.tar.gz: 23687eb9066ef25c007edf1c39b458d3522ac55f8cc9466b68af871d0549cdabc3fbd1e11fa5c67c56a882c761686ed1f5ca109a563ddc7ccd9f39b7e5f48930
data/README.md CHANGED
@@ -22,8 +22,60 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ **命令行使用:**
25
26
 
27
+ 使用指南:
26
28
 
29
+ ```
30
+ Usage: bbs_uploader [options]
31
+ -i, --input-file [file] Input file can be URL, image file or other file
32
+ -h, --help Show this message
33
+ -v, --version Print version
34
+ ```
35
+
36
+ 调用实例:
37
+
38
+ ```
39
+ $ bbs_uploader ~/Downloads/test.jpg
40
+ D, [2016-11-15T16:31:52.906672 #30130] DEBUG -- : Query bss-image hosts Success: {"ttl"=>86400, "http"=>{"io"=>["http://iovip.qbox.me"], "up"=>["http://up.qiniu.com", "http://upload.qiniu.com", "-H up.qiniu.com http://183.136.139.16"]}, "https"=>{"io"=>["https://iovip.qbox.me"], "up"=>["https://up.qbox.me"]}}
41
+ 图片上传成功!
42
+ 链接为: http://ognvcf5x6.bkt.clouddn.com/bbs_image/test.jpg
43
+ markdown 链接: ![](http://ognvcf5x6.bkt.clouddn.com/bbs_image/test.jpg)
44
+
45
+ $ bin/bbs_uploader http://xiajian.github.io/assets/images/dongxiang.png
46
+ 下载文件的地址: http://xiajian.github.io/assets/images/dongxiang.png
47
+ /tmp/dongxiang.png
48
+ D, [2016-11-21T09:57:43.758140 #49294] DEBUG -- : Query bss-image hosts Success: {"ttl"=>86400, "http"=>{"io"=>["http://iovip.qbox.me"], "up"=>["http://up.qiniu.com", "http://upload.qiniu.com", "-H up.qiniu.com http://183.136.139.16"]}, "https"=>{"io"=>["https://iovip.qbox.me"], "up"=>["https://up.qbox.me"]}}
49
+ 上传成功!
50
+ 链接为: http://ognvcf5x6.bkt.clouddn.com/bbs_image/dongxiang.png
51
+
52
+ markdown 链接: ![](http://ognvcf5x6.bkt.clouddn.com/bbs_image/dongxiang.png)
53
+ ```
54
+
55
+ ![](http://ognvcf5x6.bkt.clouddn.com/bbs_image/test.jpg)
56
+
57
+ **编程使用:**
58
+
59
+ ```
60
+ BbsUploader.qiniu = {
61
+ access_key: 'your access key',
62
+ secret_key: 'your secret key',
63
+ bucket: "your bucket",
64
+ bucket_domain: "your application domain"
65
+ }
66
+
67
+ # 超集方法:可以上传任意的文件
68
+ BbsUploader.upload '~/Downloads/test.jpg'
69
+ BbsUploader.upload 'http://xiajian.github.io/assets/images/dongxiang.png'
70
+
71
+ BbsUploader.upload_image '~/Downloads/test.jpg'
72
+ BbsUploader.upload_file '~/Downloads/install.php'
73
+
74
+ ```
75
+
76
+ 然后,就祝你安好!!!
77
+
78
+ > PS: 思考 网盘以及 CDN 在部署的上区别。 CDN 内容分发,网盘就是云存储。 这就不明白,SB!!
27
79
 
28
80
  ## Development
29
81
 
@@ -33,10 +85,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
33
85
 
34
86
  ## Contributing
35
87
 
36
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bbs_uploader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xiajian/bbs_uploader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
37
89
 
38
90
 
39
91
  ## License
40
92
 
41
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
-
93
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/bin/bbs_uploader CHANGED
@@ -21,12 +21,7 @@ OptionParser.new do |opts|
21
21
  exit
22
22
  end
23
23
 
24
- opts.on('-i', '--input-file [file]', 'Default: xxx.jpg') do |file|
25
- unless File.exist? file
26
- puts "输入的文件 #{file} 不存在,请检查后重试"
27
- exit
28
- end
29
-
24
+ opts.on('-i', '--input-file [file]', 'Input file can be URL, image file or other file') do |file|
30
25
  options[:input_file] = File.absolute_path file
31
26
  end
32
27
  end.parse!
@@ -34,15 +29,12 @@ end.parse!
34
29
  if options[:input_file].nil?
35
30
  file = ARGV[0]
36
31
 
37
- if !file.nil? && File.exists?(file)
38
- options[:input_file] = File.absolute_path file
39
- elsif !file.nil?
40
- puts "输入的文件 #{ARGV[0]} 不存在,请检查后重试"
41
- exit
42
- elsif file.nil?
43
- puts "没有选择上传的图片"
44
- exit
32
+ if file.nil?
33
+ puts "没有选择上传的文件"
34
+ exit
45
35
  end
36
+
37
+ options[:input_file] = file
46
38
  end
47
39
 
48
- BbsUploader.upload_image options[:input_file]
40
+ BbsUploader.upload options[:input_file]
@@ -0,0 +1,78 @@
1
+ # Copy from https://github.com/eparreno/ruby_regex/blob/master/lib/ruby_regex.rb
2
+ # All Rights belong to author Emili Parreno(https://github.com/eparreno)
3
+ module BbsUploader
4
+ module RubyRegex
5
+ # Username
6
+ # This regular expression doesn't validate username's length
7
+ Username = /\A[a-zA-Z0-9_]*\z/
8
+
9
+ # Dni (spanish ID card)
10
+ Dni = /\A\d{8}[A-Za-z]{1}\z/
11
+
12
+ # URL
13
+ Url = URL = /(\A\z)|(\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?\z)/ix
14
+
15
+ # Domain
16
+ Domain = /(\A\z)|(\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?)?\z)/ix
17
+
18
+ # CreditCard
19
+ # Validates Credit Card numbers, Checks if it contains 16 numbers in groups of 4 separated by -, space or nothing
20
+ CreditCard = /\A(\d{4}-){3}\d{4}\z|\A(\d{4}\s){3}\d{4}\z|\A\d{16}\z/
21
+
22
+ # MasterCard credit card
23
+ MasterCard = /\A5[1-5]\d{14}\z/
24
+
25
+ # Visa credit card
26
+ Visa = /\A4\d{15}\z/
27
+
28
+ # US Social Security
29
+ USSocialSecurity = /\A\d{3}-\d{2}-\d{4}\z/
30
+
31
+ # General postal code
32
+ # Validates a 5 digits postal code
33
+ GeneralPostalCode = /\A\d{5}\z/
34
+
35
+ # US ZIP code
36
+ # Validates US ZIP Code (basic and extended format)
37
+ ZIPCode = /\A(\d{5}\z)|(\d{5}-\d{4}\z)/
38
+
39
+ # Twitter username
40
+ TwitterUsername = /\A([a-z0-9\_])+\z/ix
41
+
42
+ # Github username
43
+ GithubUsername = /\A([a-z0-9\_\-])+\z/ix
44
+
45
+ # Slideshare username
46
+ SlideshareUsername = /\A([a-z0-9])+\z/ix
47
+
48
+ # Del.icio.us username
49
+ DeliciousUsername = /\A([a-z0-9\_\-])+\z/ix
50
+
51
+ # Email
52
+ # From the email regex research: http://fightingforalostcause.net/misc/2006/compare-email-regex.php
53
+ # Authors: James Watts and Francisco Jose Martin Moreno
54
+ Email = /\A([\w\!\#\z\%\&\'\*\+\-\/\=\?\\A\`{\|\}\~]+\.)*[\w\+-]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)\z/i
55
+
56
+ # UUID
57
+ # Validates a UUID as defined: http://en.wikipedia.org/wiki/Universally_unique_identifier
58
+ UUID = /\A([0-9a-fA-F]{32}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\z/
59
+
60
+ # Date DB format YYYY-MM-DD
61
+ # I know it will validate 2001-02-31 but is I think we are focusing in formats more than in parsing
62
+ DBDate = /\A\d{4}-(#{("01".."12").to_a.join("|")})-(#{("01".."31").to_a.join("|")})\z/
63
+
64
+ # Date Time DB format YYYY-MM-DD hh:mm:ss
65
+ DBDateTime = /\A\d{4}-(#{("01".."12").to_a.join("|")})-(#{("01".."31").to_a.join("|")})\s(#{("00".."23").to_a.join("|")}):(#{("00".."59").to_a.join("|")}):(#{("00".."59").to_a.join("|")})\z/
66
+
67
+ # SpanishBankAccountNumber
68
+ SpanishBankAccountNumber = /\A\d{4}[ -]?\d{4}[ -]?\d{2}[ -]?\d{10}\z/
69
+
70
+ # IBAN
71
+ # Source: http://snipplr.com/view/15322/iban-regex-all-ibans/
72
+ # You have to remove spaces or any separator character from the original field before use this regex
73
+ IBAN = /[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}/
74
+
75
+ # MacAddress
76
+ MacAddress = /\A([0-9A-F]{2}[:-]){5}([0-9A-F]{2})\z/i
77
+ end
78
+ end
@@ -1,3 +1,3 @@
1
1
  module BbsUploader
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/bbs_uploader.rb CHANGED
@@ -1,10 +1,20 @@
1
1
  require "qiniu"
2
+ require "open-uri" # 下载文件
2
3
  require "bbs_uploader/version"
4
+ require "bbs_uploader/ruby_regex"
3
5
 
4
6
  module BbsUploader
5
7
  # Your code goes here...
6
8
  IMAGE_URL_REGEXP = /http:\/\/.*[jpg|png|jpeg]$/
7
9
 
10
+ IMAGE_FILE_REGEXP = /.*[jpg|png|jpeg|gif|bmp]$/
11
+
12
+ module FileType
13
+ IMAGE = 'image'.freeze
14
+
15
+ FILE = 'file'.freeze
16
+ end
17
+
8
18
  class << self
9
19
  @@qiniu = {}
10
20
 
@@ -26,12 +36,48 @@ module BbsUploader
26
36
  }.merge(options)
27
37
  end
28
38
 
29
- def upload_image(file_path)
30
- # 构建鉴权对象
39
+ # 下载文件 - 依赖本地的 tmp 目录, 需要 `open-uri`
40
+ #
41
+ # 参考: http://stackoverflow.com/questions/2263540/how-do-i-download-a-binary-file-over-http
42
+ #
43
+ # @param {String} url - 网络上的 URL 地址
44
+ # @param {String} file_path - 下载指定的文件路径地址
45
+ #
46
+ # @return {String} file_path - 文件在当前系统的中的路径
47
+ def download(url, file_path = '')
48
+ puts "下载文件的地址: #{url}"
49
+
50
+ if file_path == ''.freeze
51
+ file_path = "/tmp/#{url.scan(/.*\/([^\/]*)/).flatten[0]}"
52
+ end
53
+
54
+ if File.exist? file_path
55
+ `rm -rf #{file_path}`
56
+ end
57
+
58
+ File.open(file_path, 'wb') do |saved_file|
59
+ open(url, 'rb') do |read_file|
60
+ saved_file.write(read_file.read)
61
+ end
62
+ end
63
+
64
+ file_path
65
+ rescue => e
66
+ puts "下载文件发生异常: #{e}"
67
+
68
+ ''
69
+ end
70
+
71
+ def upload_file(file_path, file_type = FileType::FILE)
31
72
  Qiniu.establish_connection! access_key: self.qiniu[:access_key],
32
73
  secret_key: self.qiniu[:secret_key]
33
74
 
34
- key = "bbs_image/#{File.basename(file_path)}"
75
+ key = if file_type == FileType::FILE
76
+ "bbs_file/#{File.basename(file_path)}"
77
+ elsif file_type == FileType::IMAGE
78
+ "bbs_image/#{File.basename(file_path)}"
79
+ end
80
+
35
81
  put_policy = Qiniu::Auth::PutPolicy.new(
36
82
  self.qiniu[:bucket], # 存储空间
37
83
  key, # 指定上传的资源名,如果传入 nil,就表示不指定资源名,将使用默认的资源名
@@ -50,22 +96,54 @@ module BbsUploader
50
96
  )
51
97
 
52
98
  if code == 200
53
- image_url = "http://#{self.qiniu[:bucket_domain]}/#{result['key']}"
99
+ file_url = "http://#{self.qiniu[:bucket_domain]}/#{result['key']}"
54
100
 
55
- puts "图片上传成功! \n链接为: #{image_url} \nmarkdown 链接: #{markdown_image_link(image_url)}"
101
+ puts "上传成功! \n链接为: #{file_url}"
56
102
 
57
- image_url
103
+ file_url
58
104
  else
59
- puts '上传图片失败'
105
+ puts '上传文件失败'
60
106
 
61
107
  nil
62
108
  end
63
109
  rescue => e
64
- puts "上传图片发生异常: #{e}"
110
+ puts "上传发生异常: #{e}"
65
111
 
66
112
  nil
67
113
  end
68
114
 
115
+ def upload_image(file_path)
116
+ if file_path =~ IMAGE_FILE_REGEXP
117
+ image_url = upload_file file_path, FileType::IMAGE
118
+
119
+ puts "markdown 链接: #{markdown_image_link(image_url)}"
120
+
121
+ image_url
122
+ else
123
+ puts '不是图片文件,请选择重新选择!!'
124
+ end
125
+ end
126
+
127
+ def upload(resource = '')
128
+ if resource =~ RubyRegex::URL
129
+ file_path = download resource
130
+
131
+ if file_path =~ IMAGE_FILE_REGEXP
132
+ upload_image file_path
133
+ end
134
+ elsif File.exist? resource
135
+ file_path = File.absolute_path(resource)
136
+
137
+ if resource =~ IMAGE_FILE_REGEXP
138
+ upload_image file_path
139
+ else
140
+ upload_file file_path
141
+ end
142
+ else
143
+ puts "输入的文件 #{resource} 不存在,请检查后重试"
144
+ end
145
+ end
146
+
69
147
  def markdown_image_link(image_url)
70
148
  if image_url && image_url.is_a?(String) && IMAGE_URL_REGEXP.match(image_url)
71
149
  "![](#{image_url})"
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - xiajian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-15 00:00:00.000000000 Z
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: qiniu
@@ -72,6 +72,7 @@ files:
72
72
  - bin/console
73
73
  - bin/setup
74
74
  - lib/bbs_uploader.rb
75
+ - lib/bbs_uploader/ruby_regex.rb
75
76
  - lib/bbs_uploader/version.rb
76
77
  homepage: https://github.com/xiajian/bbs_uploader
77
78
  licenses: