Class: NaranyaEcm::MediaResource

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Timestampable
Defined in:
lib/naranya_ecm/models/media_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.root_directoryObject

Returns the storage root directory, depending of the storage engine:

  • Amazon AWS S3: A Fog::Storage::AWS::Directory object



116
117
118
# File 'lib/naranya_ecm/models/media_resource.rb', line 116

def self.root_directory
  NContent::SDK.root_directory
end

Instance Method Details

#as_notification_payloadObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/naranya_ecm/models/media_resource.rb', line 38

def as_notification_payload
  instance_payload = super.merge(roles: self.roles)

  if self.parent_document_id.present? && self.parent_document_type.present?
    instance_payload[:parent_id]    = self.parent_document_id
    instance_payload[:parent_class] = "NaranyaEcm::#{self.parent_document_type}"
  end

  instance_payload
end

#downloadable_authorized_url(expiration_time = nil) ⇒ Object

Return the URL used to access the media resource file.

  • In case of a private resource file, the URL includes parameters used to access the file for the following 5 minutes, unless an expiration time is given.



103
104
105
106
107
108
109
110
# File 'lib/naranya_ecm/models/media_resource.rb', line 103

def downloadable_authorized_url(expiration_time = nil)
  if self.public?
    downloadable_resource.public_url
  else
    expiration_time = 5.minutes.from_now unless expiration_time.is_a? Time
    downloadable_resource.url(expiration_time)
  end
end

#downloadable_cdn_url(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/naranya_ecm/models/media_resource.rb', line 90

def downloadable_cdn_url(options = {})
  options = options.with_indifferent_access
  ret = self.downloadable_uri.clone

  ret.scheme = options[:scheme] if options.has_key? :scheme
  ret.host   = options[:host]   if options.has_key? :host

  ret.to_s
end

#downloadable_hostObject



86
87
88
# File 'lib/naranya_ecm/models/media_resource.rb', line 86

def downloadable_host
  @downloadable_host ||= self.downloadable_uri.host if self.downloadable_uri.present?
end

#downloadable_pathObject



82
83
84
# File 'lib/naranya_ecm/models/media_resource.rb', line 82

def downloadable_path
  @downloadable_path ||= self.downloadable_uri.path if self.downloadable_uri.present?
end

#downloadable_resourceObject

Returns the downloadable resource, which depending of the storage engine, is one of the following:

  • Amazon AWS S3: A Fog::Storage::AWS::File object, representing the file object in Amazon



67
68
69
# File 'lib/naranya_ecm/models/media_resource.rb', line 67

def downloadable_resource
  root_directory.files.head downloadable_resource_key
end

#downloadable_resource_keyObject

Returns the downloadable resource key, which depending of the storage engine, is one of the following:

  • Amazon AWS S3: The downloadable object key



61
62
63
# File 'lib/naranya_ecm/models/media_resource.rb', line 61

def downloadable_resource_key
  @downloadable_file_key ||= self.downloadable_path[1..-1] if self.downloadable_path.present?
end

#downloadable_uriObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/naranya_ecm/models/media_resource.rb', line 71

def downloadable_uri
  @downloadable_uri ||= if self.downloadable_url.present?
    begin
      URI(self.downloadable_url)
    rescue URI::InvalidURIError
      # Maybe a special character prevents the URI to form...
      URI(URI::encode self.downloadable_url)
    end
  end
end

#media_resourcesObject

:attr: media_resources :category: Associations

A list of child/processed MediaResource objects associated directly to this MediaResource.



33
# File 'lib/naranya_ecm/models/media_resource.rb', line 33

has_many :media_resources, class_name: :MediaResource, inverse_of: :media_resource

#move_downloadable_to(path, force_move = false) ⇒ Object

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/naranya_ecm/models/media_resource.rb', line 120

def move_downloadable_to(path, force_move=false)

  raise ArgumentError, "Given path is empty" unless path.present?
  path = path[1..-1] if path.present? && path[0] == "/"
  raise ArgumentError, "Can't use root (/) as target path" unless path.present?

  path_exists = root_directory.files.head(path).present?

  if !path_exists || (path_exists && force_move)
    old_file = root_directory.files.get downloadable_resource_key

    new_file = root_directory.files.create(
      key: path,
      body: old_file.body,
      content_type: old_file.content_type,
      last_modified: old_file.last_modified,
      :public => public?
    )

    self.downloadable_url = public? ? new_file.public_url : new_file.url(1.minutes.from_now).split("?").first

    old_file.destroy if self.save
    true
  else
    false
  end

end

#private?Boolean

Returns true if the Media Resource is private

Returns:

  • (Boolean)


50
51
52
# File 'lib/naranya_ecm/models/media_resource.rb', line 50

def private?
  self.access_type == 'private'
end

#process_with_profile(given_profile, given_options = {}) ⇒ Object

Generates a MediaProcess with self as the source MediaResource.

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
# File 'lib/naranya_ecm/models/media_resource.rb', line 150

def process_with_profile(given_profile, given_options = {})
  raise ArgumentError, 'Given profile is not a MediaProfile' unless given_profile.is_a? NaranyaEcm::MediaProfile

  # TODO: Eliminar template_id...
  options = given_options.merge media_resource: self, template_id: given_profile.template_id

  NaranyaEcm::MediaProcess.create options
end

#public?Boolean

Returns true if the Media Resource is private

Returns:

  • (Boolean)


55
56
57
# File 'lib/naranya_ecm/models/media_resource.rb', line 55

def public?
  self.access_type == 'public'
end