From 0218ca5ad69ad7d0c194e4d883e2d67bbf26cb7f Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 17 Feb 2014 19:12:52 +0000 Subject: [PATCH] * urls fixed for VRFS-1148 --- ruby/lib/jam_ruby/models/claimed_recording.rb | 10 +++++++ ruby/lib/jam_ruby/models/mix.rb | 13 +++++++-- ruby/lib/jam_ruby/models/recorded_track.rb | 2 +- .../api_claimed_recordings_controller.rb | 17 +++++++++-- web/app/controllers/api_feeds_controller.rb | 1 - .../views/api_claimed_recordings/show.rabl | 29 ++++++++----------- web/app/views/api_feeds/show.rabl | 29 +++++++++---------- web/app/views/api_recordings/show.rabl | 27 +++++++---------- web/config/routes.rb | 3 +- 9 files changed, 73 insertions(+), 58 deletions(-) diff --git a/ruby/lib/jam_ruby/models/claimed_recording.rb b/ruby/lib/jam_ruby/models/claimed_recording.rb index 4b653522c..13870f433 100644 --- a/ruby/lib/jam_ruby/models/claimed_recording.rb +++ b/ruby/lib/jam_ruby/models/claimed_recording.rb @@ -58,6 +58,16 @@ module JamRuby end end + + def has_mix? + recording.mixes.length > 0 && recording.mixes.first.completed + end + + def can_download?(some_user) + !ClaimedRecording.find_by_user_id_and_recording_id(some_user.id, recording_id).nil? + end + + def remove_non_alpha_num(token) token.gsub(/[^0-9A-Za-z]/, '') end diff --git a/ruby/lib/jam_ruby/models/mix.rb b/ruby/lib/jam_ruby/models/mix.rb index 0ed02ad13..216145a2e 100644 --- a/ruby/lib/jam_ruby/models/mix.rb +++ b/ruby/lib/jam_ruby/models/mix.rb @@ -118,20 +118,27 @@ module JamRuby completed end + # if the url starts with http, just return it because it's in some other store. Otherwise it's a relative path in s3 and needs be signed + def resolve_url(url_field, mime_type, expiration_time) + self[url_field].start_with?('http') ? self[url_field] : s3_manager.sign_url(self[url_field], {:expires => expiration_time, :response_content_type => mime_type, :secure => false}) + end + def sign_url(expiration_time = 120, type='ogg') + type ||= 'ogg' # expire link in 1 minute--the expectation is that a client is immediately following this link if type == 'ogg' - s3_manager.sign_url(self[:ogg_url], {:expires => expiration_time, :response_content_type => 'audio/ogg', :secure => false}) + resolve_url(:ogg_url, 'audio/ogg', expiration_time) else - s3_manager.sign_url(self[:mp3_url], {:expires => expiration_time, :response_content_type => 'audio/mp3', :secure => false}) + resolve_url(:mp3_url, 'audio/mpeg', expiration_time) end end def sign_put(expiration_time = 3600 * 24, type='ogg') + type ||= 'ogg' if type == 'ogg' s3_manager.sign_url(self[:ogg_url], {:expires => expiration_time, :content_type => 'audio/ogg', :secure => false}, :put) else - s3_manager.sign_url(self[:mp3_url], {:expires => expiration_time, :content_type => 'audio/mp3', :secure => false}, :put) + s3_manager.sign_url(self[:mp3_url], {:expires => expiration_time, :content_type => 'audio/mpeg', :secure => false}, :put) end end diff --git a/ruby/lib/jam_ruby/models/recorded_track.rb b/ruby/lib/jam_ruby/models/recorded_track.rb index 21e15cf6e..ee0cefa08 100644 --- a/ruby/lib/jam_ruby/models/recorded_track.rb +++ b/ruby/lib/jam_ruby/models/recorded_track.rb @@ -158,7 +158,7 @@ module JamRuby end def upload_sign(content_md5) - s3_manager.upload_sign(self[:url], content_md5, next_part_to_upload, upload_id) + s3_manager.upload_sign(url, content_md5, next_part_to_upload, upload_id) end def upload_part_complete(part, offset) diff --git a/web/app/controllers/api_claimed_recordings_controller.rb b/web/app/controllers/api_claimed_recordings_controller.rb index 72d91750d..471f76ae2 100644 --- a/web/app/controllers/api_claimed_recordings_controller.rb +++ b/web/app/controllers/api_claimed_recordings_controller.rb @@ -1,7 +1,7 @@ class ApiClaimedRecordingsController < ApiController - before_filter :api_signed_in_user - before_filter :look_up_claimed_recording, :only => [ :show, :update, :delete ] + before_filter :api_signed_in_user, :except => [ :download ] + before_filter :look_up_claimed_recording, :only => [ :show, :update, :delete, :download ] respond_to :json @@ -44,7 +44,18 @@ class ApiClaimedRecordingsController < ApiController #render :json => { :message => "claimed_recording could not be deleted" }, :status => 403 #end end - + + def download + if !@claimed_recording.is_public && @claimed_recording.user_id != current_user.id + raise PermissionError, 'this claimed recording is not public' + end + + mix = @claimed_recording.recording.mixes.first! + + params[:type] ||= 'ogg' + redirect_to mix.sign_url(120, params[:type]) + end + private def look_up_claimed_recording diff --git a/web/app/controllers/api_feeds_controller.rb b/web/app/controllers/api_feeds_controller.rb index 1de227d7b..0695fb8d2 100644 --- a/web/app/controllers/api_feeds_controller.rb +++ b/web/app/controllers/api_feeds_controller.rb @@ -26,5 +26,4 @@ class ApiFeedsController < ApiController render "api_feeds/index", :layout => nil end - end \ No newline at end of file diff --git a/web/app/views/api_claimed_recordings/show.rabl b/web/app/views/api_claimed_recordings/show.rabl index 56b25aac3..69ff50f5d 100644 --- a/web/app/views/api_claimed_recordings/show.rabl +++ b/web/app/views/api_claimed_recordings/show.rabl @@ -22,26 +22,10 @@ child(:recording => :recording) { child(:owner => :owner) { attributes :id, :name, :location, :photo_url } - - child(:mixes => :mixes) { - attributes :id, :is_completed - - node :mp3_url do |mix| - mix[:mp3_url] - end - - node :ogg_url do |mix| - mix[:ogg_url] - end - } child(:recorded_tracks => :recorded_tracks) { attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id - node :url do |recorded_track| - recorded_track[:url] - end - child(:user => :user) { attributes :id, :first_name, :last_name, :name, :city, :state, :country, :location, :photo_url } @@ -54,4 +38,15 @@ child(:recording => :recording) { attributes :id, :first_name, :last_name, :photo_url } } -} \ No newline at end of file +} + +if :has_mix? + node do |claimed_recording| + { + mix: { + mp3_url: claimed_recording_download_url(claimed_recording.id, 'mp3'), + ogg_url: claimed_recording_download_url(claimed_recording.id, 'ogg') + } + } + end +end \ No newline at end of file diff --git a/web/app/views/api_feeds/show.rabl b/web/app/views/api_feeds/show.rabl index cc2f58239..c266662bc 100644 --- a/web/app/views/api_feeds/show.rabl +++ b/web/app/views/api_feeds/show.rabl @@ -36,25 +36,9 @@ glue :recording do attributes :id, :name, :location, :photo_url } - child(:mixes => :mixes) { - attributes :id, :is_completed - - node :mp3_url do |mix| - mix[:mp3_url] - end - - node :ogg_url do |mix| - mix[:ogg_url] - end - } - child(:recorded_tracks => :recorded_tracks) { attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id - node :url do |recorded_track| - recorded_track[:url] - end - child(:user => :user) { attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url } @@ -77,6 +61,19 @@ glue :recording do share_token_url(claimed_recording.share_token.token) end end + + if :has_mix? + node do |claimed_recording| + { + mix: { + mp3_url: claimed_recording_download_url(claimed_recording.id, 'mp3'), + ogg_url: claimed_recording_download_url(claimed_recording.id, 'ogg') + } + } + end + end + + } end diff --git a/web/app/views/api_recordings/show.rabl b/web/app/views/api_recordings/show.rabl index 79c73f610..23dd3ef6c 100644 --- a/web/app/views/api_recordings/show.rabl +++ b/web/app/views/api_recordings/show.rabl @@ -10,25 +10,9 @@ child(:owner => :owner) { attributes :id, :name, :location, :photo_url } -child(:mixes => :mixes) { - attributes :id, :is_completed - - node :mp3_url do |mix| - mix[:mp3_url] - end - - node :ogg_url do |mix| - mix[:ogg_url] - end -} - child(:recorded_tracks => :recorded_tracks) { attributes :id, :fully_uploaded, :client_track_id, :client_id, :instrument_id - node :url do |recorded_track| - recorded_track[:url] - end - child(:user => :user) { attributes :id, :first_name, :last_name, :city, :state, :country, :location, :photo_url } @@ -51,4 +35,15 @@ child(:claimed_recordings => :claimed_recordings) { share_token_url(claimed_recording.share_token.token) end end + + if :has_mix? + node do |claimed_recording| + { + mix: { + mp3_url: claimed_recording_download_url(claimed_recording.id, 'mp3'), + ogg_url: claimed_recording_download_url(claimed_recording.id, 'ogg') + } + } + end + end } diff --git a/web/config/routes.rb b/web/config/routes.rb index bcc82920c..a37c1f78f 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -314,11 +314,12 @@ SampleApp::Application.routes.draw do match '/claimed_recordings/:id' => 'api_claimed_recordings#show', :via => :get match '/claimed_recordings/:id' => 'api_claimed_recordings#update', :via => :put match '/claimed_recordings/:id' => 'api_claimed_recordings#delete', :via => :delete + match '/claimed_recordings/:id/download(/:type)' => 'api_claimed_recordings#download', :via => :get, :as => :claimed_recording_download # Mixes match '/mixes/schedule' => 'api_mixes#schedule', :via => :post match '/mixes/next' => 'api_mixes#next', :via => :get - match '/mixes/:id/download' => 'api_mixes#download', :via => :get + match '/mixes/:id/download(/:type)' => 'api_mixes#download', :via => :get, :as => :mix_download # version check for JamClient match '/versioncheck' => 'artifacts#versioncheck'