This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
forked from ut7/gnancraft-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·189 lines (140 loc) · 4.66 KB
/
main.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
"""
Dependencies (on a Ubuntu computer, on other systems you will need to find other ways of finding these) :
Flask
Loaded remotely:
ace (javascript editor from ace.ajax.org)
jquery
NB/TODO:
- security issues in the handling of filenames (both file select and save)
"""
from __future__ import unicode_literals
import io # Better alternative for unicode files.
import glob
import os
import signal
import mftp
from flask import Flask, make_response, request, session, render_template, flash, redirect, url_for, send_from_directory
ENCODING = "UTF-8"
JSDIR = os.environ['LOC_WDIR']
DEBUG = 'DEBUG' in os.environ
SECRET = os.environ['SECRET']
FILEMODE = os.environ['MODE']
HOST = os.environ['LOC_HOST']
def get_file_content(file_path):
playername, filename = file_path.split('/')
try:
if FILEMODE == 'SSH' :
return mftp.load_file(playername,filename)
else :
with open("%s/%s"%(JSDIR,file_path),'r') as f :
return f.read()
except:
return ""
def set_file_content(file_path, txt):
playername, filename = file_path.split('/')
if FILEMODE == 'SSH' :
mftp.save_file(playername, filename, txt)
else :
if not os.path.isdir(os.path.dirname("%s/%s"%(JSDIR,file_path))):
os.makedirs(os.path.dirname("%s/%s"%(JSDIR,file_path)))
with open("%s/%s"%(JSDIR,file_path), 'w') as f :
f.write(txt)
def logged_in_player():
if 'player' in session:
return session['player']
else:
return None
# ---------------------
app = Flask(__name__)
app.secret_key = SECRET
@app.route('/')
def index():
return redirect(url_for('edit'))
@app.route('/auth/<token>')
def auth(token):
import auth
try:
info = auth.verifyToken(token, SECRET)
login = info['login']
session['player'] = login
flash('Logged in as %s' % login)
except Exception, e:
app.logger.error("Auth error: %s" % str(e))
flash('Invalid token')
return redirect(url_for('edit'))
@app.route('/logout')
def logout():
if 'player' in session:
session.pop('player', None)
flash('Logged out')
return redirect(url_for('edit'))
@app.route("/edit/")
def edit():
"lists files we might want to edit"
if FILEMODE == 'SSH' :
fnames = mftp.list_files()
else :
directories = glob.glob("%s/*/"%(JSDIR))
fnames = {}
for directory in directories:
files = glob.glob("%s*.js"%directory)
dirname = os.path.basename(os.path.dirname(directory))
if files :
fnames[dirname] = [os.path.basename(f) for f in files]
ownedFiles = None
playerDir = None
if logged_in_player():
ownedFiles = []
playerDir = logged_in_player()
if playerDir in fnames:
ownedFiles = fnames[playerDir]
del fnames[playerDir]
return render_template(
'edit.html',
ownedFiles=ownedFiles,
playerDir=playerDir,
filesByDirectory=fnames,
logged_in_player=logged_in_player()
)
@app.route("/edit/<playername>/<filename>")
def editfile(playername, filename):
"edit the content of a file"
fname = playername + '/' + filename
app.logger.debug(url_for("load", playername=playername, filename=filename))
return render_template(
'editfile_ace.html',
fname=fname,
logged_in_player=logged_in_player(),
can_save=logged_in_player() == playername,
save_url=url_for("save", playername=playername, filename=filename),
file_url=url_for("load", playername=playername, filename=filename),
)
@app.route("/load/<playername>/<filename>")
def load(playername, filename):
"get the content of a file"
fname = playername + '/' + filename
mfile = get_file_content("%s"%fname)
response = make_response(mfile.encode("utf8"))
response.headers['Content-Type'] = "text/plain"
response.headers['Content-Disposition'] = "inline; filename=" + filename
return response
@app.route('/save/<playername>/<filename>', methods=['GET', 'POST'])
def save(playername, filename):
"Handles save file"
if logged_in_player() != playername:
return "", 403
fname = playername + '/' + filename
app.logger.debug("Saving file %s" % fname)
txt = request.form['text']
file_path = "%s"%fname
set_file_content(file_path, txt)
return ""
def term_handler(signum, frame):
app.logger.info("Caught TERM signal, shutting down.")
exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGTERM, term_handler)
app.run(host=HOST, debug=DEBUG)