lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-0.4.3 vs lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-0.4.4
- old
+ new
@@ -1,15 +1,15 @@
-# encoding: utf-8
require 'aliyun/oss'
require 'carrierwave'
require 'uri'
module CarrierWave
module Storage
class Aliyun < Abstract
-
class Connection
+ PATH_PREFIX = %r{^/}
+
def initialize(uploader)
@uploader = uploader
@aliyun_access_id = uploader.aliyun_access_id
@aliyun_access_key = uploader.aliyun_access_key
@aliyun_bucket = uploader.aliyun_bucket
@@ -17,12 +17,12 @@
@aliyun_private_read = uploader.aliyun_private_read
# Host for get request
@aliyun_host = uploader.aliyun_host || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
- if not @aliyun_host.include?("//")
- raise "config.aliyun_host requirement include // http:// or https://, but you give: #{@aliyun_host}"
+ unless @aliyun_host.include?('//')
+ fail "config.aliyun_host requirement include // http:// or https://, but you give: #{@aliyun_host}"
end
end
# 上传文件
# params:
@@ -30,36 +30,36 @@
# - file - 需要上传文件的 File 对象
# - options:
# - content_type - 上传文件的 MimeType,默认 `image/jpg`
# returns:
# 图片的下载地址
- def put(path, file, options={})
- path.sub!(/^\//, '')
+ def put(path, file, options = {})
+ path.sub!(PATH_PREFIX, '')
opts = {
- 'Content-Type' => options[:content_type] || "image/jpg"
+ 'Content-Type' => options[:content_type] || 'image/jpg'
}
res = oss_upload_client.bucket_create_object(path, file, opts)
if res.success?
path_to_url(path)
else
- raise "Put file failed"
+ fail 'Put file failed'
end
end
# 读取文件
# params:
# - path - remote 存储路径
# returns:
# file data
def get(path)
- path.sub!(/^\//, '')
+ path.sub!(PATH_PREFIX, '')
res = oss_upload_client.bucket_get_object(path)
if res.success?
return res.parsed_response
else
- raise "Get content faild"
+ fail 'Get content faild'
end
end
# 删除 Remote 的文件
#
@@ -67,33 +67,34 @@
# - path - remote 存储路径
#
# returns:
# 图片的下载地址
def delete(path)
- path.sub!(/^\//, '')
+ path.sub!(PATH_PREFIX, '')
res = oss_upload_client.bucket_delete_object(path)
if res.success?
return path_to_url(path)
else
- raise "Delete failed"
+ fail 'Delete failed'
end
end
##
# 根据配置返回完整的上传文件的访问地址
def path_to_url(path)
- [@aliyun_host, path].join("/")
+ [@aliyun_host, path].join('/')
end
# 私有空间访问地址,会带上实时算出的 token 信息
# 有效期 3600s
def private_get_url(path)
- path.sub!(/^\//, '')
+ path.sub!(PATH_PREFIX, '')
oss_client.bucket_get_object_share_link(path, 3600)
end
private
+
def oss_client
return @oss_client if defined?(@oss_client)
opts = {
host: "oss-#{@aliyun_area}.aliyuncs.com",
bucket: @aliyun_bucket
@@ -115,25 +116,23 @@
@oss_upload_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
end
end
class File < CarrierWave::SanitizedFile
- def initialize(uploader, base, path)
- @uploader = uploader
- @path = URI.encode(path)
- @base = base
- end
-
##
# Returns the current path/filename of the file on Cloud Files.
#
# === Returns
#
# [String] A path
#
- def path
- @path
+ attr_reader :path
+
+ def initialize(uploader, base, path)
+ @uploader = uploader
+ @path = URI.encode(path)
+ @base = base
end
##
# Reads the contents of the file from Cloud Files
#
@@ -149,18 +148,16 @@
##
# Remove the file from Cloud Files
#
def delete
- begin
- oss_connection.delete(@path)
- true
- rescue Exception => e
- # If the file's not there, don't panic
- puts "carrierwave-aliyun delete file failed: #{e}"
- nil
- end
+ oss_connection.delete(@path)
+ true
+ rescue => e
+ # If the file's not there, don't panic
+ puts "carrierwave-aliyun delete file failed: #{e}"
+ nil
end
def url
if @uploader.aliyun_private_read
oss_connection.private_get_url(@path)
@@ -181,28 +178,27 @@
oss_connection.put(@path, file, opts)
end
private
- def headers
- @headers ||= {}
- end
+ def headers
+ @headers ||= {}
+ end
- def connection
- @base.connection
- end
+ def connection
+ @base.connection
+ end
- def oss_connection
- return @oss_connection if defined? @oss_connection
+ def oss_connection
+ return @oss_connection if defined? @oss_connection
- @oss_connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
- end
-
+ @oss_connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
+ end
end
def store!(file)
f = CarrierWave::Storage::Aliyun::File.new(uploader, self, uploader.store_path)
- f.store(::File.open(file.file), :content_type => file.content_type)
+ f.store(::File.open(file.file), content_type: file.content_type)
f
end
def retrieve!(identifier)
CarrierWave::Storage::Aliyun::File.new(uploader, self, uploader.store_path(identifier))