diff --git a/db/manifest b/db/manifest index 165349992..5d4ca82e9 100755 --- a/db/manifest +++ b/db/manifest @@ -374,4 +374,5 @@ retailer_payment_split.sql teacher_distribution_fields.sql jam_track_download_rights.sql guitar_center_integration_v1.sql -youtube_broadcast.sql \ No newline at end of file +mobile_recording_support.sql +youtube_broadcast.sql 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 a4ec430c3..6f007d871 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -319,6 +319,9 @@ require "jam_ruby/models/teacher_language" 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/jam_track_importer.rb b/ruby/lib/jam_ruby/jam_track_importer.rb index 8111b415d..2bf514679 100644 --- a/ruby/lib/jam_ruby/jam_track_importer.rb +++ b/ruby/lib/jam_ruby/jam_track_importer.rb @@ -2949,14 +2949,19 @@ module JamRuby master_track = jam_track.master_track if master_track - Dir.mktmpdir do |tmp_dir| - ogg_44100 = File.join(tmp_dir, 'input.ogg') - private_s3_manager.download(master_track.url_by_sample_rate(44), ogg_44100) + begin + Dir.mktmpdir do |tmp_dir| + ogg_44100 = File.join(tmp_dir, 'input.ogg') + private_s3_manager.download(master_track.url_by_sample_rate(44), ogg_44100) - if importer.synchronize_duration(jam_track, ogg_44100) - jam_track.save! - importer.finish("success", nil) + if importer.synchronize_duration(jam_track, ogg_44100) + jam_track.save! + importer.finish("success", nil) + end end + rescue + logger.error("ERROR: Import failed: "+$!.to_s) + importer.finish('no_duration', nil) end else importer.finish('no_duration', nil) diff --git a/ruby/lib/jam_ruby/models/mobile_recording.rb b/ruby/lib/jam_ruby/models/mobile_recording.rb new file mode 100644 index 000000000..3cb19560a --- /dev/null +++ b/ruby/lib/jam_ruby/models/mobile_recording.rb @@ -0,0 +1,12 @@ +module JamRuby + class MobileRecording < JsonStore + + # 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..259236b18 --- /dev/null +++ b/ruby/lib/jam_ruby/models/mobile_recording_upload.rb @@ -0,0 +1,59 @@ +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! + + # CarrierWave says we need a file extension, doesn't get provided + tmpfile = Tempfile.new([file_name, File.extname(file_name)]) + tmpfile.write(file) + + # now that the model exists (created_at exists), we can save the file in the correct path + mru.file_url = tmpfile + 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/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index dbfd2e01e..348ab6de5 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -27,6 +27,11 @@ module JamRuby accepts_nested_attributes_for :recorded_tracks, :mixes, :claimed_recordings, allow_destroy: true + has_one :mobile_recording, + class_name: 'JamRuby::MobileRecording', + foreign_key: :foreign_key1_id, + dependent: :destroy + validate :not_already_recording, :on => :create validate :not_still_finalizing_previous, :on => :create validate :not_playback_recording, :on => :create 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/assets/javascripts/react-components/PopupRecordingStartStop.js.jsx.coffee b/web/app/assets/javascripts/react-components/PopupRecordingStartStop.js.jsx.coffee index 5b95b64cb..354f80cdc 100644 --- a/web/app/assets/javascripts/react-components/PopupRecordingStartStop.js.jsx.coffee +++ b/web/app/assets/javascripts/react-components/PopupRecordingStartStop.js.jsx.coffee @@ -4,6 +4,8 @@ logger = context.JK.logger NoVideoRecordActive = 0 WebCamRecordActive = 1 ScreenRecordActive = 2 +WebCam2RecordActive = 3 +DesktopRecordActive = 4 mixins = [] @@ -53,10 +55,16 @@ if accessOpener if @inputType != 'audio-only' - if $root.find('#recording-selection').val() == 'video-window' + selection = $root.find('#recording-selection').val() + + if selection == 'video-window' recordVideo = ScreenRecordActive - else + else if selection == 'webcam-only' recordVideo = WebCamRecordActive + else if selection == 'webcam-only-2' + recordVideo = WebCam2RecordActive + else + recordVideo = DesktopRecordActive recordChat = $root.find('#include-chat').is(':checked') @@ -123,6 +131,8 @@ if accessOpener diff --git a/web/app/controllers/api_jam_tracks_controller.rb b/web/app/controllers/api_jam_tracks_controller.rb index 13a6723a6..ff7cbf6f2 100644 --- a/web/app/controllers/api_jam_tracks_controller.rb +++ b/web/app/controllers/api_jam_tracks_controller.rb @@ -102,7 +102,6 @@ class ApiJamTracksController < ApiController if params[:mixcheck] checksum = JamTrackMixdown.mixdownChecksum(current_user.id, jtid) head :ok, checksum: checksum - return else # 204: nothing purchased for user # 200: jamtrack purchase for user confirmed @@ -111,21 +110,12 @@ class ApiJamTracksController < ApiController .limit(1) .blank? head(:no_content) - return end end - elsif params[:syncbuys] - syncbuys = params[:syncbuys].to_i - latestPurchase = JamTrack.latestPurchase(current_user.id) - if 0 == syncbuys || (0 < latestPurchase && (latestPurchase <= syncbuys)) - head :no_content, latestpurchase: latestPurchase - return - else - head :ok, latestpurchase: latestPurchase - return - end + elsif params[:syncbuys].present? + head :ok, purchasecount: JamTrackRight.where(user_id: current_user.id).count end - head(:ok) + head(:ok) unless performed? end def purchased diff --git a/web/app/controllers/api_recordings_controller.rb b/web/app/controllers/api_recordings_controller.rb index dc51b2166..38447f664 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 ] + 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,86 @@ 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 + + if mobile_rec + mobile_rec.data_blob = JSON.parse(request.body.read) + mobile_rec.save + + unless mobile_rec.errors.any? + render :json => @recording, :status => 200 + return + end + end + response.status = :unprocessable_entity + render nothing: true + end + + def create + if request.headers['Jamk-Mobile-Env'] + json = JSON.parse(request.body.read) + + record_params = { + name: json['title'], + description: json['description'], + genre: json['genre']['id'], + record_video: json['isVideo'], + is_public: false, # NOTE: figure out why we need this one and the yt one + upload_to_youtube: false, + } + result = Recording.create_immediately(current_user, record_params) + unless result.errors.any? + + mr = MobileRecording.new + mr.data_blob = json + mr.recording = result + mr.save! + + render :json => result, :status => 200 + return + end + end + response.status = :unprocessable_entity + render nothing: true + end + def index # lists recordings created by for the current user @recordings = Recording.list_recordings(current_user, params[:created_by]) diff --git a/web/config/routes.rb b/web/config/routes.rb index 76f12e2e6..94718c3aa 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -590,6 +590,7 @@ Rails.application.routes.draw do match '/recordings/uploads' => 'api_recordings#list_uploads', :via => :get, :as => 'api_recordings_list_uploads' match '/recordings/downloads' => 'api_recordings#list_downloads', :via => :get, :as => 'api_recordings_list_downloads' match '/recordings/start' => 'api_recordings#start', :via => :post, :as => 'api_recordings_start' + match '/recordings/create' => 'api_recordings#create', :via => :post, :as => 'api_recordings_create' match '/recordings/:id' => 'api_recordings#show', :via => :get, :as => 'api_recordings_detail' match '/recordings/:id/stop' => 'api_recordings#stop', :via => :post, :as => 'api_recordings_stop' match '/recordings/:id/claim' => 'api_recordings#claim', :via => :post, :as => 'api_recordings_claim' @@ -602,6 +603,11 @@ Rails.application.routes.draw do 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 match '/recordings/:id/tracks/:track_id' => 'api_recordings#show_recorded_track', :via => :get, :as => 'api_recordings_show_recorded_track'