forked from Wildog/flask-file-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_server.py
334 lines (307 loc) · 11.6 KB
/
file_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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from flask import (
Flask,
make_response,
request,
redirect,
render_template,
send_file,
Response
)
from flask.views import MethodView
from flask_cors import CORS
from flask_bcrypt import Bcrypt
from werkzeug.utils import secure_filename
from config import root, ytad_url
from cosntants import *
from filters import size_fmt, time_desc, time_humanize, icon_fmt, data_fmt
from api.api_routes import api
from api.api_methods import *
from notepad.notepad_routes import notepad
# from tools.tools_routes import tools
import shutil
import os
import re
import json
import mimetypes
from pathlib2 import Path
import logging
logging.basicConfig(
filename="record.log",
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s",
)
app = Flask(__name__, static_url_path="/assets", static_folder="assets")
CORS(app)
bcrypt = Bcrypt(app)
key = ""
app.template_filter("size_fmt")(size_fmt)
app.template_filter("time_fmt")(time_desc)
app.template_filter("humanize")(time_humanize)
app.template_filter("icon_fmt")(icon_fmt)
app.template_filter("data_fmt")(data_fmt)
class PathView(MethodView):
def get(self, p=""):
hide_dotfile = request.args.get(
"hide-dotfile", request.cookies.get("hide-dotfile", "no")
)
path = os.path.join(root, p)
path = os.path.normpath(path)
if os.path.isdir(path):
contents = []
audio_files = []
has_audio = False
image_files = []
has_image = False
total = {"size": 0, "dir": 0, "file": 0}
for filename in os.listdir(path):
filename_noext, ext = os.path.splitext(filename)
if ext in ignored:
continue
""" if hide_dotfile == 'yes' and filename[0] == '.':
continue """
filepath = os.path.join(path, filename)
stat_res = os.stat(filepath)
info = {}
info["name"] = filename
info["mtime"] = stat_res.st_mtime
ft = get_type(stat_res.st_mode)
info["type"] = ft
total[ft] += 1
sz = stat_res.st_size
info["size"] = sz
total["size"] += sz
ext = filename.split(".")[-1]
if ext != "":
audio_exts = datatypes.get("audio")
if ext in audio_exts:
audio_files.append(filename)
has_audio = True
image_exts = datatypes.get("image")
if ext in image_exts:
image_files.append(filename)
has_image = True
contents.append(info)
try:
audio_files = sorted(
audio_files, key=lambda s: int(re.search(r"\d+", s).group())
)
except:
audio_files = sorted(audio_files)
page = render_template(
"index.html",
path=p,
dir_path=path,
contents=contents,
total=total,
audio_files=json.dumps(audio_files),
has_audio=has_audio,
image_files=json.dumps(image_files),
has_image=has_image,
ytad_url=ytad_url,
hide_dotfile=hide_dotfile,
)
res = make_response(page, 200)
res.set_cookie("hide-dotfile", hide_dotfile, max_age=16070400)
elif os.path.isfile(path):
if "stream" in request.args:
def generate():
with open(path, "rb") as fwav:
data = fwav.read(1024)
while data:
yield data
data = fwav.read(1024)
return Response(generate(), mimetype="audio/mpeg")
else:
if "Range" in request.headers:
start, end = get_range(request)
res = partial_response(path, start, end)
else:
if len(request.values) == 0 or not request.values["download"]:
res = send_file(path)
else:
res = send_file(path, as_attachment=True)
else:
res = make_response("Not found", 404)
return res
def put(self, p=""):
if request.cookies.get("auth_cookie") == key:
path = os.path.join(root, p)
dir_path = os.path.dirname(path)
Path(dir_path).mkdir(parents=True, exist_ok=True)
info = {}
if os.path.isdir(dir_path):
try:
filename = secure_filename(os.path.basename(path))
with open(os.path.join(dir_path, filename), "wb") as f:
f.write(request.stream.read())
except Exception as e:
info["status"] = "error"
info["msg"] = str(e)
else:
info["status"] = "success"
info["msg"] = "File Saved"
else:
info["status"] = "error"
info["msg"] = "Invalid Operation"
res = make_response(json.JSONEncoder().encode(info), 201)
res.headers.add("Content-type", "application/json")
else:
info = {}
info["status"] = "error"
info["msg"] = "Authentication failed"
res = make_response(json.JSONEncoder().encode(info), 401)
res.headers.add("Content-type", "application/json")
return res
def post(self, p=""):
res = None
if request.cookies.get("auth_cookie") == key:
path = os.path.join(root, p)
info = {}
Path(path).mkdir(mode=0o777, parents=True, exist_ok=True)
if os.path.isdir(path):
files = request.files.getlist("files[]")
for file in files:
try:
filename = secure_filename(file.filename)
file.save(os.path.join(path, filename))
except Exception as e:
info["status"] = "error"
info["msg"] = str(e)
else:
info["status"] = "success"
info["msg"] = "File Saved"
else:
info["status"] = "error"
info["msg"] = "Invalid Operation"
res = make_response(json.JSONEncoder().encode(info), 200)
res.headers.add("Content-type", "application/json")
else:
info = {}
info["status"] = "error"
info["msg"] = "Authentication failed"
res = make_response(json.JSONEncoder().encode(info), 401)
res.headers.add("Content-type", "application/json")
return res
def delete(self, p=""):
if request.cookies.get("auth_cookie") == key:
path = os.path.join(root, p)
dir_path = os.path.dirname(path)
Path(dir_path).mkdir(parents=True, exist_ok=True)
info = {}
if os.path.isdir(dir_path):
try:
filename = secure_filename(os.path.basename(path))
full_path = os.path.join(dir_path, filename)
if filename != "":
os.remove(full_path)
else:
shutil.rmtree(dir_path)
except Exception as e:
info["status"] = "error"
info["msg"] = str(e)
print(e)
else:
info["status"] = "success"
info["msg"] = "File Deleted"
else:
info["status"] = "error"
info["msg"] = "Invalid Operation"
res = make_response(json.JSONEncoder().encode(info), 204)
res.headers.add("Content-type", "application/json")
else:
info = {}
info["status"] = "error"
info["msg"] = "Authentication failed"
res = make_response(json.JSONEncoder().encode(info), 401)
res.headers.add("Content-type", "application/json")
return res
@app.route("/", methods=["GET", "POST", "PUT", "DELETE"])
def redirect_to_store():
return redirect("/store", code=307)
@app.route("/gcleansearch", methods=["GET"])
def gclean():
return render_template("/gclean/index.html")
@app.route("/gcleansearch/index.html", methods=["GET"])
def gcleanIndex():
return render_template("/gclean/index.html")
@app.route("/store/viewerJSODF/index.html", methods=["GET"])
def storeViewerJSODF():
return redirect("/viewerJSODF/index.html", code=307)
@app.route("/viewerJSODF/index.html", methods=["GET"])
def viewerJSODF():
url_hash = request.url.split("#")
if len(url_hash) == 2:
hash = url_hash[1]
return render_template("viewerJSODF/index.html")
@app.route("/audioplayer/index.html", methods=["GET"])
def audioplayer():
p = "/audioplayer/index.html"
dir_path = request.args.get("path")
if dir_path:
dir_path = dir_path.replace("/", "", 1)
path = os.path.join(root, dir_path or "")
path = os.path.normpath(path)
if os.path.isdir(path):
audio_files = []
has_audio = False
for filename in os.listdir(path):
filename_noext, ext = os.path.splitext(filename)
if ext in ignored:
continue
exts = datatypes.get("audio")
if filename.split(".")[-1] in exts:
audio_files.append(filename)
has_audio = True
try:
audio_files = sorted(
audio_files, key=lambda s: int(re.search(r"\d+", s).group())
)
except:
audio_files = sorted(audio_files)
return render_template(
"/audioplayer/index.html",
path="store",
dir_path=path,
audio_files=json.dumps(audio_files),
has_audio=has_audio,
)
@app.route("/mediaplayer/index.html", methods=["GET"])
def mediaplayer():
p = "/mediaplayer/index.html"
dir_path = request.args.get("path")
if dir_path:
dir_path = dir_path.replace("/", "", 1)
path = os.path.join(root, dir_path or "")
path = os.path.normpath(path)
if os.path.isdir(path):
media_files = []
has_audio = False
for filename in os.listdir(path):
filename_noext, ext = os.path.splitext(filename)
if ext in ignored:
continue
media_src = {}
data_type = data_fmt(filename)
if data_type == "audio" or data_type == "video":
media_src["mimetype"] = mimetypes.guess_type(filename)[0]
media_src["name"] = filename
media_files.append(media_src)
has_audio = True
return render_template(
"/mediaplayer/index.html",
path="store",
dir_path=path,
media_files=json.dumps(media_files),
)
path_view = PathView.as_view("path_view")
app.add_url_rule("/store/", view_func=path_view)
app.add_url_rule("/store/<path:p>", view_func=path_view)
app.register_blueprint(api, url_prefix="/api")
app.register_blueprint(notepad, url_prefix="/notepad")
#app.register_blueprint(tools, url_prefix="/tools")
if __name__ == "__main__":
from config import BIND, PORT, KEY
from notepad.notepad_methods import *
key = KEY
create_database()
app.run(BIND, PORT, threaded=True, debug=False)