-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·290 lines (240 loc) · 5.94 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
require 'bundler/setup'
Bundler.require
require 'sinatra/reloader' if development?
require './models.rb'
require 'browser/browser'
require 'browser/aliases'
Browser::Base.include(Browser::Aliases)
require 'sinatra-websocket'
require 'json'
require 'dotenv'
Dotenv.load
enable :sessions
set :server, 'thin'
set :sockets, {}
def current_user
User.find_by(id: session[:user])
end
def browser_check
ua = request.user_agent
browser = Browser.new(ua)
if browser.mobile?
redirect '/sp'
end
end
def session_check(routing)
before "#{routing}" do
#セッション中でない場合indexへ
if session[:user].nil?
redirect '/'
end
end
end
def no_session_check(routing)
before "#{routing}" do
#セッション中の場合indexへ
if session[:user].present?
redirect '/'
end
end
end
get '/' do
browser_check
if session[:user].nil?
erb :index
else
@workspaces = current_user.workspaces.order(updated_at: :desc)
erb :index_session
end
end
get '/sp' do
ua = request.user_agent
browser = Browser.new(ua)
unless browser.mobile?
redirect '/'
end
erb :mobile
end
no_session_check("/login")
get '/login' do
browser_check
erb :login
end
post '/login' do
user = User.find_by(mail: params[:mail])
if user && user.authenticate(params[:password])
session[:user] = user.id
redirect '/'
else
session[:signin_error] = 'メールアドレス、またはパスワードが間違っています'
erb :login
end
end
no_session_check("/signup")
get '/signup' do
browser_check
erb :signup
end
post '/signup' do
if params[:name] == "" || params[:mail] == "" || params[:password] == "" || params[:password_confirmation] == ""
session[:signup_error] = '入力されていない項目があります'
erb :signup
else
@user = User.create(
name: params[:name],
mail: params[:mail],
password: params[:password],
password_confirmation: params[:password_confirmation])
if @user.persisted?
session[:user] = @user.id
redirect '/'
else
if User.find_by(name: params[:name]).present?
session[:signup_error] = 'ユーザー名がすでに存在します'
elsif User.find_by(mail: params[:mail]).present?
session[:signup_error] = 'このメールアドレスは登録済みです'
else
session[:signup_error] = '入力を確認してください'
end
erb :signup
end
end
end
get '/logout' do
session[:user] = nil
redirect '/'
end
session_check("/workspace/create")
get '/workspace/create' do
browser_check
erb :workspace_create
end
post '/workspace/create' do
random = SecureRandom.urlsafe_base64
if Workspace.find_by(url: random).nil?
if params[:name] == ""
name = "名称未設定"
else
name = params[:name]
end
current_user.workspaces.create(
name: name,
description: params[:description],
url: random
)
workspace = Workspace.last
strs = ('a'..'z').to_a
strs.push(*'0'..'9')
strs.each do |str|
workspace.keys.create(
key: str
)
end
redirect '/'
else
"内部エラーが発生しました。もう一度操作をやり直してください"
end
end
post '/workspace/delete/:id' do
workspace = Workspace.find(params[:id])
keys = Key.where(workspace_id: workspace.id)
workspace.destroy
keys.each do |key|
key.destroy
end
redirect '/'
end
get '/workspace/:url' do
browser_check
@workspace = Workspace.find_by(url: params[:url])
@strs = ('a'..'z').to_a
@strs.push(*'0'..'9')
erb :workspace
end
before '/workspace/edit/:url/:id' do
Dotenv.load
Cloudinary.config do |config|
config.cloud_name = env['CLOUDINARY_CLOUD_NAME']
config.api_key = env['CLOUDINARY_API_KEY']
config.api_secret = env['CLOUDINARY_API_SECRET']
config.secure = true
end
end
post '/workspace/edit/:url/:id' do
workspace = Workspace.find_by(url: params[:url])
key = workspace.keys.find(params[:id])
msc_url = key.path
data_type = key.data_type
if params[:file]
msc_url = ""
data_type = "cloudinary"
msc = params[:file]
tempfile = msc[:tempfile]
upload = Cloudinary::Uploader.upload(tempfile.path,:resource_type => :video)
msc_url = upload["secure_url"]
elsif params[:youtube]
tmp = params[:youtube].split("?v=")
if tmp[1].nil?
msc_url = params[:youtube][-11 .. -1]
else
msc_url = tmp[1][0 .. 10]
end
data_type = "youtube"
end
if params[:description] == ""
desc = "説明なし"
else
desc = params[:description]
end
key.update(
path: msc_url,
data_type: data_type,
description: desc
)
redirect "/workspace/#{workspace.url}"
end
post '/workspace/edit/delete/:url/:id' do
workspace = Workspace.find_by(url: params[:url])
key = workspace.keys.find(params[:id])
key.update(
path: nil,
data_type: nil,
description: nil
)
redirect "/workspace/#{workspace.url}"
end
get '/workspace/meta/edit/:id' do
@workspace = Workspace.find(params[:id])
erb :workspace_edit
end
post '/workspace/meta/edit/:id' do
workspace = Workspace.find(params[:id])
workspace.update(
name: params[:name],
description: params[:description]
)
redirect '/'
end
get '/websocket/:id' do |path|
if request.websocket? then
request.websocket do |ws|
ws.onopen do # 接続を開始した時
settings.sockets[path] ||= []
settings.sockets[path] << ws # socketsリストに追加
puts settings.sockets[path]
end
ws.onmessage do |msg| # メッセージを受け取った時
puts 'メッセージを受け取ったよ!'
data = JSON.parse(msg)
puts data
workspace = Workspace.find(path)
settings.sockets[path].each do |s| # メッセージを転送
s.send(data.to_json.to_s)
end
end
ws.onclose do # メッセージを終了する時
settings.sockets.delete(ws)
end
end
end
end