class Duracloud::Content

A piece of content in DuraCloud

Constants

CHUNK_SIZE
COPY_SOURCE_HEADER
COPY_SOURCE_STORE_HEADER
MANIFEST_EXT

Public Class Methods

create(**kwargs) click to toggle source

Create new content in DuraCloud. @return [Duraclound::Content] the content @raise [Duracloud::NotFoundError] the space or store (if given) does not exist. @raise [Duracloud::MessageDigestError] the provided digest in the :md5 keyword option,

if given, does not match the stored value.
# File lib/duracloud/content.rb, line 53
def self.create(**kwargs)
  new(**kwargs).save
end
delete(**kwargs) click to toggle source

Delete content from DuraCloud. @return [Duraclound::Content] the deleted content. @raise [Duracloud::NotFoundError] the space, content or store (if given) does not exist. @raise [Duracloud::MessageDigestError] the provided digest in the :md5 keyword option,

if given, does not match the stored value.
# File lib/duracloud/content.rb, line 62
def self.delete(**kwargs)
  find(**kwargs).delete
end
exist?(**kwargs) click to toggle source

Does the content exist in DuraCloud? @return [Boolean] whether the content exists. @raise [Duracloud::MessageDigestError] the provided digest in the :md5 keyword option,

if given, does not match the stored value.
# File lib/duracloud/content.rb, line 29
def self.exist?(**kwargs)
  find(**kwargs) && true
rescue NotFoundError
  false
end
find(**kwargs) click to toggle source

Find content in DuraCloud. @return [Duraclound::Content] the content @raise [Duracloud::NotFoundError] the space, content, or store (if given) does not exist. @raise [Duracloud::MessageDigestError] the provided digest in the :md5 keyword option,

if given, does not match the stored value.
# File lib/duracloud/content.rb, line 40
def self.find(**kwargs)
  new(**kwargs).tap do |content|
    content.load_properties
  end
rescue NotFoundError => e
  ChunkedContent.find(**kwargs)
end

Public Instance Methods

chunked?() click to toggle source
# File lib/duracloud/content.rb, line 125
def chunked?
  false
end
copy(**args) click to toggle source

@return [Duracloud::Content] the copied content

The current instance still represents the original content.

@raise [Duracloud::Error]

# File lib/duracloud/content.rb, line 96
def copy(**args)
  dest = args.reject { |k, v| k == :force }
  dest[:space_id]   ||= space_id
  dest[:store_id]   ||= store_id
  dest[:content_id] ||= content_id
  if dest == copy_source
    raise CopyError, "Destination is the same as the source."
  end
  if !args[:force] && Content.exist?(**dest)
    raise CopyError, "Destination exists and `:force' option is false."
  end
  options = { storeID: dest[:store_id], headers: copy_headers }
  response = Client.copy_content(dest[:space_id], dest[:content_id], **options)
  if md5 != response.md5
    raise CopyError, "Message digest of copy does not match source " \
                     "(source: #{md5}; destination: #{response.md5})"
  end
  Content.new(dest.merge(md5: md5))
end
download(&block) click to toggle source

Downloads the remote content @yield [String] chunk of the remote content, if block given. @return [Duracloud::Response] the response to the content request. @raise [Duracloud::NotFoundError]

# File lib/duracloud/content.rb, line 89
def download(&block)
  Client.get_content(*args, **query, &block)
end
empty?() click to toggle source

Is the content empty? @return [Boolean] whether the content is empty (nil or empty string)

# File lib/duracloud/content.rb, line 81
def empty?
  body.nil? || ( body.respond_to?(:size) && body.size == 0 )
end
human_size() click to toggle source
# File lib/duracloud/content.rb, line 129
def human_size
  ActiveSupport::NumberHelper.number_to_human_size(size, prefix: :si)
end
inspect() click to toggle source
# File lib/duracloud/content.rb, line 73
def inspect
  "#<#{self.class} space_id=#{space_id.inspect}," \
  " content_id=#{content_id.inspect}," \
  " store_id=#{store_id || '(default)'}>"
end
move(**args) click to toggle source

@return [Duracloud::Content] the moved content

The current instance still represents the deleted content.

@raise [Duracloud::Error]

# File lib/duracloud/content.rb, line 119
def move(**args)
  copied = copy(**args)
  delete
  copied
end
space() click to toggle source

Return the space associated with this content. @return [Duracloud::Space] the space. @raise [Duracloud::NotFoundError] the space or store does not exist.

# File lib/duracloud/content.rb, line 69
def space
  @space ||= Space.find(space_id, store_id)
end

Private Instance Methods

args() click to toggle source
# File lib/duracloud/content.rb, line 210
def args
  [ space_id, content_id ]
end
calculate_md5() click to toggle source
# File lib/duracloud/content.rb, line 163
def calculate_md5
  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
copy_headers() click to toggle source
# File lib/duracloud/content.rb, line 145
def copy_headers
  ch = { COPY_SOURCE_HEADER=>"#{space_id}/#{content_id}" }
  ch[COPY_SOURCE_STORE_HEADER] = store_id if store_id
  ch
end
copy_source() click to toggle source
# File lib/duracloud/content.rb, line 151
def copy_source
  { space_id: space_id, content_id: content_id, store_id: store_id }
end
do_delete() click to toggle source
# File lib/duracloud/content.rb, line 196
def do_delete
  Client.delete_content(*args, **query)
end
do_load_properties() click to toggle source
# File lib/duracloud/content.rb, line 181
def do_load_properties
  response = Client.get_content_properties(*args, **query)
  if md5
    if md5 != response.md5
      raise MessageDigestError, "Expected MD5: {#{md5}}; DuraCloud MD5: {#{response.md5}}."
    end
  else
    self.md5 = response.md5
  end
  self.properties = response.headers
  self.content_type = response.content_type
  self.size = response.size
  self.modified = response.modified
end
do_save() click to toggle source
# File lib/duracloud/content.rb, line 200
def do_save
  if !empty?
    store
  elsif persisted?
    set_properties
  else
    raise Error, "Cannot store empty content."
  end
end
io_like?() click to toggle source
# File lib/duracloud/content.rb, line 155
def io_like?
  body.respond_to?(:read) && body.respond_to?(:rewind)
end
properties_class() click to toggle source
# File lib/duracloud/content.rb, line 177
def properties_class
  ContentProperties
end
query() click to toggle source
# File lib/duracloud/content.rb, line 214
def query
  { storeID: store_id }
end
set_properties() click to toggle source
# File lib/duracloud/content.rb, line 159
def set_properties
  Client.set_content_properties(*args, headers: properties, query: query)
end
store() click to toggle source
# File lib/duracloud/content.rb, line 135
def store
  headers = {
    "Content-MD5"  => md5 || calculate_md5,
    "Content-Type" => content_type || "application/octet-stream"
  }
  headers.merge!(properties)
  options = { body: body, headers: headers, query: query }
  Client.store_content(*args, **options)
end