-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
66 lines (55 loc) · 1.74 KB
/
api.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
#!/usr/bin/python
import cgi
import datetime
import json
import re
from os import listdir, stat
from os.path import exists, isdir, isfile, join
print "Content-Type: application/json"
print
param = cgi.FieldStorage()
def getDirectory(dir):
originDir = "/var/www/download_files"
if dir:
dir = re.sub(r'\\','',dir)
dir = re.sub(r'\/\.\.\/','',dir)
if dir.startswith('/'):
dir = dir[1:]
dir = re.sub(r'\\','',dir)
dir = re.sub(r'\/\.\.\/','',dir)
if dir == "../" or dir == ".." or dir == "/" or dir == "../." :
dir = ""
else:
dir = ""
dir = join(join(originDir,dir),'','')
if not exists(dir):
dir = originDir
allcontent = listdir(dir)
dirItem = []
for item in allcontent:
hashInfo = {}
if item.startswith('.'):
continue
listInfo = stat(join(dir,item))
hashInfo['name'] = item
hashInfo['url'] = re.sub(r''+re.escape(originDir)+'','',dir)+item
hashInfo['edit'] = datetime.datetime.fromtimestamp(int(listInfo.st_mtime)).strftime('%Y-%m-%d %H:%M:%S')
hashInfo['size'] = int(listInfo.st_size)
if isfile(join(dir,item)):
hashInfo['type'] = 'file'
if isdir(join(dir,item)):
hashInfo['type'] = 'dir'
dirItem.append(hashInfo)
return dirItem
def getAction():
action = param.getvalue("action")
if action == "getDirectory":
dir = param.getvalue("dir")
return getDirectory(dir)
elif action == "deleteFile":
file = param.getvalue("file")
#TODO#
return {"message": "Not implemented"}
else:
return {"message": "Please specify an valid action"}
print json.dumps(getAction())