This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
86 lines (59 loc) · 2.24 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'base64'
require 'securerandom'
require 'erb'
require 'tmpdir'
require 'sinatra'
require 'sinatra/streaming'
require 'tempfile'
configure do
use Rack::Session::Cookie, :secret => "some unique secret string here"
enable :sessions
end
get '/' do
csrf = (session['csrf'] ||= SecureRandom.hex)
presets = Dir.glob('./presets/*.sts').map {|m| File.basename(m, '.sts')}
erb :index, :locals => { :presets => presets, :csrf => csrf }
end
def check_csrf
raise 'Incorrect CSRF' if params['csrf'] != session['csrf'] or session['csrf'] == nil
end
post '/download/' do
check_csrf
content_type 'audio/flac'
final = Tempfile.new('sts', Dir.tmpdir)
final.close
raise 'Invalid file' if !params[:file][:filename]
file_name = params[:file][:filename]
ext = File.extname(file_name)
file_name = File.basename(file_name, ext)
raise 'Invalid extension' unless ['mp3', 'flac', 'wav', 'ogg'].include?(ext[1..-1])
preset = params[:preset]
preset_path = './presets/' + preset + '.sts'
raise 'Invalid preset' unless preset.match(/^[a-zA-Z0-9\_]+$/) and File.exist?(preset_path)
key = ENV['ST_KEY'] ? ' -k "' + ENV['ST_KEY'] + '"' : ''
`avconv -i "#{params[:file][:tempfile].path}" -f wav -acodec pcm_s16le -ac 2 - | stereo_tool_cmd#{key} - - -s #{preset} | avconv -i - -c:a flac -f flac -y #{final.path}`
puts 'Calling send_file on #{final.path}'
send_file final.path, { :disposition => 'attachment', :filename => "#{file_name}.processed.flac", :type => 'flac' }
#redirect "/file/" + File.basename(final.path) + "?fn=" + Base64::urlsafe_encode64(file_name)
end
get '/file/:file' do | file |
next if file[0..2] != "sts" or file.include? "/"
file_name = Base64::urlsafe_decode64(params[:fn]) rescue "untitled"
send_file Dir.tmpdir + "/" + file, { :disposition => 'attachment', :filename => file_name + ".processed.flac", :type => 'flac' }
end
Thread.new do | a |
loop do
Dir.glob(Dir.tmpdir + "/sts*").each do | path |
data = `lsof #{path}`
next if data.to_s.length > 5
# The file isn't being read, we can close it
sleep 5
puts "Re-checking #{path}"
data = `lsof #{path}`
next if data.to_s.length > 5
puts "File hasn't been opened in the past 5 seconds, likely clean-up able"
File.unlink(path)
end
sleep 5
end
end