module BbsUploader
Copy from github.com/eparreno/ruby_regex/blob/master/lib/ruby_regex.rb All Rights belong to author Emili Parreno(github.com/eparreno)
Constants
- IMAGE_FILE_REGEXP
- IMAGE_URL_REGEXP
Your code goes here…
- VERSION
Public Class Methods
command(options = {})
click to toggle source
# File lib/bbs_uploader.rb, line 209 def command(options = {}) if options[:input_file] upload options[:input_file] elsif options[:input_directory] generate_markdown_format options[:input_directory] end end
default_options(options = {})
click to toggle source
# File lib/bbs_uploader.rb, line 35 def default_options(options = {}) options = Hashie::Mash.new(options) Hashie::Mash.new({ access_key: "79WvzAxvV-nOEX7m0PERzwR0Lhm-FDHriz2-QdAN", secret_key: "4wwnWNU16n9uVK8DHRW6qO61b2gls3aSduHswkvc", bucket: "bss-image", bucket_domain: "ognvcf5x6.bkt.clouddn.com" }).merge(options) end
download(url, file_path = '')
click to toggle source
下载文件 - 依赖本地的 tmp 目录, 需要 `open-uri`
参考: stackoverflow.com/questions/2263540/how-do-i-download-a-binary-file-over-http
@param {String} url - 网络上的 URL 地址 @param {String} file_path - 下载指定的文件路径地址
@return {String} file_path - 文件在当前系统的中的路径
# 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)
click to toggle source
根据目录下指定目录下的文件,将文件上传到七牛上
@note 注意文件需要按照文件目录的自然顺序
# 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)
click to toggle source
返回文件的状态码
# 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)
click to toggle source
# 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) "" else image_url end end
qiniu()
click to toggle source
# File lib/bbs_uploader.rb, line 31 def qiniu @@qiniu != {} ? @@qiniu : default_options end
qiniu=(options)
click to toggle source
初始化全局变量 @@qiniu
# File lib/bbs_uploader.rb, line 27 def qiniu=(options) @@qiniu = default_options(options) end
upload(resource = '')
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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 = '')
click to toggle source
# 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
Protected Class Methods
establish_connection()
click to toggle source
# File lib/bbs_uploader.rb, line 219 def establish_connection Qiniu.establish_connection! access_key: self.qiniu[:access_key], secret_key: self.qiniu[:secret_key] end