diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb index b42796a5..7c0c918e 100644 --- a/app/controllers/media_controller.rb +++ b/app/controllers/media_controller.rb @@ -1,100 +1,104 @@ class MediaController < ApplicationController include Cache, Spectrogram, Audio - respond_to :xml, :json, :html, :png + respond_to :xml, :json, :html, :png, :ogg, :oga, :webm, :webma, :mp3 def index # index page for media files - @testing = QubarSite::Application.config.media_file_config + #@testing = QubarSite::Application.config.media_file_config end def item # page 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 + + 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) + + 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) + + else + + end + + # use the + @requested_format = request.format + return render + # use params to get query string or parsed route parameters - - @avail_params = params - - # see if a file extension was specified. - # if none was specified, default to html - if !params.include? 'format' + #@avail_params = params + + if params.include? :format + + else params[:format] = 'html' end - - if params[:format] == 'png' # if the extension is png, it is a spectrogram request + + # 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::possible_paths(Cache::cached_spectrogram_storage_paths,file_name) - - elsif ['mp3', 'webm', 'webma', 'ogg', 'oga'].include? params[:format] # if the extension is mp3, webma, ogg, it is an audio request - file_name = Cache::cached_audio_file(params) - file_paths = Cache::possible_paths(Cache::cached_audio_storage_paths,file_name) + file_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,file_name) else - # invalid request, what to do? + file_name = Cache::cached_audio_file(params) + file_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,file_name) end - - @file_paths = file_paths - - mimeType = Mime::Type.lookup_by_extension(params[:format]) -#=begin + + # 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 + 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: params } - format.js { render json: params } - format.mp3 { send_file_response @file_paths, mimeType } - format.webma { send_file_response @file_paths, mimeType } - format.webm { send_file_response @file_paths, mimeType } - format.oga { send_file_response @file_paths, mimeType } - format.ogg { send_file_response @file_paths, mimeType } - format.png { send_file_response @file_paths, mimeType } + 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 - #render :formats => [:html] +=end end private - def send_file_response (file_paths, mime_type) - send_file file_paths[0], :stream => true, :buffer_size => 4096, :disposition => 'inline', :type => mime_type - end - - def info - @input_path = './test/fixtures/' - # not-an-audio-file.wav - # TorresianCrow.wav - # TestAudio1.wv - # sites.yml - # this file does not exist.nope - @audio = 'TestAudio1.wv' - @input_audio = @input_path + @audio - @result = Audio::info(@input_audio) - end - def audio - print params - - @input_path = './test/fixtures/' - @output_path = './public/tests/' - - @audio = 'TestAudio1.wv' - @modified_audio = 'TestAudio1.wav' - - @input_audio = @input_path + @audio - @output_audio = @output_path + @modified_audio - - - #@result = Audio::modify(@input_audio, @output_audio, []) - end - def spectrogram - print params - @input_path = './test/fixtures/' - @output_path = './public/tests/' - - @audio = 'TorresianCrow.wav' - @image = 'TorresianCrow.png' - - @input_audio = @input_path + @audio - @output_image = @output_path + @image - - #@result = Spectrogram::generate(@input_audio, @output_image) + 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 \ No newline at end of file diff --git a/app/views/media/index.html.erb b/app/views/media/index.html.erb index 4b560577..461a9934 100644 --- a/app/views/media/index.html.erb +++ b/app/views/media/index.html.erb @@ -1,3 +1,2 @@
Index page for media controller.
-<%= @testing %>
\ No newline at end of file +Index page for media controller.
\ No newline at end of file diff --git a/app/views/media/item.html.erb b/app/views/media/item.html.erb index 5a4ede73..487bcf3f 100644 --- a/app/views/media/item.html.erb +++ b/app/views/media/item.html.erb @@ -1,4 +1,6 @@Individual item page for media controller.
-<%= @avail_params %>
-<%= @file_paths %>
\ No newline at end of file +