7
7
#
8
8
# Licensed under GNU/GPLv3
9
9
# Copyright (C) 2019, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <[email protected] >
10
-
11
10
import json
12
11
import os
13
12
import urllib
14
- import cgi
15
13
16
14
def run (request , params ):
17
15
if params is None : params = {}
@@ -26,21 +24,18 @@ def run(request, params):
26
24
else :
27
25
result_json ["ok" ] = True
28
26
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 )
34
29
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 ]
36
32
37
33
is_exists = os .path .exists (file_path )
38
34
is_dir = os .path .isdir (file_path ) if is_exists else False
39
35
40
36
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 ])
44
39
else :
45
40
result_json ["error" ] = {
46
41
"not_exists" : not is_exists ,
@@ -50,4 +45,4 @@ def run(request, params):
50
45
request .send_response (200 )
51
46
request .send_header ("Content-Type" , "application/json; charset=utf-8" )
52
47
request .end_headers ()
53
- request .wfile .write (json .dumps (result_json ).encode ())
48
+ request .wfile .write (json .dumps (result_json ).encode ())
0 commit comments