diff --git a/db/manifest b/db/manifest index 3e10c271f..4d6757f21 100755 --- a/db/manifest +++ b/db/manifest @@ -373,4 +373,4 @@ connection_role.sql retailer_payment_split.sql teacher_distribution_fields.sql jam_track_download_rights.sql -json_stores_type_index.sql +mobile_recording_support.sql diff --git a/db/up/json_stores_type_index.sql b/db/up/json_stores_type_index.sql deleted file mode 100644 index c6d731ff4..000000000 --- a/db/up/json_stores_type_index.sql +++ /dev/null @@ -1 +0,0 @@ -CREATE INDEX json_stores_type ON json_stores USING btree(type); diff --git a/db/up/mobile_recording_support.sql b/db/up/mobile_recording_support.sql new file mode 100644 index 000000000..0e6c248c2 --- /dev/null +++ b/db/up/mobile_recording_support.sql @@ -0,0 +1,15 @@ +-- the type column should be indexed +CREATE INDEX json_stores_type ON json_stores USING btree(type); + +-- mobile recording media S3 upload +CREATE TABLE mobile_recording_uploads ( + id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), + mobile_recording_id varchar(64) NOT NULL, + file_url varchar(1024) DEFAULT NULL, + file_name varchar(255) DEFAULT NULL, + size integer, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX mobile_recording_id_idx ON mobile_recording_uploads USING btree(mobile_recording_id); diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 1ac125372..783d534a4 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -318,6 +318,8 @@ require "jam_ruby/models/teacher_genre" require "jam_ruby/models/jam_class_report" require "jam_ruby/models/campaign_spend" require "jam_ruby/models/mobile_recording" +require "jam_ruby/app/uploaders/mobile_recording_uploader" +require "jam_ruby/models/mobile_recording_upload" include Jampb module JamRuby diff --git a/ruby/lib/jam_ruby/app/uploaders/mobile_recording_uploader.rb b/ruby/lib/jam_ruby/app/uploaders/mobile_recording_uploader.rb new file mode 100644 index 000000000..6897a51f5 --- /dev/null +++ b/ruby/lib/jam_ruby/app/uploaders/mobile_recording_uploader.rb @@ -0,0 +1,25 @@ +# encoding: utf-8 + +class MobileRecordingUploader < CarrierWave::Uploader::Base + + def initialize(*) + super + JamRuby::UploaderConfiguration.set_aws_private_configuration(self) + end + + def store_dir + nil + end + + def md5 + @md5 ||= ::Digest::MD5.file(current_path).hexdigest + end + + def filename + model.filename if model.id + end + + def extension_white_list + %w(aac m4a mp3) + end +end diff --git a/ruby/lib/jam_ruby/models/mobile_recording.rb b/ruby/lib/jam_ruby/models/mobile_recording.rb index 79ac04533..3cb19560a 100644 --- a/ruby/lib/jam_ruby/models/mobile_recording.rb +++ b/ruby/lib/jam_ruby/models/mobile_recording.rb @@ -4,5 +4,9 @@ module JamRuby # this class keeps backups of mobile recording jobs belongs_to :recording, class_name: 'JamRuby::Recording', foreign_key: :foreign_key1_id + has_one :mobile_recording_upload, + class_name: 'JamRuby::MobileRecordingUpload', + foreign_key: :mobile_recording_id + end end diff --git a/ruby/lib/jam_ruby/models/mobile_recording_upload.rb b/ruby/lib/jam_ruby/models/mobile_recording_upload.rb new file mode 100644 index 000000000..ed25b8204 --- /dev/null +++ b/ruby/lib/jam_ruby/models/mobile_recording_upload.rb @@ -0,0 +1,55 @@ +module JamRuby + class MobileRecordingUpload < ActiveRecord::Base + include JamRuby::S3ManagerMixin + + self.table_name = 'mobile_recording_uploads' + self.primary_key = 'id' + + RECORDING_FILE_DIR = "mobile_recording_uploads" + + attr_accessible :file_url, :size, :file_name + + belongs_to :mobile_recording, + class_name: "JamRuby::MobileRecording", + foreign_key: :mobile_recording_id + + mount_uploader :file_url, MobileRecordingUploader + + before_destroy :delete_s3_files + + validates :size, :presence => true + + def self.create(mobile_recording, file, file_name) + mru = self.new + mru.file_name = file_name + mru.mobile_recording = mobile_recording + mru.size = file.size + + # save first to get a valid created_at time + mru.save! + + # now that the model exists (created_at exists), we can save the file in the correct path + mru.file_url = file + mru.save + mru + end + + def filename + self.class.construct_filename(self) + end + + def sign_url(expiration_time = 120) + s3_manager.sign_url(self[:file_url], {:expires => expiration_time, :secure => true}) + end + + private + + def self.construct_filename(mru) + "#{RECORDING_FILE_DIR}/#{mru.created_at.strftime('%Y%m%d%H%M%S')}/#{mru.mobile_recording.user_id}/#{mru.file_name}" + end + + def delete_s3_files + s3_manager({:public => true}).delete(self[:file_url]) if self[:file_url] + end + end +end diff --git a/ruby/spec/jam_ruby/models/mobile_recording_upload_spec.rb b/ruby/spec/jam_ruby/models/mobile_recording_upload_spec.rb new file mode 100644 index 000000000..400bd14b2 --- /dev/null +++ b/ruby/spec/jam_ruby/models/mobile_recording_upload_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' +require 'digest/md5' + +# NOTE this was cloned from music_notation_spec.rb + +describe MobileRecordingUpload do + + include UsesTempFiles + + MRU_TEMP_FILE='detail.png' + + in_directory_with_file(MRU_TEMP_FILE) + + before do + content_for_file("this is mobile recording test file") + end + + it "return empty" do + MobileRecordingUpload.all.length.should == 0 + end + + + it "should allow insertion" do + mru = MobileRecordingUpload.new + mru.file_url = File.open(MRU_TEMP_FILE) + mru.size = File.size(MRU_TEMP_FILE) + + mr = MobileRecording.new + mr.user = FactoryGirl.create(:user) + mr.save! + + mru.mobile_recording = mr + mru.save! + + File.basename(mru.file_url.path).should == mr.user_id + mru.size.should == File.size(MRU_TEMP_FILE) + + stub_const("APP_CONFIG", app_config) + mru.sign_url.should_not be_nil + + end +end diff --git a/web/app/controllers/api_recordings_controller.rb b/web/app/controllers/api_recordings_controller.rb index db91059dc..fc87630c0 100644 --- a/web/app/controllers/api_recordings_controller.rb +++ b/web/app/controllers/api_recordings_controller.rb @@ -1,7 +1,7 @@ class ApiRecordingsController < ApiController before_filter :api_signed_in_user, :except => [ :add_like ] - before_filter :lookup_recording, :only => [ :show, :stop, :claim, :discard, :keep, :delete_claim, :add_timeline, :add_video_data, :delete_video_data, :mobile_update ] + before_filter :lookup_recording, :only => [ :show, :stop, :claim, :discard, :keep, :delete_claim, :add_timeline, :add_video_data, :delete_video_data, :mobile_upload, :mobile_update, :mobile_upload_download, :mobile_upload_delete ] before_filter :lookup_recorded_track, :only => [ :download, :upload_next_part, :upload_sign, :upload_part_complete, :upload_complete ] before_filter :lookup_recorded_backing_track, :only => [ :backing_track_download, :backing_track_upload_next_part, :backing_track_upload_sign, :backing_track_upload_part_complete, :backing_track_upload_complete ] before_filter :lookup_recorded_video, :only => [ :video_upload_sign, :video_upload_start, :video_upload_complete ] @@ -13,6 +13,42 @@ class ApiRecordingsController < ApiController @log || Logging.logger[ApiRecordingsController] end + def mobile_upload + mobile_rec = @recording.mobile_recording + + if mobile_rec + mru = MobileRecordingUpload.create(mobile_rec, request.body, params[:file_name]) + + unless mobile_rec.errors.any? + render :json => @recording, :status => 200 + return + end + end + response.status = :unprocessable_entity + render nothing: true + end + + def mobile_upload_download + mobile_rec = @recording.mobile_recording + if mobile_rec && mobile_rec.mobile_recording_upload + redirect_to mobile_rec.mobile_recording_upload.sign_url + return + end + response.status = :unprocessable_entity + render nothing: true + end + + def mobile_upload_delete + mobile_rec = @recording.mobile_recording + if mobile_rec + mobile_rec.mobile_recording_upload.try(:destroy) + render :json => {}, status: 204 + return + end + response.status = :unprocessable_entity + render nothing: true + end + def mobile_update mobile_rec = @recording.mobile_recording diff --git a/web/config/routes.rb b/web/config/routes.rb index fa0b5b95e..5831ceabe 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -601,7 +601,11 @@ Rails.application.routes.draw do match '/recordings/:id/video_data' => 'api_recordings#add_video_data', :via => :post, :as => 'api_recordings_video_data' match '/recordings/:id/video_data' => 'api_recordings#delete_video_data', :via => :delete, :as => 'api_recordings_video_data_delete' match '/recordings' => 'api_recordings#create_immediately', :via => :post + match '/recordings/:id/mobile_update' => 'api_recordings#mobile_update', :via => :post, :as => 'api_recordings_mobile_update' + match '/recordings/:id/mobile_upload' => 'api_recordings#mobile_upload', :via => :post, :as => 'api_recordings_mobile_upload' + match '/recordings/:id/mobile_upload' => 'api_recordings#mobile_upload_download', :via => :get, :as => :download_mobile_recording + match '/recordings/:id/mobile_upload' => 'api_recordings#mobile_upload_delete', :via => :delete, :as => :delete_mobile_recording # Recordings - recorded_tracks