Skip to content

Commit 75bd370

Browse files
authored
Merge pull request #72 from ksXV/ksxv
removed cgi - deprecated package
2 parents be9a3ca + 9e97d3c commit 75bd370

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

url_modules/api/fs/write.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
#
88
# Licensed under GNU/GPLv3
99
# Copyright (C) 2019, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <[email protected]>
10-
1110
import json
1211
import os
1312
import urllib
14-
import cgi
1513

1614
def run(request, params):
1715
if params is None: params = {}
@@ -26,21 +24,18 @@ def run(request, params):
2624
else:
2725
result_json["ok"] = True
2826

29-
form_data = cgi.FieldStorage(
30-
fp=request.rfile,
31-
headers=request.headers,
32-
environ={"REQUEST_METHOD": "POST"}
33-
)
27+
content_length = int(request.headers.get('Content-Length', 0))
28+
post_data: bytes = request.rfile.read(content_length)
3429

35-
file_path = form_data.getvalue("path")
30+
form_data = urllib.parse.parse_qs(post_data.decode('utf-8'))
31+
file_path = form_data["path"][0]
3632

3733
is_exists = os.path.exists(file_path)
3834
is_dir = os.path.isdir(file_path) if is_exists else False
3935

4036
if is_exists and not is_dir:
41-
fd = open(file_path, 'w')
42-
fd.write(form_data.getvalue("content"))
43-
fd.close()
37+
with open(file_path, 'w') as fd:
38+
fd.write(form_data["content"][0])
4439
else:
4540
result_json["error"] = {
4641
"not_exists": not is_exists,
@@ -50,4 +45,4 @@ def run(request, params):
5045
request.send_response(200)
5146
request.send_header("Content-Type", "application/json; charset=utf-8")
5247
request.end_headers()
53-
request.wfile.write(json.dumps(result_json).encode())
48+
request.wfile.write(json.dumps(result_json).encode())

0 commit comments

Comments
 (0)