Class: BindUrl::Binder
- Inherits:
-
Object
- Object
- BindUrl::Binder
- Defined in:
- lib/bind_url/binder.rb
Instance Attribute Summary collapse
-
#attr ⇒ Object
Returns the value of attribute attr.
-
#model ⇒ Object
Returns the value of attribute model.
-
#private ⇒ Object
Returns the value of attribute private.
Class Method Summary collapse
Instance Method Summary collapse
- #gen_url(v, parameters = {}) ⇒ Object
-
#initialize(model:, attr:, private:) ⇒ Binder
constructor
A new instance of Binder.
- #store_dir ⇒ Object
- #upload_via_file(file) ⇒ Object
- #upload_via_url(url) ⇒ Object
Constructor Details
#initialize(model:, attr:, private:) ⇒ Binder
Returns a new instance of Binder.
14 15 16 17 18 |
# File 'lib/bind_url/binder.rb', line 14 def initialize(model:, attr:, private:) @model = model @attr = attr @private = private end |
Instance Attribute Details
#attr ⇒ Object
Returns the value of attribute attr.
12 13 14 |
# File 'lib/bind_url/binder.rb', line 12 def attr @attr end |
#model ⇒ Object
Returns the value of attribute model.
12 13 14 |
# File 'lib/bind_url/binder.rb', line 12 def model @model end |
#private ⇒ Object
Returns the value of attribute private.
12 13 14 |
# File 'lib/bind_url/binder.rb', line 12 def private @private end |
Class Method Details
.oss_bucket ⇒ Object
71 72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/bind_url/binder.rb', line 59 def storage(val = nil) if val @storage = val else @storage || :default end end |
.storage_config ⇒ Object
67 68 69 |
# File 'lib/bind_url/binder.rb', line 67 def storage_config BindUrl.storage_configs[storage] end |
Instance Method Details
#gen_url(v, parameters = {}) ⇒ Object
24 25 26 27 28 |
# 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 ⇒ Object
20 21 22 |
# File 'lib/bind_url/binder.rb', line 20 def store_dir raise "need overwrite" end |
#upload_via_file(file) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# 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) ⇒ Object
30 31 32 |
# File 'lib/bind_url/binder.rb', line 30 def upload_via_url(url) upload_via_file(download_as_tmp_file(url)) end |