-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
sandbox_controller.rb
202 lines (164 loc) · 3.88 KB
/
sandbox_controller.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
class SandboxController < ApplicationController
AS_JSON = {only: %i[id title content created_at updated_at user_id mint_version], include: { user: { only: %i[id nickname image]}}}
skip_before_action :verify_authenticity_token
def login
user =
User.find_or_create_by(
nickname: auth_hash[:info][:nickname],
uid: auth_hash[:uid])
user.update(image: auth_hash[:info][:image])
session[:id] = user.id
redirect_to ENV['SANDBOX_URL']
end
def logout
session[:id] = nil
render plain: ''
end
def user
with_user do |user|
render json: user.as_json
end
end
def update
with_sandbox do |user, sandbox|
UpdateSandbox.run mint_version: params[:mintVersion],
content: params[:content],
title: params[:title],
sandbox: sandbox
render json: sandbox.as_json(AS_JSON)
end
end
def format
with_sandbox do |user, sandbox|
FormatSandbox.run sandbox: sandbox
render json: sandbox.as_json(AS_JSON)
end
end
def recent
sandboxes =
Sandbox
.includes(:user)
.order(updated_at: :desc)
.limit(20)
.as_json(AS_JSON)
render json: sandboxes
end
def index
with_user do |user|
sandboxes =
Sandbox
.where(user_id: user.id)
.includes(:user)
.sort_by(&:title)
.as_json(AS_JSON)
render json: sandboxes
end
end
DEFAULT_SANDBOX =
<<~EOS
component Main {
fun render : Html {
<div>"Hello World!"</div>
}
}
EOS
DEFAULT_HTML =
CompileSandbox.run!(content: DEFAULT_SANDBOX, mint_version: '0.19.0')
def create
with_user do |user|
sandbox =
Sandbox.create!(
id: SecureRandom.urlsafe_base64(10),
content: DEFAULT_SANDBOX,
mint_version: '0.19.0',
title: 'My Sandbox',
html: DEFAULT_HTML,
user: user)
render json: sandbox.as_json(AS_JSON), status: 201
end
end
def destroy
with_sandbox do |user, sandbox|
sandbox.destroy!
render json: sandbox.as_json(AS_JSON), status: 200
end
end
def show
sandbox =
Sandbox.find_by_id(params[:id])
if sandbox
render json: sandbox.as_json(AS_JSON), status: 200
else
render plain: '', status: 404
end
end
def fork
with_user do |user|
sandbox =
Sandbox.find_by_id(params[:id])
if sandbox
forked =
Sandbox.create!(
id: SecureRandom.urlsafe_base64(10),
content: sandbox.content,
title: sandbox.title,
html: sandbox.html,
user: user)
render json: forked.as_json(AS_JSON), status: 200
else
render plain: '', status: 404
end
end
end
def preview
response.headers.delete "X-Frame-Options"
sandbox =
Sandbox.find_by_id(params[:id])
if sandbox
render html: sandbox.html.to_s.html_safe
else
render plain: '', status: 404
end
end
def screenshot
sandbox =
Sandbox.find_by_id(params[:id])
if sandbox
url = url_for(sandbox.screenshot)
if url
redirect_to url
else
render plain: '', status: 404
end
else
render plain: '', status: 404
end
end
protected
def url_for(object)
return unless object.attached?
Rails.application.routes.url_helpers.url_for(object)
end
def with_sandbox
with_user do |user|
sandbox =
user.sandboxes.find_by_id(params[:id])
if sandbox
yield user, sandbox
else
render plain: '', status: 403
end
end
end
def with_user
user = User.find_by_id(session[:id])
if user
yield user
else
render plain: '', status: 401
end
end
def auth_hash
request.env['omniauth.auth']
end
end