Implement track/stream

* Add Streamer class
* Use FactoryGirl for factories
* Add sha256 field to tracks
* Add mime_type field to tracks
This commit is contained in:
Thibault Jouan
2011-07-23 16:30:51 +00:00
parent 47fa969617
commit 2f6a447416
18 changed files with 166 additions and 51 deletions

View File

@@ -9,10 +9,10 @@ class TracksController < ApplicationController
def create
@track = Track.new(:name => params[:track][:name])
if params[:track][:file]
@track.uploaded_file = params[:track][:file]
end
if @track.save
if @track.save_with_file(
params[:track][:file].tempfile,
params[:track][:file].content_type
)
redirect_to @track
else
render :new
@@ -20,5 +20,8 @@ class TracksController < ApplicationController
end
def stream
track = Track.find params[:id]
self.content_type = track.mime_type
self.response_body = Streamer.new(track.filepath)
end
end

View File

@@ -1,17 +1,18 @@
class Track < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :mime_type
validates_presence_of :sha256
after_create :save_file
def uploaded_file=(file)
@file = file
def filepath
"#{Rails.root}/data/tracks/#{sha256}"
end
def save_file
if @file
File.open("#{Rails.root}/data/tracks/#{id}", 'w') do |f|
f.write @file.tempfile.read
end
def save_with_file(file, mime_type)
self.sha256 = Digest::SHA256.file(file.path).hexdigest
self.mime_type = mime_type
File.open(filepath, 'w') do |f|
f.write file.read
end
save!
end
end

View File

@@ -1,3 +1,3 @@
%h1= @track.name
%audio{:src => stream_track_path(@track)}
%audio{:src => stream_track_path(@track), :controls => true, :autoplay => true}
Your browser does not support the audio element