-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
105 lines (98 loc) · 3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'sinatra'
require 'sinatra-websocket'
require 'sinatra/streaming'
require 'securerandom'
require 'json'
require 'dlcenter'
module DLCenter
class App < Sinatra::Base
helpers Sinatra::Streaming
set :server, 'thin'
set :static, true
set :registry, DLCenter::Registry.new
get '/ws' do
begin
request.websocket do |ws|
namespace = namespace_for_request(request)
client = WSClient.new namespace, ws
namespace.add_client client
end
rescue SinatraWebsocket::Error::ConnectionError
puts "Not a websocket"
end
end
post '/p/:filename' do
stream(:keep_open) do |out|
namespace = namespace_for_request(request)
client = IOClient.new namespace, request.env['data.input'], out, filename: params[:filename], size: request.env["CONTENT_LENGTH"], content_type: request.env["CONTENT_TYPE"]
namespace.add_client client
namespace.broadcast_available_shares
end
end
get '/' do
File.read(settings.public_folder+'/index.html')
end
def namespace_for_request(request)
ctx_key = ENV.fetch("DLCENTER_COMMON_CONTEXT", nil) ? :default : request.ip
settings.registry.context_for(ctx_key).namespace_for(:default)
end
get '/g' do
namespace = namespace_for_request(request)
share = namespace.shares.first
if share
options = {
"Cache-Control" => "no-cache, private",
"Pragma" => "no-cache",
"Content-type" => "#{share.content_type || "octet/stream"}",
"Content-Disposition" => "attachment; filename=\"#{share.name}\""
}
options["Content-Length"] = "#{share.size}" unless share.size.nil?
headers options
stream(:keep_open) do |out|
share.content(out)
nil
end
else
status 404
end
end
get '/all' do
namespace = namespace_for_request(request)
shares = namespace.shares
if shares.size > 0
options = {
"Cache-Control" => "no-cache, private",
"Pragma" => "no-cache",
"Content-type" => "application/zip",
"Content-Disposition" => "attachment; filename=\"dlcenter-pack-#{Time.now.strftime '%F'}.zip\""
}
headers options
stream(:keep_open) do |out|
Share.content(shares, out)
nil
end
else
status 404
end
end
get '/share/:uuid' do
uuid = params[:uuid]
puts "Lookup file #{uuid}"
share = settings.registry.get_share_by_uuid uuid
if share
headers \
"Cache-Control" => "no-cache, private",
"Pragma" => "no-cache",
"Content-type" => "#{share.content_type}",
"Content-Length" => "#{share.size}",
"Content-Disposition" => "attachment; filename=\"#{share.name}\""
stream(:keep_open) do |out|
share.content(out)
nil
end
else
status 404
end
end
end
end