Skip to content

Commit

Permalink
fixed up some errors in the test, media controller now works
Browse files Browse the repository at this point in the history
  • Loading branch information
cofiem committed Nov 4, 2012
1 parent f3c43f0 commit 623cee6
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 99 deletions.
6 changes: 3 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class ApplicationController < ActionController::Base

private

def set_stamper
#def set_stamper
# current_user is provided by devise
User.stamper = self.current_user
end
#User.stamper = self.current_user
#end
end
111 changes: 31 additions & 80 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MediaController < ApplicationController
include Cache, Spectrogram, Audio
include FileCacher

respond_to :xml, :json, :html, :png, :ogg, :oga, :webm, :webma, :mp3

Expand All @@ -9,96 +9,47 @@ def index
end

def item
# page for an individual media file
# controller action for an individual media file

@requested_format1 = request.format
@requested_format2 = params[:format]

respond_to do |format|
format.html { render 'item.html.erb'}
format.json { render 'item.html.erb' }
format.png { render 'item.html.erb' }
format.mp3 { render 'item.html.erb' }
format.oga { render 'item.html.erb' }
format.ogg { render 'item.html.erb' }
end


=begin
# determine the request format
if request.format == :html
# construct a hash of information to be returned and used for modify_parameters
@file_info = params
@file_info.delete 'controller'
@file_info.delete 'action'
@file_info.delete 'format'

elsif %w(png jpg).include? request.format
file_name = Cache::cached_spectrogram_file(params)
file_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,file_name)
# decide on the format requested. There are at least two ways to get the request format:
# request.format (created based on accept mime type) and params[:format] (from the file extension in the request)

elsif %w(ogg oga webm webma mp3).include? request.format
file_name = Cache::cached_audio_file(params)
file_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,file_name)
format_requested_from_accept = Mime::Type[request.format]
format_requested_from_ext = Mime::Type.lookup_by_extension(params[:format])

else
final_format_requested = format_requested_from_accept

end
image_media_types = [ Mime::Type['image/png'] ]
audio_media_types = [ Mime::Type['audio/webm'], Mime::Type['audio/webma'],
Mime::Type['audio/ogg'], Mime::Type['audio/oga'],
Mime::Type['audio/mp3'], Mime::Type['audio/mpeg'] ]
text_media_types = [ Mime::Type['application/json'], Mime::Type['text/html'],
Mime::Type['application/xhtml+xml'], Mime::Type['application/xml'],
Mime::Type['application/x-javascript'], Mime::Type['text/javascript'],
Mime::Type['text/x-javascript'],Mime::Type['text/x-json'] ]

# use the
@requested_format = request.format
return render

# use params to get query string or parsed route parameters
#@avail_params = params
# if the format is a supported image format, locate a cached spectrogram or generate it, then stream it back.
if image_media_types.include? final_format_requested
full_path = FileCacher::generate_spectrogram @file_info
send_file file_path, :stream => true, :buffer_size => 4096, :disposition => 'inline', :type => final_format_requested, :content_type => final_format_requested

if params.include? :format
elsif audio_media_types.include? final_format_requested
full_path = FileCacher::create_audio_segment @file_info
send_file file_path, :stream => true, :buffer_size => 4096, :disposition => 'inline', :type => final_format_requested, :content_type => final_format_requested

else
params[:format] = 'html'
end
elsif text_media_types.include? final_format_requested
respond_with @file_info

# if an image format was specified, get spectrogram information, otherwise audio
if %w(png jpg).include? params[:format] # if the extension is png or jpg, it is a spectrogram request
file_name = Cache::cached_spectrogram_file(params)
file_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,file_name)
else
file_name = Cache::cached_audio_file(params)
file_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,file_name)
end
# construct a hash of information to be returned
@file_info = params
@file_info.delete 'controller'
@file_info.delete 'action'
@file_info.delete 'format'
if file_paths.length > 0 && %w(html htm js json).include?(params[:format])
@file_info[:information] = Audio::info file_paths.first
# respond with a bad request
respond_with nil, { :head => :bad_request }
end
mime_type = Mime::Type.lookup_by_extension(params[:format])
# respond to the request
respond_with do |format|
format.html {
render
}
format.htm {
render 'index.html.erb'
}
format.json { render json: @file_info }
format.js { render json: @file_info }
format.mp3 { send_file_response file_paths, file_name, mime_type }
format.webma { send_file_response file_paths, file_name, mime_type }
format.webm { send_file_response file_paths, file_name, mime_type }
format.oga { send_file_response file_paths, file_name, mime_type }
format.ogg { send_file_response file_paths, file_name, mime_type }
format.png { send_file_response file_paths, file_name, mime_type }
end
=end
end

private

def send_file_response (file_path, file_name, mime_type)
#raise RuntimeError, "Can't find a source for file: #{file_name}" unless file_paths.length > 0
send_file file_path, :stream => true, :buffer_size => 4096, :disposition => 'inline', :type => mime_type, :content_type => mime_type
end
end
2 changes: 1 addition & 1 deletion app/models/audio_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AudioEvent < ActiveRecord::Base
accepts_nested_attributes_for :tags

# attr
attr_accessible :end_time_seconds, :high_frequency_hertz, :is_reference,
attr_accessible :audio_recording_id, :end_time_seconds, :high_frequency_hertz, :is_reference,
:low_frequency_hertz, :start_time_seconds

# userstamp
Expand Down
4 changes: 2 additions & 2 deletions app/models/audio_recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AudioRecording < ActiveRecord::Base

# attr
attr_accessible :bit_rate_bps, :channels, :data_length_bytes,
:duration_seconds, :hash, :media_type, :notes,
:duration_seconds, :media_data_hash, :media_type, :notes,
:recorded_date, :sample_rate_hertz, :status

# userstamp
Expand All @@ -37,7 +37,7 @@ class AudioRecording < ActiveRecord::Base
validates :media_type, :presence => true
validates :data_length_bytes, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }

validates :hash, :presence => true
validates :media_data_hash, :presence => true

# uuid stuff
attr_protected :uuid
Expand Down
4 changes: 2 additions & 2 deletions app/views/audio_events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<%= f.text_field :low_frequency_hertz %>
</div>
<div class="field">
<%= f.label :high_frequency %><br />
<%= f.text_field :high_frequency %>
<%= f.label :high_frequency_hertz %><br />
<%= f.text_field :high_frequency_hertz %>
</div>
<div class="field">
<%= f.label :is_reference %><br />
Expand Down
2 changes: 1 addition & 1 deletion app/views/audio_events/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<td><%= audio_event.start_time_seconds %></td>
<td><%= audio_event.end_time_seconds %></td>
<td><%= audio_event.low_frequency_hertz %></td>
<td><%= audio_event.high_frequency %></td>
<td><%= audio_event.high_frequency_hertz %></td>
<td><%= audio_event.is_reference %></td>
<td><%= link_to 'Show', audio_event %></td>
<td><%= link_to 'Edit', edit_audio_event_path(audio_event) %></td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/audio_events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
</p>

<p>
<b>High frequency:</b>
<%= @audio_event.high_frequency %>
<b>High frequency hertz:</b>
<%= @audio_event.high_frequency_hertz %>
</p>

<p>
Expand Down
4 changes: 2 additions & 2 deletions app/views/audio_recordings/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
<%= f.number_field :data_length_bytes %>
</div>
<div class="field">
<%= f.label :hash %><br />
<%= f.text_field :hash %>
<%= f.label :media_data_hash %><br />
<%= f.text_field :media_data_hash %>
</div>
<div class="field">
<%= f.label :status %><br />
Expand Down
4 changes: 2 additions & 2 deletions app/views/audio_recordings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<th>Bit rate bps</th>
<th>Media type</th>
<th>Data length bytes</th>
<th>Hash</th>
<th>Media Data Hash</th>
<th>Status</th>
<th>Notes</th>
<th></th>
Expand All @@ -30,7 +30,7 @@
<td><%= audio_recording.bit_rate_bps %></td>
<td><%= audio_recording.media_type %></td>
<td><%= audio_recording.data_length_bytes %></td>
<td><%= audio_recording.hash %></td>
<td><%= audio_recording.media_data_hash %></td>
<td><%= audio_recording.status %></td>
<td><%= audio_recording.notes %></td>
<td><%= link_to 'Show', audio_recording %></td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/audio_recordings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
</p>

<p>
<b>Hash:</b>
<%= @audio_recording.hash %>
<b>Media Data Hash:</b>
<%= @audio_recording.media_data_hash %>
</p>

<p>
Expand Down
3 changes: 3 additions & 0 deletions lib/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Exceptions
class SourceAudioFileNotFoundError < IOError; end
end
2 changes: 1 addition & 1 deletion lib/modules/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def self.build_parameters(parameter_names = {}, modify_parameters = {})
if param == :id
file_name += get_parameter(:id, modify_parameters, false)
elsif param == :format
file_name += '.'+get_parameter(:format, modify_parameters, false)
file_name += '.'+get_parameter(:format, modify_parameters, false).reverse.chomp('.').reverse
else
file_name += get_parameter(param, modify_parameters)
end
Expand Down
60 changes: 60 additions & 0 deletions lib/modules/file_cacher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module FileCacher
include Cache, Spectrogram, Audio

public

def self.generate_spectrogram(modify_parameters = {})
# first check if a cached spectrogram matches the request
target_file = Cache::cached_spectrogram_file modify_parameters
target_existing_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,target_file)

if target_existing_paths.blank?
# if no cached spectrogram images exist, try to create them from the cached audio
source_file = Cache::cached_audio_file(params)
source_existing_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,source_file)

if source_existing_paths.blank?
# if no cached audio files exist, try to create them
create_audio_segment modify_parameters
source_existing_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,source_file)
# raise an exception if the cached audio files could not be created
raise Exceptions::SourceAudioFileNotFoundError, "Could not generate spectrogram." if source_existing_paths.blank?
end

# create the spectrogram image in each of the possible paths
target_possible_paths = Cache::possible_paths(Cache::cached_spectrogram_storage_paths,target_file)
target_possible_paths.each { |path| Spectrogram::generate source_existing_paths.first, path, modify_parameters }
target_existing_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,target_file)
end

# the requested spectrogram image should exist in at least one possible path
# return the first existing full path
target_existing_paths.first
end

def self. create_audio_segment(modify_parameters = {})
# first check if a cached audio file matches the request
target_file = Cache::cached_audio_file modify_parameters
target_existing_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,target_file)

if target_existing_paths.blank?
# if no cached audio files exist, try to create them from the original audio
source_file = Cache::original_audio_file(params)
source_existing_paths = Cache::existing_paths(Cache::original_audio_storage_paths,source_file)

if source_existing_paths.blank?
# if the original audio files cannot be found, raise an exception
raise Exceptions::SourceAudioFileNotFoundError, "Could not find original audio file." if source_existing_paths.blank?
end

# create the cached audio file in each of the possible paths
target_possible_paths = Cache::possible_paths(Cache::cached_audio_storage_paths,target_file)
target_possible_paths.each { |path| Audio::modify source_existing_paths.first, path, modify_parameters }
target_existing_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,target_file)
end

# the requested audio file should exist in at least one possible path
# return the first existing full path
target_existing_paths.first
end
end
2 changes: 2 additions & 0 deletions lib/modules/spectrogram.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'open3'

module Spectrogram
@sox_path = if OS.windows? then "./vendor/bin/sox/windows/sox.exe" else "sox" end
@sox_arguments_verbose = "-V"
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/uuid_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.included(base)
before_validation :set_uuid, :on => :create

def set_uuid
self.uuid = UUID.timestamp_create.to_s
self.uuid = UUIDTools::UUID.timestamp_create.to_s
end
end
end
Expand Down

0 comments on commit 623cee6

Please sign in to comment.