/
lsd.rb
63 lines (51 loc) · 1.34 KB
/
lsd.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'fileutils'
require 'sinatra/base'
require 'erb'
class LSD < Sinatra::Base
# Allow static files from the public directory
set :static, true
set :public, 'public'
def initialize player, music_directory
super()
@music_directory = File.expand_path music_directory
@player = player
# Clean music direcory
Dir.glob("#{@music_directory}/*").each do |file|
File.delete file
end
# ID for generating file names
@id = 0
# Start the player
Thread.new {@player.run}
end
# Show song list
get '/songs' do
content = erb :songs, :locals => {:now_playing => @player.now_playing,
:queue => @player.queue}
erb :index, :locals => {:content => content}
end
# Add a song
post '/songs' do
# Read POST'ed file and put it into the music directory
data = params[:data]
tempfile = data[:tempfile]
extension = File.extname(data[:filename])
file_name = File.join @music_directory, @id.to_s + extension
File.open file_name, "wb" do |file|
file.write(tempfile.read)
end
# Add filename to the queue and increment id
@id = @id + 1
if @player.add_song file_name
redirect '/songs'
"HUGE SUCCES\n"
else
status 403
"Queue is full\n"
end
end
# Home
get '/' do
redirect '/songs'
end
end