-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinatra.rb
101 lines (86 loc) · 2.16 KB
/
sinatra.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
require 'cgi'
require 'digest/md5'
require 'mongoid'
require 'sinatra'
require 'rubygems'
require 'hmac-sha2'
require 'json'
Mongoid.load!('mongoid.yml')
class Link
include Mongoid::Document
field :shortened, type: String
field :session_id, type: Integer
field :url, type: String
end
class Album
include Mongoid::Document
field :session_id, type: Integer
field :urls, type: String
end
get '/chooser' do
erb :chooser
end
get '/gallery' do
id = params[:id]
album = Album.where(:session_id => id)[0]
@id = id
@urls = JSON.parse(album.urls)
erb :gallery
end
post '/gallery' do
key = (0...8).map{65.+(rand(26)).chr}.join
album = Album.new(:session_id => key, :urls => params[:imgs])
album.save
key
end
get '/' do
erb :index
end
get '/play' do
if !params[:url] || !params[:session_id]
return [404, '404: Invalid url']
end
# shortener
url = CGI.unescape(params[:url])
session_id = params[:session_id]
# shortening this is kinda risky, but I'm a daredevil -Don
short = Digest::MD5.hexdigest(url+session_id)[0..8]
link = Link.new(:shortened => short, :session_id => session_id, :url => url)
link.save()
case url
when /youtube\.com/
erb :youtube, :locals => {
:video_id => url.split('=', 2)[1],
:session_id => session_id,
:short_url => short
}
when /.*\.(mp4|mov)/
erb :other_video, :locals => {
:url => url,
:session_id => session_id,
:short_url => short
}
#when /soundcloud\.com/
# erb :soundcloud, :locals => {
# :url => url,
# :session_id => session_id,
# :short_url => short
# }
else
[404, '404: Invalid link']
end
end
get %r{/(?<shortened>[\da-zA-Z]+)} do
# look it up
shortened = params['captures'][0]
link = Link.where('shortened' => shortened)[0]
final_url = "/play?url=#{CGI.escape(link.url)}&session_id=#{link.session_id.to_s}"
erb :shortcut, :locals => { :url => final_url }
end
post '/pusher/auth' do
key = '86baf974dd9fd950a9c8'
secret = 'e3fcaaf58db221657bf4'
string_to_sign = "#{params[:socket_id]}:#{params[:channel_name]}"
signature = HMAC::SHA256.hexdigest(secret, string_to_sign)
"{\"auth\":\"#{key}:#{signature}\"}"
end