Skip to content

Commit

Permalink
Cache module tests. Gems added. Settings updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark committed Oct 26, 2012
1 parent c8be7e3 commit cdd5cea
Show file tree
Hide file tree
Showing 5 changed files with 600 additions and 10 deletions.
9 changes: 8 additions & 1 deletion app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ def item
# use params to get query string or parsed route parameters

# get the path for the file matching the request
@file_path = Cache::calculate_target_path(params)
file_name = Cache::cached_spectrogram_file(params)
#file_name = Cache::cached_audio_file(params)
#file_name = Cache::original_audio_file({ :id => '21EC2020-3AEA-1069-A2DD-08002B30309D', :date => '20121026', :time => '132600', :format => 'wav'})

#@file_path = Cache::possible_paths(file_name)
#@file_path = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,file_name)
#@file_path = QubarSite::Application.config.media_file_config.cached_spectrogram_paths
@file_path = Cache::possible_paths(Cache::cached_spectrogram_storage_paths,file_name)

# see if the requested file exists

Expand Down
116 changes: 109 additions & 7 deletions lib/modules/cache.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,119 @@
require 'String'

# Determines file names for cached and original files.
module Cache

@parameter_file_name_separator = '_'

public

# calculate the target file name and path based on the modification parameters
def self.calculate_target_path(modify_parameters = {})
#Mime::Type.lookup_by_extension(
#QubarSite::Application.config.media_file_config
#original audio

# get the file name for an original audio file
def self.original_audio_file(modify_parameters = {})
file_name = self.build_parameters [ :id, :date, :time, :format ], modify_parameters
file_name
end

# get all the storage paths for original audio
def self.original_audio_storage_paths()
storage_paths = QubarSite::Application.config.media_file_config.original_audio_paths
storage_paths
end

# cached audio

# get the file name for a cached audio file
def self.cached_audio_file(modify_parameters = {})
file_name = self.build_parameters [ :id, :start_offset, :end_offset, :channel, :sample_rate, :format ], modify_parameters
file_name
end

# get all the storage paths for cached audio
def self.cached_audio_storage_paths()
storage_paths = QubarSite::Application.config.media_file_config.cached_audio_paths
storage_paths
end

def self. cached_audio_defaults()
cache_defaults = QubarSite::Application.config.media_file_config.cached_audio_defaults
cache_defaults
end

def self. cached_spectrogram_defaults()
cache_defaults = QubarSite::Application.config.media_file_config.cached_spectrogram_defaults
cache_defaults
end

# check to see if a target file exists
def self.exists(source, modify_parameters = {})
File.exists? calculate_target_path(source, modify_parameters)
# cached spectrograms

# get the file name for a cached spectrogram
def self.cached_spectrogram_file(modify_parameters = {})
file_name = self.build_parameters [ :id, :start_offset, :end_offset, :channel, :sample_rate, :window, :colour, :format ], modify_parameters
file_name
end

# get all the storage paths for cached spectrograms
def self.cached_spectrogram_storage_paths()
storage_paths = QubarSite::Application.config.media_file_config.cached_spectrogram_paths
storage_paths
end

# get all possible full paths for a file
def self.possible_paths(storage_paths, file_name)
possible_paths = storage_paths.collect { |path| File.join(path,file_name) }
possible_paths
end

# get the full paths for all existing files that match a file name
def self.existing_paths(storage_paths, file_name)
existing_paths = possible_paths(storage_paths, file_name).find_all {|file| File.exists? file }
existing_paths
end

private

def self.build_parameters(parameter_names = {}, modify_parameters = {})
file_name = ''

parameter_names.each do |param|
if param == :id
file_name += get_parameter(:id, modify_parameters, false)
elsif param == :format
file_name += '.'+get_parameter(:format, modify_parameters, false)
else
file_name += get_parameter(param, modify_parameters)
end
end

file_name
end

def self.get_parameter(parameter, modify_parameters, include_separator = true)
# need to cater for the situation where modify_parameters contains strings (we want symbols)
modify_parameters.keys.each do |key|
modify_parameters[(key.to_sym rescue key) || key] = modify_parameters.delete(key)
end

# need to cater for the situation where parameter is a string (we want a symbol)
parameter = parameter.to_s.to_sym

raise ArgumentError, "Parameters must include #{parameter}." unless modify_parameters.include? parameter
result_name = ''

if modify_parameters.include? parameter
result_name = modify_parameters[parameter]

if parameter == :format
result_name = result_name.trim '.', ''
end

if include_separator
result_name = @parameter_file_name_separator+result_name
end
end

result_name
end

end
4 changes: 2 additions & 2 deletions lib/modules/string.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class String
def trim(replace_chars, substitute_chars)
"#{self}".gsub(/^[#{substitute_chars}]+|[#{substitute_chars}]+$/, replace_chars)
def trim(chars_to_replace, char_to_insert)
"#{self}".gsub(/^[#{chars_to_replace}]+|[#{chars_to_replace}]+$/, char_to_insert)
end
end
Binary file added public/tests/TorresianCrow.wav
Binary file not shown.
Loading

0 comments on commit cdd5cea

Please sign in to comment.