class SprocketsCDN::Providers::Upyun

Attributes

password[RW]
username[RW]

Public Class Methods

new(opt) click to toggle source
Calls superclass method SprocketsCDN::Providers::Base::new
# File lib/sprockets_cdn/providers/upyun.rb, line 8
def initialize opt
  super
  @username = opt[:username] || @access_key
  @password = md5(opt[:password] || @secret_key)
end

Public Instance Methods

base_url() click to toggle source
# File lib/sprockets_cdn/providers/upyun.rb, line 40
def base_url
  "http://v0.api.upyun.com"
end
fullpath(key) click to toggle source
# File lib/sprockets_cdn/providers/upyun.rb, line 44
def fullpath key
  File.join("/#{@bucket}", key).to_s
end
md5(content) click to toggle source
# File lib/sprockets_cdn/providers/upyun.rb, line 48
def md5 content
  Digest::MD5.hexdigest content
end
post_url(key) click to toggle source
# File lib/sprockets_cdn/providers/upyun.rb, line 36
def post_url key
  "http://v0.api.upyun.com/#{@bucket}/" + key #).to_s
end
sign(method, date, path, length) click to toggle source
# File lib/sprockets_cdn/providers/upyun.rb, line 52
def sign(method, date, path, length)
  sign = "#{method.to_s.upcase}&#{path}&#{date}&#{length}&#{@password}"
  "UpYun #{@username}:#{md5(sign)}"
end
uploading(file, opt) click to toggle source
# File lib/sprockets_cdn/providers/upyun.rb, line 14
def uploading file, opt
  headers = {mkdir: true}

  key = get_key file, opt[:dir]
  body = File.new(file, 'rb')
  post_data = {
    body: body
  }
  date = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')

  sign = sign(:put, date, fullpath(key), body.size)
  headers[:Date] = date
  headers[:Authorization] = sign
  res = RestClient.put post_url(key), body, headers
  if res.code == 200
    URI.join(@asset_host, key).to_s
  end
  #  target = JSON.parse(res)['key']
  # URI.join(@asset_host, target).to_s
  generate_remote_url key
end