VRFS-2094 wip notation upload bug fixes
This commit is contained in:
parent
cb7b42e761
commit
07024a44a6
|
|
@ -1,20 +1,24 @@
|
|||
# encoding: utf-8
|
||||
|
||||
|
||||
class MusicNotationUploader < CarrierWave::Uploader::Base
|
||||
|
||||
def initialize(*)
|
||||
super
|
||||
JamRuby::UploaderConfiguration.set_aws_public_configuration(self)
|
||||
end
|
||||
|
||||
def store_dir
|
||||
"music_session_notations/#{model.user.id}"
|
||||
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(pdf png jpg jpeg gif xml mxl txt)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ module JamRuby
|
|||
|
||||
end
|
||||
|
||||
def s3_manager
|
||||
@s3_manager ||= S3Manager.new(app_config.aws_bucket, app_config.aws_access_key_id, app_config.aws_secret_access_key)
|
||||
def s3_manager(options={:public => false})
|
||||
@s3_manager ||= S3Manager.new(options[:public] ? app_config.aws_bucket_public : app_config.aws_bucket, app_config.aws_access_key_id, app_config.aws_secret_access_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -2,9 +2,11 @@ module JamRuby
|
|||
class MusicNotation < ActiveRecord::Base
|
||||
include JamRuby::S3ManagerMixin
|
||||
|
||||
NOTATION_FILE_DIR = "music_session_notations"
|
||||
|
||||
self.primary_key = 'id'
|
||||
|
||||
attr_accessible :file_url, :size, :file_name
|
||||
attr_accessible :file_url, :size, :file_name, :absolute_url_path
|
||||
|
||||
belongs_to :user, :class_name => "JamRuby::User", foreign_key: :user_id
|
||||
belongs_to :music_session, :class_name => "JamRuby::MusicSession", foreign_key: :music_session_id
|
||||
|
|
@ -13,27 +15,44 @@ module JamRuby
|
|||
|
||||
before_destroy :delete_s3_files
|
||||
|
||||
validates :file_url, :presence => true
|
||||
validates :file_url, :presence => true
|
||||
validates :size, :presence => true
|
||||
|
||||
before_validation do
|
||||
if file_url.present? && file_url_changed?
|
||||
self.size = file_url.file.size
|
||||
end
|
||||
def self.create(session_id, file, current_user)
|
||||
music_notation = MusicNotation.new
|
||||
music_notation.file_name = file.original_filename
|
||||
music_notation.music_session_id = session_id
|
||||
music_notation.user = current_user
|
||||
music_notation.size = file.size
|
||||
music_notation[:file_url] = music_notation.filename
|
||||
music_notation.save
|
||||
return music_notation
|
||||
end
|
||||
|
||||
def filename
|
||||
MusicNotation.construct_filename(self)
|
||||
end
|
||||
|
||||
def absolute_url_path
|
||||
s3_manager({:public => true}).url(self.filename)
|
||||
end
|
||||
|
||||
def s3_url
|
||||
s3_manager.s3_url(self[:file_url])
|
||||
s3_manager({:public => true}).s3_url(self.filename)
|
||||
end
|
||||
|
||||
def sign_url(expiration_time = 120)
|
||||
s3_manager.sign_url(self[:file_url], {:expires => expiration_time, :secure => false})
|
||||
s3_manager({:public => true}).sign_url(self[:file_url], {:expires => expiration_time, :secure => false})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.construct_filename(notation)
|
||||
"#{NOTATION_FILE_DIR}/#{notation.user.id}/#{notation.file_name}" #-#{created_at.strftime('%m-%d-%Y')}
|
||||
end
|
||||
|
||||
def delete_s3_files
|
||||
s3_manager.delete(self[:file_url]) if self[:file_url]
|
||||
s3_manager({:public => true}).delete(self[:file_url]) if self[:file_url]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -15,13 +15,7 @@ class ApiMusicNotationsController < ApiController
|
|||
@music_notations = []
|
||||
|
||||
params[:files].each do |file|
|
||||
music_notation = MusicNotation.new
|
||||
music_notation.file_url = file
|
||||
music_notation.file_name = file.original_filename
|
||||
music_notation.music_session_id = params[:session_id]
|
||||
music_notation.user = current_user
|
||||
music_notation.save
|
||||
|
||||
music_notation = MusicNotation.create(params[:session_id], file, current_user)
|
||||
@music_notations.push music_notation
|
||||
end if params[:files]
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ else
|
|||
attributes :id, :file_name
|
||||
|
||||
node do |music_notation|
|
||||
{ file_url: music_notation["file_url"] }
|
||||
{ file_url: music_notation.absolute_url_path }
|
||||
end
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ else
|
|||
attributes :id, :file_name
|
||||
|
||||
node do |music_notation|
|
||||
{ file_url: music_notation["file_url"] }
|
||||
{ file_url: music_notation.absolute_url_path }
|
||||
end
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue