-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (45 loc) · 1.49 KB
/
main.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
from fastapi import FastAPI, Path
import uvicorn
import subprocess
import json
app = FastAPI()
@app.get("/")
@app.get("/{full_path:path}")
def directoryHunt(full_path: str = "", filepath = "/mnt/mydata"):
"""
INPUT: A path in str type
OUTPUT: JSON format of a directory showing the file name, owner, size, and permissions
DESCRIPTION:
directoryHunt takes in a path given by the user. We run the "ls -la" OS command on the path
and translate the output into JSON format
"""
all_files = []
return_dir = []
try:
stream = subprocess.check_output(["ls", "-la", filepath + "/" + full_path])
except subprocess.CalledProcessError:
return json.dumps({"error": "Please put in a valid path"})
all_files = stream.decode("utf-8").split("\n")
try:
for eFile in all_files[1:-1]:
sublist = eFile.split()
permission = sublist[0]
owner = sublist[2]
group = sublist[3]
size = sublist[4]
name = sublist[8]
return_dir.append(
{
"permission": permission,
"owner": owner,
"group": group,
"size": size,
"name": name,
}
)
return json.dumps(return_dir)
except Exception as e:
return str(e)
# Setting host and port for application to run on
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=80)