<commit_before>require 'hydrant/matterhorn_jobs'
require 'fileutils'

class MasterFile < ActiveFedora::Base
  include ActiveFedora::Associations
  include Hydra::ModelMethods
  include Hydra::ModelMixins::CommonMetadata
  include Hydra::ModelMixins::RightsMetadata

  belongs_to :mediaobject, :class_name=>'MediaObject', :property=>:is_part_of
  has_many :derivatives, :class_name=>'Derivative', :property=>:is_derivation_of

  has_metadata name: 'descMetadata', :type => ActiveFedora::SimpleDatastream do |d|
    d.field :file_location, :string
    d.field :file_checksum, :string
    d.field :file_size, :string
    d.field :duration, :string
    d.field :file_format, :string
  end

  has_metadata name: 'mhMetadata', :type => ActiveFedora::SimpleDatastream do |d|
    d.field :workflow_id, :string
    d.field :mediapackage_id, :string
    d.field :percent_complete, :string
    d.field :status_code, :string
    d.field :failures, :string
  end

  delegate_to 'descMetadata', [:file_location, :file_checksum, :file_size, :duration, :file_format], unique: true
  delegate_to 'mhMetadata', [:workflow_id, :mediapackage_id, :percent_complete, :status_code, :failures], unique:true

  has_file_datastream name: 'thumbnail'
  has_file_datastream name: 'poster'

  # First and simplest test - make sure that the uploaded file does not exceed the
  # limits of the system. For now this is hard coded but should probably eventually
  # be set up in a configuration file somewhere
  #
  # 250 MB is the file limit for now
  MAXIMUM_UPLOAD_SIZE = (2**20) * 250

  AUDIO_TYPES = ["audio/vnd.wave", "audio/mpeg", "audio/mp3", "audio/mp4", "audio/wav", "audio/x-wav"]
  VIDEO_TYPES = ["application/mp4", "video/mpeg", "video/mpeg2", "video/mp4", "video/quicktime", "video/avi"]
  UNKNOWN_TYPES = ["application/octet-stream", "application/x-upload-data"]

  QUALITY_ORDER = { "low" => 1, "medium" => 2, "high" => 3 }

  END_STATES = ['STOPPED', 'SUCCEEDED', 'FAILED', 'SKIPPED']

  def save_parent
    unless mediaobject.nil?
      mediaobject.save(validate: false)  
    end
  end

  def destroy
    parent = mediaobject
    parent.parts -= [self]

    unless new_object?
      parent.save(validate: false)
      Rubyhorn.client.stop(workflow_id) if workflow_id
      delete
    end
  end

  def setContent(file, content_type = nil)
    if file.is_a? ActionDispatch::Http::UploadedFile
      self.file_format = determine_format(file.tempfile, file.content_type)
      saveOriginal(file, file.original_filename)
    else
      self.file_format = determine_format(file, content_type)
      saveOriginal(file, nil)
    end
  end

  def process
    args = {"url" => URI.join("file:///", URI.escape(file_location)).to_s,
                "title" => pid,
                "flavor" => "presenter/source",
                "filename" => File.basename(file_location)}

    if file_format == 'Sound'
      args['workflow'] = "fullaudio"
    elsif file_format == 'Moving image'
      args['workflow'] = "hydrant"
    end
    
    m = MatterhornJobs.new
    m.send_request args
  end

  def status_description
    case status_code
      when "INSTANTIATED"
        "Preparing file for conversion"
      when "RUNNING"
        "Creating derivatives"
      when "SUCCEEDED"
        s = self.failures.to_i > 0 ? " (#{failures} failed steps)" : ""
        "Processing is complete#{s}"
      when "FAILED"
        "File(s) could not be processed"
      when "STOPPED"
        "Processing has been stopped"
      else
        "Waiting for conversion to begin"
      end
  end

  def stream_details(token)
    flash, hls = [], []
    derivatives.each do |d|
      common = { quality: d.encoding.quality.first,
                 mimetype: d.encoding.mime_type.first,
                 format: d.format } 
      flash << common.merge(url: d.tokenized_url(token, false))
      hls << common.merge(url: d.tokenized_url(token, true)) 
    end

    # Sorts the streams in order of quality, note: Hash order only works in Ruby 1.9 or later
    flash = sort_streams flash
    hls = sort_streams hls

    # Returns the hash
    {
      label: label,
      poster_image: poster_url,
      mediapackage_id: mediapackage_id,
      stream_flash: flash, 
      stream_hls: hls 
    }
  end
<commit_msg>Poster images should use proxied poster image...path is in hash now but not rendering in player<commit_after>require 'hydrant/matterhorn_jobs'
require 'fileutils'

class MasterFile < ActiveFedora::Base
  include ActiveFedora::Associations
  include Hydra::ModelMethods
  include Hydra::ModelMixins::CommonMetadata
  include Hydra::ModelMixins::RightsMetadata

  belongs_to :mediaobject, :class_name=>'MediaObject', :property=>:is_part_of
  has_many :derivatives, :class_name=>'Derivative', :property=>:is_derivation_of

  has_metadata name: 'descMetadata', :type => ActiveFedora::SimpleDatastream do |d|
    d.field :file_location, :string
    d.field :file_checksum, :string
    d.field :file_size, :string
    d.field :duration, :string
    d.field :file_format, :string
  end

  has_metadata name: 'mhMetadata', :type => ActiveFedora::SimpleDatastream do |d|
    d.field :workflow_id, :string
    d.field :mediapackage_id, :string
    d.field :percent_complete, :string
    d.field :status_code, :string
    d.field :failures, :string
  end

  delegate_to 'descMetadata', [:file_location, :file_checksum, :file_size, :duration, :file_format], unique: true
  delegate_to 'mhMetadata', [:workflow_id, :mediapackage_id, :percent_complete, :status_code, :failures], unique:true

  has_file_datastream name: 'thumbnail'
  has_file_datastream name: 'poster'

  # First and simplest test - make sure that the uploaded file does not exceed the
  # limits of the system. For now this is hard coded but should probably eventually
  # be set up in a configuration file somewhere
  #
  # 250 MB is the file limit for now
  MAXIMUM_UPLOAD_SIZE = (2**20) * 250

  AUDIO_TYPES = ["audio/vnd.wave", "audio/mpeg", "audio/mp3", "audio/mp4", "audio/wav", "audio/x-wav"]
  VIDEO_TYPES = ["application/mp4", "video/mpeg", "video/mpeg2", "video/mp4", "video/quicktime", "video/avi"]
  UNKNOWN_TYPES = ["application/octet-stream", "application/x-upload-data"]

  QUALITY_ORDER = { "low" => 1, "medium" => 2, "high" => 3 }

  END_STATES = ['STOPPED', 'SUCCEEDED', 'FAILED', 'SKIPPED']

  def save_parent
    unless mediaobject.nil?
      mediaobject.save(validate: false)  
    end
  end

  def destroy
    parent = mediaobject
    parent.parts -= [self]

    unless new_object?
      parent.save(validate: false)
      Rubyhorn.client.stop(workflow_id) if workflow_id
      delete
    end
  end

  def setContent(file, content_type = nil)
    if file.is_a? ActionDispatch::Http::UploadedFile
      self.file_format = determine_format(file.tempfile, file.content_type)
      saveOriginal(file, file.original_filename)
    else
      self.file_format = determine_format(file, content_type)
      saveOriginal(file, nil)
    end
  end

  def process
    args = {"url" => URI.join("file:///", URI.escape(file_location)).to_s,
                "title" => pid,
                "flavor" => "presenter/source",
                "filename" => File.basename(file_location)}

    if file_format == 'Sound'
      args['workflow'] = "fullaudio"
    elsif file_format == 'Moving image'
      args['workflow'] = "hydrant"
    end
    
    m = MatterhornJobs.new
    m.send_request args
  end

  def status_description
    case status_code
      when "INSTANTIATED"
        "Preparing file for conversion"
      when "RUNNING"
        "Creating derivatives"
      when "SUCCEEDED"
        s = self.failures.to_i > 0 ? " (#{failures} failed steps)" : ""
        "Processing is complete#{s}"
      when "FAILED"
        "File(s) could not be processed"
      when "STOPPED"
        "Processing has been stopped"
      else
        "Waiting for conversion to begin"
      end
  end

  def stream_details(token)
    flash, hls = [], []
    derivatives.each do |d|
      common = { quality: d.encoding.quality.first,
                 mimetype: d.encoding.mime_type.first,
                 format: d.format } 
      flash << common.merge(url: d.tokenized_url(token, false))
      hls << common.merge(url: d.tokenized_url(token, true)) 
    end

    # Sorts the streams in order of quality, note: Hash order only works in Ruby 1.9 or later
    flash = sort_streams flash
    hls = sort_streams hls

    poster_path = Rails.application.routes.url_helpers.poster_master_file_path(self) unless poster.new?

    # Returns the hash
    {
      label: label,
      poster_image: poster_path,
      mediapackage_id: mediapackage_id,
      stream_flash: flash, 
      stream_hls: hls 
    }
  end

{'fraction_non_alphanumeric': 0.11218390804597701, 'fraction_numerical': 0.004597701149425287, 'mean_word_length': 3.252688172043011, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '207055', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2806, 'n_words': 713}