class BindUrl::Binder
Attributes
attr[RW]
model[RW]
private[RW]
Public Class Methods
new(model:, attr:, private:)
click to toggle source
# File lib/bind_url/binder.rb, line 14 def initialize(model:, attr:, private:) @model = model @attr = attr @private = private end
Private Class Methods
oss_bucket()
click to toggle source
# File lib/bind_url/binder.rb, line 71 def oss_bucket return @oss_bucket if @oss_bucket c = Aliyun::OSS::Client.new( endpoint: storage_config.endpoint, access_key_id: storage_config.access_key_id, access_key_secret: storage_config.access_key_secret, ) @oss_bucket = c.get_bucket(storage_config.bucket) end
storage(val = nil)
click to toggle source
# File lib/bind_url/binder.rb, line 59 def storage(val = nil) if val @storage = val else @storage || :default end end
storage_config()
click to toggle source
# File lib/bind_url/binder.rb, line 67 def storage_config BindUrl.storage_configs[storage] end
Public Instance Methods
gen_url(v, parameters = {})
click to toggle source
# File lib/bind_url/binder.rb, line 24 def gen_url(v, parameters = {}) parameters = parameters.map { |key, value| [key.to_s, value.to_s] }.to_h path = self.class.oss_bucket.object_url(File.join(store_dir, v).delete_prefix('/'), self.private, 28800, parameters).delete_prefix(self.class.oss_bucket.bucket_url.delete_suffix('/')) self.class.storage_config.host.delete_suffix('/') + path end
store_dir()
click to toggle source
# File lib/bind_url/binder.rb, line 20 def store_dir raise "need overwrite" end
upload_via_file(file)
click to toggle source
# File lib/bind_url/binder.rb, line 34 def upload_via_file(file) extname = Pathname.new(file.path).extname filename = "#{SecureRandom.uuid.delete('-')}#{extname}" self.class.oss_bucket.put_object( File.join(store_dir, filename), file: file.path, content_type: Rack::Mime::MIME_TYPES[extname] || MimeMagic.by_magic(file)&.type || "application/octet-stream", acl: self.private ? "private" : "default", ) filename end
upload_via_url(url)
click to toggle source
# File lib/bind_url/binder.rb, line 30 def upload_via_url(url) upload_via_file(download_as_tmp_file(url)) end
Private Instance Methods
download_as_tmp_file(url)
click to toggle source
# File lib/bind_url/binder.rb, line 48 def download_as_tmp_file(url) res = RestClient.get(url) ext = Pathname.new(URI(url).path).extname.presence || Rack::Mime::MIME_TYPES.invert[res.headers[:content_type]] file = Tempfile.new(["", ext]) file.binmode file.write(res.body) file.flush file end