From 0522c20e608ed74efea379be10834c1920000f77 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 24 Feb 2014 16:55:56 +0000 Subject: [PATCH] * VRFS-1100 - can only download 100 times before 404 given for client downloads, VRFS-862 - quick change to unblock --- db/manifest | 1 + db/up/track_download_counts.sql | 5 ++ ruby/lib/jam_ruby/models/claimed_recording.rb | 2 +- ruby/lib/jam_ruby/models/mix.rb | 16 +++++- ruby/lib/jam_ruby/models/recorded_track.rb | 14 +++++ ruby/spec/factories.rb | 20 ++++++- ruby/spec/jam_ruby/models/mix_spec.rb | 10 ++++ ruby/spec/spec_helper.rb | 3 ++ ruby/spec/support/utilities.rb | 4 ++ web/app/assets/javascripts/ftue.js | 2 +- web/app/assets/javascripts/shareDialog.js | 7 --- web/app/controllers/api_controller.rb | 3 +- web/app/controllers/api_mixes_controller.rb | 13 ++++- .../controllers/api_recordings_controller.rb | 13 ++++- web/app/views/api_mixes/download.rabl | 1 + web/app/views/layouts/web.erb | 1 - web/config/application.rb | 4 +- ...spec.rb => api_claimed_recordings_spec.rb} | 0 ...ec.rb => api_corporate_controller_spec.rb} | 1 - .../controllers/api_mixes_controller_spec.rb | 53 +++++++++++++++++++ ...c.rb => api_recordings_controller_spec.rb} | 48 +++++++++++++++++ web/spec/factories.rb | 19 +++++++ web/spec/features/recordings_spec.rb | 4 +- web/spec/spec_helper.rb | 10 ++-- 24 files changed, 228 insertions(+), 26 deletions(-) create mode 100644 db/up/track_download_counts.sql create mode 100644 web/app/views/api_mixes/download.rabl rename web/spec/controllers/{claimed_recordings_spec.rb => api_claimed_recordings_spec.rb} (100%) rename web/spec/controllers/{corporate_controller_spec.rb => api_corporate_controller_spec.rb} (96%) create mode 100644 web/spec/controllers/api_mixes_controller_spec.rb rename web/spec/controllers/{recordings_controller_spec.rb => api_recordings_controller_spec.rb} (70%) diff --git a/db/manifest b/db/manifest index 00a8e8bb2..2741cfc1a 100755 --- a/db/manifest +++ b/db/manifest @@ -121,3 +121,4 @@ scores_mod_connections.sql scores_create_schemas_and_extensions.sql scores_create_tables.sql remove_is_downloadable.sql +track_download_counts.sql \ No newline at end of file diff --git a/db/up/track_download_counts.sql b/db/up/track_download_counts.sql new file mode 100644 index 000000000..f08d001a9 --- /dev/null +++ b/db/up/track_download_counts.sql @@ -0,0 +1,5 @@ +ALTER TABLE recorded_tracks ADD COLUMN download_count INTEGER NOT NULL DEFAULT 0; +ALTER TABLE recorded_tracks ADD COLUMN last_downloaded_at TIMESTAMP; + +ALTER TABLE mixes ADD COLUMN download_count INTEGER NOT NULL DEFAULT 0; +ALTER TABLE mixes ADD COLUMN last_downloaded_at TIMESTAMP; \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/claimed_recording.rb b/ruby/lib/jam_ruby/models/claimed_recording.rb index e7f8fbc4b..607eb053c 100644 --- a/ruby/lib/jam_ruby/models/claimed_recording.rb +++ b/ruby/lib/jam_ruby/models/claimed_recording.rb @@ -20,6 +20,7 @@ module JamRuby validates_uniqueness_of :user_id, :scope => :recording_id validate :user_belongs_to_recording + before_create :generate_share_token SHARE_TOKEN_LENGTH = 8 @@ -67,7 +68,6 @@ module JamRuby !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 216145a2e..0d2282b0c 100644 --- a/ruby/lib/jam_ruby/models/mix.rb +++ b/ruby/lib/jam_ruby/models/mix.rb @@ -10,14 +10,24 @@ module JamRuby attr_accessible :ogg_url, :should_retry, as: :admin attr_accessor :is_skip_mount_uploader + attr_writer :current_user belongs_to :recording, :class_name => "JamRuby::Recording", :inverse_of => :mixes, :foreign_key => 'recording_id' + validates :download_count, presence: true + validate :verify_download_count + skip_callback :save, :before, :store_picture!, if: :is_skip_mount_uploader mount_uploader :ogg_url, MixUploader + def verify_download_count + if (self.download_count < 0 || self.download_count > APP_CONFIG.max_audio_downloads) && !@current_user.admin + errors.add(:download_count, "must be less than or equal to 100") + end + end + before_validation do # this should be an activeadmin only path, because it's using the mount_uploader (whereas the client does something completely different) if !is_skip_mount_uploader && ogg_url.present? && ogg_url.respond_to?(:file) && ogg_url_changed? @@ -67,7 +77,6 @@ module JamRuby !ClaimedRecording.find_by_user_id_and_recording_id(some_user.id, recording_id).nil? end - def errored(reason, detail) self.error_reason = reason self.error_detail = detail @@ -148,6 +157,11 @@ module JamRuby Mix.construct_filename(self.created_at, self.recording_id, self.id, type) end + def update_download_count(count=1) + self.download_count = self.download_count + count + self.last_downloaded_at = Time.now + end + private def delete_s3_files diff --git a/ruby/lib/jam_ruby/models/recorded_track.rb b/ruby/lib/jam_ruby/models/recorded_track.rb index 21e15cf6e..0dc498996 100644 --- a/ruby/lib/jam_ruby/models/recorded_track.rb +++ b/ruby/lib/jam_ruby/models/recorded_track.rb @@ -12,6 +12,7 @@ module JamRuby attr_writer :is_skip_mount_uploader attr_accessible :discard, :user, :user_id, :instrument_id, :sound, :client_id, :track_id, :client_track_id, :url, as: :admin + attr_writer :current_user SOUND = %w(mono stereo) MAX_PART_FAILURES = 3 @@ -31,11 +32,13 @@ module JamRuby validates :length, length: {minimum: 1, maximum: 1024 * 1024 * 256 }, if: :upload_starting? # 256 megs max. is this reasonable? surely... validates :user, presence: true validates :instrument, presence: true + validates :download_count, presence: true before_destroy :delete_s3_files validate :validate_fully_uploaded validate :validate_part_complete validate :validate_too_many_upload_failures + validate :verify_download_count before_save :sanitize_active_admin skip_callback :save, :before, :store_picture!, if: :is_skip_mount_uploader? @@ -97,6 +100,12 @@ module JamRuby end end + def verify_download_count + if (self.download_count < 0 || self.download_count > APP_CONFIG.max_audio_downloads) && !@current_user.admin + errors.add(:download_count, "must be less than or equal to 100") + end + end + def sanitize_active_admin self.user_id = nil if self.user_id == '' end @@ -187,6 +196,11 @@ module JamRuby RecordedTrack.construct_filename(self.created_at, self.recording.id, self.client_track_id) end + def update_download_count(count=1) + self.download_count = self.download_count + count + self.last_downloaded_at = Time.now + end + private def delete_s3_files diff --git a/ruby/spec/factories.rb b/ruby/spec/factories.rb index 69bb70a09..a0a6cdbfd 100644 --- a/ruby/spec/factories.rb +++ b/ruby/spec/factories.rb @@ -171,10 +171,28 @@ FactoryGirl.define do association :user, factory: :user before(:create) { |claimed_recording| - claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user) unless claimed_recording.recording } + + end + + factory :mix, :class => JamRuby::Mix do + started_at Time.now + completed_at Time.now + ogg_md5 'abc' + ogg_length 1 + sequence(:ogg_url) { |n| "recordings/ogg/#{n}" } + mp3_md5 'abc' + mp3_length 1 + sequence(:mp3_url) { |n| "recordings/mp3/#{n}" } + completed true + + before(:create) {|mix| + user = FactoryGirl.create(:user) + mix.recording = FactoryGirl.create(:recording_with_track, owner: user) + mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording) + } end factory :musician_instrument, :class => JamRuby::MusicianInstrument do diff --git a/ruby/spec/jam_ruby/models/mix_spec.rb b/ruby/spec/jam_ruby/models/mix_spec.rb index 150b305e8..7296140a1 100755 --- a/ruby/spec/jam_ruby/models/mix_spec.rb +++ b/ruby/spec/jam_ruby/models/mix_spec.rb @@ -63,6 +63,16 @@ describe Mix do recordings.length.should == 0 end + + describe "download count" do + it "will fail if too high" do + mix = FactoryGirl.create(:mix) + mix.current_user = mix.recording.owner + mix.update_download_count(APP_CONFIG.max_audio_downloads + 1) + mix.save + mix.errors[:download_count].should == ["must be less than or equal to 100"] + end + end end diff --git a/ruby/spec/spec_helper.rb b/ruby/spec/spec_helper.rb index 60f32513b..f1df45da8 100644 --- a/ruby/spec/spec_helper.rb +++ b/ruby/spec/spec_helper.rb @@ -13,6 +13,9 @@ SpecDb::recreate_database # initialize ActiveRecord's db connection ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))["test"]) +# so jam_ruby models that use APP_CONFIG in metadata will load. this is later stubbed pre test run +APP_CONFIG = app_config + require 'jam_ruby' require 'factory_girl' require 'rubygems' diff --git a/ruby/spec/support/utilities.rb b/ruby/spec/support/utilities.rb index ef2e28292..ab6c023f3 100644 --- a/ruby/spec/support/utilities.rb +++ b/ruby/spec/support/utilities.rb @@ -101,6 +101,10 @@ def app_config '315576000' end + def max_audio_downloads + 100 + end + private def audiomixer_workspace_path diff --git a/web/app/assets/javascripts/ftue.js b/web/app/assets/javascripts/ftue.js index 2823888ef..e51581602 100644 --- a/web/app/assets/javascripts/ftue.js +++ b/web/app/assets/javascripts/ftue.js @@ -487,7 +487,7 @@ * Load available drivers and populate the driver select box. */ function loadAudioDrivers() { - var drivers = jamClient.FTUEGetDevices(); + var drivers = jamClient.FTUEGetDevices(false); var driverOptionFunc = function (driverKey, index, list) { optionsHtml += '