<commit_before>class MediaController < ApplicationController

  #load_resource :project, only: [:audio, :spectrogram]
  #load_resource :site, only: [:audio, :spectrogram]
  load_and_authorize_resource :audio_recording, only: [:show]

  before_filter :check_offset_format

  AUDIO_MEDIA_TYPES = [Mime::Type.lookup('audio/webm'), Mime::Type.lookup('audio/webma'),
                       Mime::Type.lookup('audio/ogg'), Mime::Type.lookup('audio/oga'),
                       Mime::Type.lookup('audio/mp3'), Mime::Type.lookup('audio/mpeg'),
                       Mime::Type.lookup('audio/wav'), Mime::Type.lookup('audio/x-wav'),
                       Mime::Type.lookup('audio/x-flac')]
  IMAGE_MEDIA_TYPES = [Mime::Type.lookup('image/png')]

  OFFSET_REGEXP = /^\d+(\.\d{1,3})?$/ # passes '111', '11.123'

  MEDIA_PROCESSOR_LOCAL = 'local'
  MEDIA_PROCESSOR_RESQUE = 'resque'

  def show

    log_options(params, '#show method start')

    @media_processor = Settings.media_request_processor
    @range_request = Settings.range_request
    @media_cacher = Settings.media_cache_tool

    @available_text_formats = Settings.available_formats.text
    @available_audio_formats = Settings.available_formats.audio
    @available_image_formats = Settings.available_formats.image

    #default_dataset = Settings.cached_dataset_defaults

    @available_formats = @available_text_formats + @available_audio_formats + @available_image_formats

    is_audio_ready = @audio_recording.status == 'ready'
    is_head_request = request.head?
    is_available_format = @available_formats.include?(params[:format].downcase)

    if !is_audio_ready && is_head_request
      # changed from 422 Unprocessable entity
      head status: :accepted
    elsif !is_audio_ready && !is_head_request
      render json: {error: 'Audio recording is not ready'}.to_json, status: :accepted
    elsif !is_available_format && is_head_request
      head status: :unsupported_media_type
    elsif !is_available_format && !is_head_request
      render json: {error: 'Requested format is invalid. It must be one of available_formats.', available_formats: @available_formats}.to_json, status: :unsupported_media_type
    elsif is_available_format && is_audio_ready
      log_options(params, '#show audio recording ready')
      parse_media_request(@audio_recording, params)
    else
      render json: {error: 'Invalid request'}.to_json, status: :bad_request
    end
  end

  def get_available_formats(audio_recording, formats, start_offset, end_offset, defaults)

    result = {}

    formats.each do |format|
      format_key = format.to_s
      result[format_key] = defaults.dup
      result[format_key].delete 'format'
      result[format_key][:extension] = format_key
      result[format_key]['mime_type'] = Mime::Type.lookup_by_extension(format).to_s
      result[format_key]['url'] = audio_recording_media_path(
          audio_recording,
          format: format,
          start_offset: start_offset,
          end_offset: end_offset)
    end

    result
  end

  private

  # @param [AudioRecording] audio_recording
  # @param [Hash] request_params
  def parse_media_request(audio_recording, request_params)
    options = Hash.new<commit_msg>another attempt to fix the params problem<commit_after>class MediaController < ApplicationController

  #load_resource :project, only: [:audio, :spectrogram]
  #load_resource :site, only: [:audio, :spectrogram]
  load_and_authorize_resource :audio_recording, only: [:show]

  before_filter :check_offset_format

  AUDIO_MEDIA_TYPES = [Mime::Type.lookup('audio/webm'), Mime::Type.lookup('audio/webma'),
                       Mime::Type.lookup('audio/ogg'), Mime::Type.lookup('audio/oga'),
                       Mime::Type.lookup('audio/mp3'), Mime::Type.lookup('audio/mpeg'),
                       Mime::Type.lookup('audio/wav'), Mime::Type.lookup('audio/x-wav'),
                       Mime::Type.lookup('audio/x-flac')]
  IMAGE_MEDIA_TYPES = [Mime::Type.lookup('image/png')]

  OFFSET_REGEXP = /^\d+(\.\d{1,3})?$/ # passes '111', '11.123'

  MEDIA_PROCESSOR_LOCAL = 'local'
  MEDIA_PROCESSOR_RESQUE = 'resque'

  def show

    # to stop hard-to-find bugs where start_offset and end_offset
    # change to nil or just vanish
    request_params = params.symbolize_keys

    log_options(request_params, '#show method start')

    @media_processor = Settings.media_request_processor
    @range_request = Settings.range_request
    @media_cacher = Settings.media_cache_tool

    @available_text_formats = Settings.available_formats.text
    @available_audio_formats = Settings.available_formats.audio
    @available_image_formats = Settings.available_formats.image

    #default_dataset = Settings.cached_dataset_defaults

    @available_formats = @available_text_formats + @available_audio_formats + @available_image_formats

    is_audio_ready = @audio_recording.status == 'ready'
    is_head_request = request.head?
    is_available_format = @available_formats.include?(request_params[:format].downcase)

    if !is_audio_ready && is_head_request
      # changed from 422 Unprocessable entity
      head status: :accepted
    elsif !is_audio_ready && !is_head_request
      render json: {error: 'Audio recording is not ready'}.to_json, status: :accepted
    elsif !is_available_format && is_head_request
      head status: :unsupported_media_type
    elsif !is_available_format && !is_head_request
      render json: {error: 'Requested format is invalid. It must be one of available_formats.', available_formats: @available_formats}.to_json, status: :unsupported_media_type
    elsif is_available_format && is_audio_ready
      log_options(request_params, '#show audio recording ready')
      parse_media_request(@audio_recording, request_params)
    else
      render json: {error: 'Invalid request'}.to_json, status: :bad_request
    end
  end

  def get_available_formats(audio_recording, formats, start_offset, end_offset, defaults)

    result = {}

    formats.each do |format|
      format_key = format.to_s
      result[format_key] = defaults.dup
      result[format_key].delete 'format'
      result[format_key][:extension] = format_key
      result[format_key]['mime_type'] = Mime::Type.lookup_by_extension(format).to_s
      result[format_key]['url'] = audio_recording_media_path(
          audio_recording,
          format: format,
          start_offset: start_offset,
          end_offset: end_offset)
    end

    result
  end

  private

  # @param [AudioRecording] audio_recording
  # @param [Hash] request_params
  def parse_media_request(audio_recording, request_params)
    options = Hash.new
{'fraction_non_alphanumeric': 0.10566037735849057, 'fraction_numerical': 0.004226415094339623, 'mean_word_length': 4.004531722054381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '229421', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2048, 'n_words': 391}