lib/duracloud/content.rb in duracloud-client-0.1.3 vs lib/duracloud/content.rb in duracloud-client-0.1.4

- old
+ new

@@ -8,10 +8,12 @@ class Content include ActiveModel::Dirty include Persistence include HasProperties + CHUNK_SIZE = 1024 * 16 + after_save :changes_applied # Does the content exist in DuraCloud? # @see .new for arguments # @return [Boolean] whether the content exists @@ -62,11 +64,11 @@ # @api private # @raise [Duracloud::NotFoundError] the content does not exist in DuraCloud. def load_body response = Client.get_content(*args, **query) - set_body(response) # don't use setter b/c marks as dirty + @body = response.body # don't use setter b/c marks as dirty persisted! end def load_properties super do |response| @@ -74,11 +76,11 @@ @content_type = response.content_type end end def body=(str_or_io) - set_body(str_or_io) + @body = str_or_io body_will_change! end # Return the content body, loading from DuraCloud if necessary. # @return [String, StringIO] the content body @@ -102,12 +104,12 @@ @content_type end private - def set_body(str_or_io) - @body = StringIO.new(read_string_or_io(str_or_io), "r") + def io_like? + body.respond_to?(:read) && body.respond_to?(:rewind) end def set_properties headers = properties.to_h headers["Content-Type"] = content_type if content_type_changed? @@ -124,14 +126,21 @@ options = { body: body, headers: headers, query: query } Client.store_content(*args, **options) end def md5 - body.rewind - Digest::MD5.hexdigest(body.read) - ensure - body.rewind + digest = Digest::MD5.new + if io_like? + body.rewind + while chunk = body.read(CHUNK_SIZE) do + digest << chunk + end + body.rewind + else + digest << body + end + digest.hexdigest end def properties_class ContentProperties end @@ -149,28 +158,9 @@ store elsif persisted? set_properties else raise Error, "Cannot store empty content." - end - end - - def read_string_or_io(str_or_io) - if str_or_io.respond_to?(:read) - read_io_like(str_or_io) - elsif str_or_io.respond_to?(:to_str) - str_or_io.to_str - else - raise ArgumentError, "IO-like or String-like argument required." - end - end - - def read_io_like(io_like) - begin - io_like.rewind if io_like.respond_to?(:rewind) - io_like.read - ensure - io_like.rewind if io_like.respond_to?(:rewind) end end def args [ space_id, content_id ]