Module: BbsUploader
- Defined in:
- lib/bbs_uploader/ruby_regex.rb,
lib/bbs_uploader.rb,
lib/bbs_uploader/version.rb
Overview
Copy from github.com/eparreno/ruby_regex/blob/master/lib/ruby_regex.rb All Rights belong to author Emili Parreno(github.com/eparreno)
Defined Under Namespace
Modules: Code, FileType, RubyRegex
Constant Summary collapse
- IMAGE_URL_REGEXP =
Your code goes here…
/http:\/\/.*(jpg|png|jpeg)$/
- IMAGE_FILE_REGEXP =
/.*(jpg|png|jpeg|gif|bmp)$/
- VERSION =
"0.1.6"
- @@qiniu =
{}
Class Method Summary collapse
- .command(options = {}) ⇒ Object
- .default_options(options = {}) ⇒ Object
-
.download(url, file_path = '') ⇒ String
下载文件 - 依赖本地的 tmp 目录, 需要 ‘open-uri`.
-
.generate_markdown_format(dir) ⇒ Object
根据目录下指定目录下的文件,将文件上传到七牛上.
-
.get_file_info(key) ⇒ Object
返回文件的状态码.
- .markdown_image_link(image_url) ⇒ Object
- .qiniu ⇒ Object
-
.qiniu=(options) ⇒ Object
初始化全局变量 @@qiniu.
- .upload(resource = '') ⇒ Object
- .upload_file(file_path, file_type = FileType::FILE) ⇒ Object
- .upload_image(file_path) ⇒ Object
- .upload_with_directory(dir = '') ⇒ Object
Class Method Details
.command(options = {}) ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/bbs_uploader.rb', line 209 def command( = {}) if [:input_file] upload [:input_file] elsif [:input_directory] generate_markdown_format [:input_directory] end end |
.default_options(options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bbs_uploader.rb', line 35 def ( = {}) = Hashie::Mash.new() Hashie::Mash.new({ access_key: "79WvzAxvV-nOEX7m0PERzwR0Lhm-FDHriz2-QdAN", secret_key: "4wwnWNU16n9uVK8DHRW6qO61b2gls3aSduHswkvc", bucket: "bss-image", bucket_domain: "ognvcf5x6.bkt.clouddn.com" }).merge() end |
.download(url, file_path = '') ⇒ String
下载文件 - 依赖本地的 tmp 目录, 需要 ‘open-uri`
参考: stackoverflow.com/questions/2263540/how-do-i-download-a-binary-file-over-http
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bbs_uploader.rb', line 54 def download(url, file_path = '') puts "下载文件的地址: #{url}" if file_path == ''.freeze file_path = "/tmp/#{url.scan(/.*\/([^\/]*)/).flatten[0]}" end if File.exist? file_path `rm -rf #{file_path}` end File.open(file_path, 'wb') do |saved_file| open(url, 'rb') do |read_file| saved_file.write(read_file.read) end end file_path rescue => e puts "下载文件发生异常: #{e}" '' end |
.generate_markdown_format(dir) ⇒ Object
Note:
注意文件需要按照文件目录的自然顺序
根据目录下指定目录下的文件,将文件上传到七牛上
200 201 202 203 204 205 206 207 |
# File 'lib/bbs_uploader.rb', line 200 def generate_markdown_format(dir) content = upload_with_directory(dir).map { |link| markdown_image_link(link) }.join("\n") puts "上传链接地址: \n" puts content content end |
.get_file_info(key) ⇒ Object
返回文件的状态码
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bbs_uploader.rb', line 79 def get_file_info(key) establish_connection # 获取文件信息 code, result, response_headers = Qiniu::Storage.stat( self.qiniu[:bucket], # 存储空间 key # 资源名 ) code end |
.markdown_image_link(image_url) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/bbs_uploader.rb', line 189 def markdown_image_link(image_url) if image_url && image_url.is_a?(String) && IMAGE_URL_REGEXP.match(image_url) "![](#{image_url})" else image_url end end |
.qiniu ⇒ Object
31 32 33 |
# File 'lib/bbs_uploader.rb', line 31 def qiniu @@qiniu != {} ? @@qiniu : end |
.qiniu=(options) ⇒ Object
初始化全局变量 @@qiniu
27 28 29 |
# File 'lib/bbs_uploader.rb', line 27 def qiniu=() @@qiniu = () end |
.upload(resource = '') ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bbs_uploader.rb', line 152 def upload(resource = '') if resource =~ RubyRegex::URL file_path = download resource if file_path =~ IMAGE_FILE_REGEXP upload_image file_path else upload_file file_path end elsif File.exist? resource file_path = File.absolute_path(resource) if resource =~ IMAGE_FILE_REGEXP upload_image file_path else upload_file file_path end else puts "输入的文件 #{resource} 不存在,请检查后重试" end end |
.upload_file(file_path, file_type = FileType::FILE) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bbs_uploader.rb', line 91 def upload_file(file_path, file_type = FileType::FILE) key = if file_type == FileType::FILE "bbs_file/#{File.basename(file_path)}" elsif file_type == FileType::IMAGE "bbs_image/#{File.basename(file_path)}" end unless (code = get_file_info(key)) == Code::SUCCESS establish_connection put_policy = Qiniu::Auth::PutPolicy.new( self.qiniu[:bucket], # 存储空间 key, # 指定上传的资源名,如果传入 nil,就表示不指定资源名,将使用默认的资源名 3600 # token 过期时间,默认为 3600 秒,即 1 小时 ) uptoken = Qiniu::Auth.generate_uptoken(put_policy) # 调用 upload_with_token_2 方法上传 code, result = Qiniu::Storage.upload_with_token_2( uptoken, file_path, key, nil, # 可以接受一个 Hash 作为自定义变量,请参照 http://developer.qiniu.com/article/kodo/kodo-developer/up/vars.html#xvar bucket: self.qiniu[:bucket] ) end if code == Code::SUCCESS file_url = "http://#{self.qiniu[:bucket_domain]}/#{key}" puts "上传成功! \n链接为: #{file_url}" encode_url = URI.encode file_url puts "编码后的链接: #{encode_url}" encode_url else puts '上传文件失败' nil end rescue => e puts "上传发生异常: #{e}" nil end |
.upload_image(file_path) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/bbs_uploader.rb', line 140 def upload_image(file_path) if file_path =~ IMAGE_FILE_REGEXP image_url = upload_file file_path, FileType::IMAGE puts "markdown 链接: #{markdown_image_link(image_url)}" image_url else puts '不是图片文件,请选择重新选择!!' end end |
.upload_with_directory(dir = '') ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bbs_uploader.rb', line 174 def upload_with_directory(dir = '') dir = `cd #{dir}; pwd`.chomp return unless File.exists?(dir) && File.directory?(dir) puts "上传目录: #{dir}" files = `ls #{dir}`.split("\n").map { |filename| "#{dir}/#{filename}" } file_links = files.map { |file| upload(file) } puts "生成的图片的链接: #{file_links.join('\n')}" file_links end |