Class: Duracloud::Content

Inherits:
AbstractEntity show all
Defined in:
lib/duracloud/content.rb

Overview

A piece of content in DuraCloud

Direct Known Subclasses

ChunkedContent

Defined Under Namespace

Classes: CopyError

Constant Summary collapse

CHUNK_SIZE =
1024 * 16
COPY_SOURCE_HEADER =
"x-dura-meta-copy-source"
COPY_SOURCE_STORE_HEADER =
"x-dura-meta-copy-source-store"
MANIFEST_EXT =
".dura-manifest"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractEntity

#delete, #deleted?, #load_properties, #persisted?, #properties, #save

Class Method Details

.create(**kwargs) ⇒ Duraclound::Content

Create new content in DuraCloud.

Returns:

  • (Duraclound::Content)

    the content

Raises:



53
54
55
# File 'lib/duracloud/content.rb', line 53

def self.create(**kwargs)
  new(**kwargs).save
end

.delete(**kwargs) ⇒ Duraclound::Content

Delete content from DuraCloud.

Returns:

  • (Duraclound::Content)

    the deleted content.

Raises:



62
63
64
# File 'lib/duracloud/content.rb', line 62

def self.delete(**kwargs)
  find(**kwargs).delete
end

.exist?(**kwargs) ⇒ Boolean

Does the content exist in DuraCloud?

Returns:

  • (Boolean)

    whether the content exists.

Raises:



29
30
31
32
33
# File 'lib/duracloud/content.rb', line 29

def self.exist?(**kwargs)
  find(**kwargs) && true
rescue NotFoundError
  false
end

.find(**kwargs) ⇒ Duraclound::Content

Find content in DuraCloud.

Returns:

  • (Duraclound::Content)

    the content

Raises:



40
41
42
43
44
45
46
# 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

Instance Method Details

#chunked?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/duracloud/content.rb', line 125

def chunked?
  false
end

#copy(**args) ⇒ Duracloud::Content

Returns the copied content The current instance still represents the original content.

Returns:

  • (Duracloud::Content)

    the copied content The current instance still represents the original content.

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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 {|String| ... } ⇒ Duracloud::Response

Downloads the remote content

Yields:

  • (String)

    chunk of the remote content, if block given.

Returns:

Raises:



89
90
91
# File 'lib/duracloud/content.rb', line 89

def download(&block)
  Client.get_content(*args, **query, &block)
end

#empty?Boolean

Is the content empty?

Returns:

  • (Boolean)

    whether the content is empty (nil or empty string)



81
82
83
# File 'lib/duracloud/content.rb', line 81

def empty?
  body.nil? || ( body.respond_to?(:size) && body.size == 0 )
end

#human_sizeObject



129
130
131
# File 'lib/duracloud/content.rb', line 129

def human_size
  ActiveSupport::NumberHelper.number_to_human_size(size, prefix: :si)
end

#inspectObject



73
74
75
76
77
# 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) ⇒ Duracloud::Content

Returns the moved content The current instance still represents the deleted content.

Returns:

  • (Duracloud::Content)

    the moved content The current instance still represents the deleted content.

Raises:



119
120
121
122
123
# File 'lib/duracloud/content.rb', line 119

def move(**args)
  copied = copy(**args)
  delete
  copied
end

#spaceDuracloud::Space

Return the space associated with this content.

Returns:

Raises:



69
70
71
# File 'lib/duracloud/content.rb', line 69

def space
  @space ||= Space.find(space_id, store_id)
end