-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathback.py
172 lines (147 loc) · 5.2 KB
/
back.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
from pathlib import Path
import shutil
import mimetypes
import subprocess
import sys
from flask import (
Flask,
request,
render_template,
redirect,
url_for,
session,
send_file,
)
from urllib.parse import unquote
import logging
app = Flask(__name__, template_folder="templates", static_folder="static")
app.config["MAX_CONTENT_PATH"] = 1024 * 1024 * 1024 * 5 # 指定最大文件大小,单位为字节
# Flask 的 Session 是通过加密后放到 Cookie 中的,
# 所以在使用 Session 模块时就一定要配置 SECRET_KEY 全局宏用于加密。
app.config["SECRET_KEY"] = "123456"
log = logging.getLogger("werkzeug")
log.setLevel(logging.ERROR)
base_path = "F:\\fast_protal"
filter_files = ["desktop.ini"]
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r
@app.route("/")
def index():
try:
name = request.args.get("name","")
name = unquote(name)
if name == "../":
path = Path(session["current_path"]).parent
elif name == ".":
path = Path(base_path)
else:
path = Path(name)
print(path)
if not path.exists() or not (len(str(path)) >= len(base_path)):
raise KeyError()
except KeyError:
name = ""
session["current_path"] = base_path
path = base_path
return redirect(url_for("index", name="."), 302)
session["current_path"] = str(path)
if Path(path).is_dir():
files = Path(path).iterdir()
files = [file for file in files if file.name not in filter_files]
sorted_file = sorted(files, key=lambda f: f.is_dir(), reverse=True)
return render_template("index.html", files=sorted_file)
if Path(path).is_file():
# 不拼接文件名
return redirect(url_for("get_file", path=path), 302)
@app.route("/file?<string:path>")
def get_file(path):
mime_type, _ = mimetypes.guess_type(path)
return send_file(path, mimetype=mime_type)
@app.route("/del")
def del_file():
path = request.args.get("path","")
path = unquote(path)
path = Path(path)
if path.exists():
if path.is_file():
path.unlink()
elif path.is_dir():
shutil.rmtree(path)
return redirect(url_for("index", name=str(path.parent)), 301)
@app.route("/cmd")
def exec_cmd():
path = Path(request.args.get("c"))
print(path)
result = subprocess.run(
path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="gbk"
)
if result.returncode == 1:
return {
"result": result.stdout,
"code": result.returncode,
}, 200
else:
return {
"result": result.stderr,
"code": result.returncode,
}, 200
@app.route("/upload", methods=["POST"])
def upload_file():
try:
file = request.files["file"]
file_name = file.filename
save_path = Path(session["current_path"], file_name)
file_size = request.form["dztotalfilesize"] # 文件总大小
current_chunk = int(request.form["dzchunkindex"]) # 当前分块索引
total_chunks = int(request.form["dztotalchunkcount"]) # 总块数
if save_path.exists() and current_chunk == 0:
return {"result": "已经存在该文件"}, 400
with open(save_path, "ab") as f:
f.seek(int(request.form["dzchunkbyteoffset"])) # 字节偏移量
f.write(file.stream.read())
if current_chunk + 1 == total_chunks: # 传输最后一个块
if Path(save_path).stat().st_size != int(file_size):
return {"result": "保存文件大小与实际大小不匹配"}, 500
else:
print(f"File {file.filename} has been uploaded successfully")
else:
print(f"文件 {file.filename}-{current_chunk + 1}/{total_chunks}")
return {"result": "上传成功"}, 200
except KeyError as ke:
print(ke)
return {"result": "error"}, 400
except OSError as oe:
print(oe)
save_path.unlink() if save_path.exists() else ...
return {"result": "文件保存读写错误"}, 400
except Exception as e:
print(e)
save_path.unlink() if save_path.exists() else ...
return {"result": "error"}, 400
if __name__ == "__main__":
# 函数 gethostname() 返回当前正在执行 Python 的系统主机名
cmd_params = sys.argv[1:]
if len(cmd_params) > 0:
if (argv_path := Path(cmd_params[0])).exists():
if argv_path.is_dir():
base_path = str(argv_path)
else:
base_path = str(argv_path.parent)
ip_list = socket.gethostbyname_ex(socket.gethostname())[
-1
] # ('hostname', [], [ip list])
print("http://127.0.0.1:5000/")
for ip in ip_list:
print("http://" + ip + ":5000/")
if Path(base_path).exists():
print(f"监视目录为: {base_path}")
app.run(debug=False, host="0.0.0.0") # 默认端口 port=5000
else:
raise FileNotFoundError(f"没有该目录: {base_path}")