Class: Attach::Backends::Abstract
- Inherits:
-
Object
- Object
- Attach::Backends::Abstract
- Defined in:
- lib/attach/backends/abstract.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#bytesize(binary) ⇒ Object
Return the bytesize of a given binary.
-
#delete(attachment) ⇒ Object
Delete the data for the given attachment.
-
#digest(binary) ⇒ Object
Return the SHA1 digest of a given binary.
-
#initialize(config = {}) ⇒ Abstract
constructor
A new instance of Abstract.
-
#read(attachment) ⇒ Object
Return the data for the given attachment.
-
#read_multi(attachments) ⇒ Object
Return binaries for a set of files.
-
#url(attachment) ⇒ Object
Return the URL that this attachment can be accessed at.
-
#write(attachment, data) ⇒ Object
Write data for the given attachment.
Constructor Details
#initialize(config = {}) ⇒ Abstract
Returns a new instance of Abstract.
7 8 9 |
# File 'lib/attach/backends/abstract.rb', line 7 def initialize(config = {}) @config = config end |
Instance Method Details
#bytesize(binary) ⇒ Object
Return the bytesize of a given binary
66 67 68 69 70 71 72 |
# File 'lib/attach/backends/abstract.rb', line 66 def bytesize(binary) if binary.respond_to?(:path) ::File.size(binary.path) else binary.bytesize end end |
#delete(attachment) ⇒ Object
Delete the data for the given attachment
26 27 |
# File 'lib/attach/backends/abstract.rb', line 26 def delete() end |
#digest(binary) ⇒ Object
Return the SHA1 digest of a given binary
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/attach/backends/abstract.rb', line 49 def digest(binary) if binary.respond_to?(:path) sha1 = Digest::SHA1.new binary.binmode binary.rewind while chunk = binary.read(1024 * 1024) sha1.update(chunk) end sha1.hexdigest else Digest::SHA1.hexdigest(binary) end end |
#read(attachment) ⇒ Object
Return the data for the given attachment
14 15 |
# File 'lib/attach/backends/abstract.rb', line 14 def read() end |
#read_multi(attachments) ⇒ Object
Return binaries for a set of files. They should be returned as a hash consisting of the attachment ID followed by the data
40 41 42 43 44 |
# File 'lib/attach/backends/abstract.rb', line 40 def read_multi() .compact.each_with_object({}) do |, hash| hash[] = read() end end |
#url(attachment) ⇒ Object
Return the URL that this attachment can be accessed at
32 33 34 |
# File 'lib/attach/backends/abstract.rb', line 32 def url() "#{Attach.asset_host}/attachment/#{attachment.token}/#{attachment.file_name}" end |
#write(attachment, data) ⇒ Object
Write data for the given attachment
20 21 |
# File 'lib/attach/backends/abstract.rb', line 20 def write(, data) end |