-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
101 lines (82 loc) · 2.56 KB
/
server.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
import os
import mimetypes
import json
import tornado.ioloop
import tornado.web
import tornado.websocket
FILE_PATH = os.getcwd()
SRC_PATH = os.path.dirname(__file__)
VALID_TYPES = [
"image/png",
"image/jpeg",
"video/mp4",
"application/pdf",
"audio/mpeg"
]
def pwd():
files = [{
"type": "dir",
"path": "parent",
"absolute": os.path.split(FILE_PATH)[0],
}]
listing = os.listdir(FILE_PATH)
for f in listing:
t = mimetypes.guess_type(f)[0]
if t in VALID_TYPES:
files.append({
"type": t,
"path": f"/files/{f}",
})
elif os.path.isdir(f) and f[0] != ".":
files.append({
"type": "dir",
"path": f,
"absolute": os.path.join(FILE_PATH, f)
})
layout = {}
if ".CS_Store" in listing:
with open(f"{FILE_PATH}/.CS_Store", "r") as f:
layout = json.loads(f.read())
return {
"path": FILE_PATH,
"files": files,
"layout": layout,
}
class WSHandler(tornado.websocket.WebSocketHandler):
def on_message(self, message):
global FILE_PATH
message = json.loads(message)
if message["type"] == "initialize":
self.write_message(json.dumps(pwd()))
elif message["type"] == "layout":
with open(f"{FILE_PATH}/.CS_Store", "w") as f:
f.write(json.dumps(message["layout"], indent=4))
elif message["type"] == "cd":
FILE_PATH = message["path"]
os.chdir(FILE_PATH)
server.redirect()
self.write_message(json.dumps(pwd()))
class Server:
def __init__(self):
pass
def start(self):
self.app = tornado.web.Application([
(r'/ws', WSHandler),
(r'/static/(.*)', tornado.web.StaticFileHandler, { "path": SRC_PATH }),
(r'/files/(.*)', tornado.web.StaticFileHandler, { "path": FILE_PATH }),
])
self.server = self.app.listen(1234)
tornado.ioloop.IOLoop.current().start()
def redirect(self):
global FILE_PATH, SRC_PATH
self.app.default_router.rules = []
self.app.add_handlers(r".*", [
(r'/ws', WSHandler),
(r'/static/(.*)', tornado.web.StaticFileHandler, { "path": SRC_PATH }),
(r'/files/(.*)', tornado.web.StaticFileHandler, { "path": FILE_PATH }),
])
server = Server()
if __name__ == "__main__":
print("Files:", FILE_PATH)
print("Code:", SRC_PATH)
server.start()