Module: Ddr::Datastreams::DatastreamBehavior

Defined in:
lib/ddr/datastreams/datastream_behavior.rb

Constant Summary collapse

DEFAULT_FILE_EXTENSION =
"bin"
STRFTIME_FORMAT =
"%Y-%m-%dT%H:%M:%S.%LZ"

Instance Method Summary collapse

Instance Method Details

#content_digest(algorithm) ⇒ Object



40
41
42
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 40

def content_digest algorithm
  Ddr::Utils.digest(self.content, algorithm)
end

#create_date_stringObject



36
37
38
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 36

def create_date_string
  dsCreateDate.strftime(STRFTIME_FORMAT) if dsCreateDate
end

#default_file_extensionObject

Return default file extension for datastream based on MIME type



72
73
74
75
76
77
78
79
80
81
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 72

def default_file_extension
  mimetypes = MIME::Types[mimeType]
  return mimetypes.first.extensions.first unless mimetypes.empty?
  case mimeType
  when 'application/n-triples'
    'txt'
  else
    DEFAULT_FILE_EXTENSION
  end
end

#default_file_nameObject

Return default file name



89
90
91
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 89

def default_file_name
  [default_file_prefix, default_file_extension].join(".")
end

#default_file_prefixObject

Return default file name prefix based on object PID



84
85
86
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 84

def default_file_prefix
  [pid.sub(/:/, '_'), dsid].join("_")
end

#file_nameObject

Returns the file name of the external file for the datastream. See #external_datastream_file_path(ds)



60
61
62
63
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 60

def file_name
  raise "The `file_name' method is valid only for external datastreams." unless external?
  File.basename(file_path) rescue nil
end

#file_pathObject

Returns the external file path for the datastream. Returns nil if dsLocation is not a file URI.



53
54
55
56
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 53

def file_path
  raise "The `file_path' method is valid only for external datastreams." unless external?
  Ddr::Utils.path_from_uri(dsLocation) if Ddr::Utils.file_uri?(dsLocation)
end

#file_pathsObject

Returns a list of the external file paths for all versions of the datastream.



45
46
47
48
49
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 45

def file_paths
  raise "The `file_paths' method is valid only for external datastreams." unless external?
  return Array(file_path) if new?
  versions.map(&:file_path).compact
end

#file_sizeObject

Returns the size of the external file for the datastream.



66
67
68
69
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 66

def file_size
  raise "The `file_size' method is valid only for external datastreams." unless external?
  File.size(file_path) rescue nil
end

#validate_checksum!(checksum, checksum_type = nil) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 9

def validate_checksum! checksum, checksum_type=nil
  raise Ddr::Models::Error, "Checksum cannot be validated on new datastream." if new?
  raise Ddr::Models::Error, "Checksum cannot be validated on unpersisted content." if content_changed?
  raise Ddr::Models::ChecksumInvalid, "The repository internal checksum validation failed." unless dsChecksumValid
  algorithm = checksum_type || self.checksumType
  ds_checksum = if algorithm == self.checksumType
                  self.checksum
                else 
                  content_digest(algorithm)
                end
  if checksum == ds_checksum
    "The checksum [#{algorithm}]#{checksum} is valid for datastream #{version_info}."
  else
    raise Ddr::Models::ChecksumInvalid, "The checksum [#{algorithm}]#{checksum} is not valid for datastream #{version_info}."
  end
end

#version_infoObject



31
32
33
34
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 31

def version_info
  # E.g., info:fedora/duke:1/content/content.0 [2013-09-26T20:00:03.357Z]
  "#{version_uri} [#{Ddr::Utils.ds_as_of_date_time(self)}]" unless new?
end

#version_uriObject



26
27
28
29
# File 'lib/ddr/datastreams/datastream_behavior.rb', line 26

def version_uri
  # E.g., info:fedora/duke:1/content/content.0
  ["info:fedora", pid, dsid, dsVersionID].join("/") unless new?
end