Skip to content

Commit

Permalink
Merge pull request #211 from munen/bug/72028086/player_fail_windows
Browse files Browse the repository at this point in the history
Bug/72028086/player fail windows
  • Loading branch information
branch14 committed Jun 17, 2014
2 parents f195b7d + d220d73 commit ec4e887
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
19 changes: 15 additions & 4 deletions app/models/talk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,21 @@ def podcast_file
storage["#{uri}/#{id}.mp3"]
end


private

# upload file to storage
def upload_file(key, file)
return unless key and file
handle = File.open(file)
ext = key.split('.').last
# Explicity set content type via Mime::Type, otherwise
# Fog will use MIME::Types to determine the content type
# and MIME::Types is a horrible, horrible beast.
ctype = Mime::Type.lookup_by_extension(ext)
media_storage.files.create key: key, body: handle, content_type: ctype
end

# Assemble `starts_at` from `starts_at_date` and `starts_at_time`.
#
# Since the validity of `starts_at_date` and `starts_at_time` is ensured
Expand Down Expand Up @@ -408,9 +421,8 @@ def process_override!(uat=false)
%x[ #{cmd} ]
# upload ogg to s3
key = uri + "/" + ogg
handle = File.open(ogg)
logfile.puts "#R# s3cmd put #{ogg} to s3://media_storage.key/#{key}"
media_storage.files.create key: key, body: handle
upload_file(key, file)
# store reference
path = "s3://#{media_storage.key}/#{key}"
update_attribute :recording_override, path
Expand Down Expand Up @@ -473,9 +485,8 @@ def run_chain!(chain, uat=false)
files.each do |file|
cache_storage_metadata(file)
key = "#{uri}/#{File.basename(file)}"
handle = File.open(file)
logfile.puts "#R# s3cmd put #{file} s3://#{media_storage.key}/#{key}"
media_storage.files.create key: key, body: handle
upload_file(key, file)
FileUtils.rm(file, verbose: true)
end

Expand Down
5 changes: 1 addition & 4 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Be sure to restart your server when you modify this file.
Mime::Type.register 'audio/mp4', :m4a

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone
25 changes: 25 additions & 0 deletions lib/tasks/cleanup.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
namespace :cleanup do
desc 'Correctly set content-type for M4A files in S3'
task :set_content_type => :environment do
directory = Storage.directories.get(Settings.storage.media)

directory.files.each do |f|
content_type = case f.key.split(".").last.downcase
when "m4a" then "audio/mp4"
when "mp3" then "audio/mpeg"
when "ogg" then "audio/ogg"
else next
end
options = {
'Content-Type' => content_type,
'x-amz-metadata-directive' => 'REPLACE'
}
begin
f.copy(f.directory.key, f.key, options)
Rails.logger.info "Updated content-type on file: '#{f.key}'"
rescue Exception => e
Rails.logger.error "Could not update content-type on file: '#{f.key}'"
Rails.logger.error "Error: '#{e.message}'"
end
end
end

desc 'Delete guest users that are no longer active'
task :guests => :environment do
User.where('firstname like ?', '%guest%').
Expand Down
27 changes: 27 additions & 0 deletions spec/models/talk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@
expect(Talk.featured).to eq([talk1, talk0])
expect(Talk.featured).to include(talk0)
end

describe 'saves the Content-Type' do
before { @talk = FactoryGirl.create(:talk) }
it 'works for m4a' do
pending 'find a way to spec the mime type hack "m4a -> audio/mp4"'
m4a_file = File.expand_path("spec/support/fixtures/transcode0/1.m4a", Rails.root)
@talk.send(:upload_file, '1.m4a', m4a_file)

media_storage = Storage.directories.new(key: Settings.storage.media, prefix: @talk.uri)
media_storage.files.get('1.m4a').content_type.should == 'audio/mp4'
end
it 'works for mp3' do
mp3_file = File.expand_path("spec/support/fixtures/transcode0/1.mp3", Rails.root)
@talk.send(:upload_file, '1.mp3', mp3_file)

media_storage = Storage.directories.new(key: Settings.storage.media, prefix: @talk.uri)
media_storage.files.get('1.mp3').content_type.should == 'audio/mpeg'
end
it 'works for ogg' do
ogg_file = File.expand_path("spec/support/fixtures/transcode0/1.ogg", Rails.root)
@talk.send(:upload_file, '1.ogg', ogg_file)

media_storage = Storage.directories.new(key: Settings.storage.media, prefix: @talk.uri)
media_storage.files.get('1.ogg').content_type.should == 'audio/ogg'
end
end

end

describe 'created' do
Expand Down
Binary file added spec/support/fixtures/transcode0/1.mp3
Binary file not shown.
Binary file added spec/support/fixtures/transcode0/1.ogg
Binary file not shown.

0 comments on commit ec4e887

Please sign in to comment.