Skip to content

Commit 2d08262

Browse files
committed
updated upload method
1 parent 8e53f0a commit 2d08262

2 files changed

Lines changed: 52 additions & 40 deletions

File tree

fri/server/main.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@app.route('/upload/<dir>', methods=['POST'])
1818
def upload(dir):
1919
apikey = request.args.get('apikey')
20-
dirname = dir + "_" + apikey
20+
dirname = secure_filename(dir) + "_" + apikey
2121

2222
if 'files[]' not in request.files:
2323
resp = jsonify({'message': 'No file in the request'})
@@ -62,10 +62,10 @@ def upload(dir):
6262
def build(dir):
6363
graphml_file = request.args.get('fetch')
6464
apikey = request.args.get('apikey')
65-
dirname = dir + "_" + apikey
65+
dirname = secure_filename(dir) + "_" + apikey
6666
makestudy_dir = dirname + "/" + graphml_file #for makestudy
6767
dir_path = os.path.abspath(os.path.join(concore_path, graphml_file)) #path for ./build
68-
if not os.path.exists(secure_filename(dir_path)):
68+
if not os.path.exists(dir_path):
6969
proc = call(["./makestudy", makestudy_dir], cwd=concore_path)
7070
if(proc == 0):
7171
resp = jsonify({'message': 'Directory successfully created'})
@@ -79,6 +79,7 @@ def build(dir):
7979

8080
@app.route('/debug/<dir>', methods=['POST'])
8181
def debug(dir):
82+
dir = secure_filename(dir)
8283
dir_path = os.path.abspath(os.path.join(concore_path, dir))
8384
proc = call(["./debug"], cwd=dir_path)
8485
if(proc == 0):
@@ -93,6 +94,7 @@ def debug(dir):
9394

9495
@app.route('/run/<dir>', methods=['POST'])
9596
def run(dir):
97+
dir = secure_filename(dir)
9698
dir_path = os.path.abspath(os.path.join(concore_path, dir))
9799
proc = call(["./run"], cwd=dir_path)
98100
if(proc == 0):
@@ -106,6 +108,7 @@ def run(dir):
106108

107109
@app.route('/stop/<dir>', methods=['POST'])
108110
def stop(dir):
111+
dir = secure_filename(dir)
109112
dir_path = os.path.abspath(os.path.join(concore_path, dir))
110113
proc = call(["./stop"], cwd=dir_path)
111114
if(proc == 0):
@@ -120,6 +123,7 @@ def stop(dir):
120123

121124
@app.route('/clear/<dir>', methods=['POST'])
122125
def clear(dir):
126+
dir = secure_filename(dir)
123127
dir_path = os.path.abspath(os.path.join(concore_path, dir))
124128
proc = call(["./clear"], cwd=dir_path)
125129
if(proc == 0):
@@ -131,20 +135,17 @@ def clear(dir):
131135
resp.status_code = 500
132136
return resp
133137

134-
# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetch=example.py.out&apikey=xyz
138+
# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetchDir=xyz&fetch=u
135139
@app.route('/download/<dir>', methods=['POST', 'GET'])
136140
def download(dir):
137141
download_file = request.args.get('fetch')
138-
apikey = request.args.get('apikey')
139-
dirname = dir + "_" + apikey
140-
directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname)))
141-
142-
143-
if not os.path.exists(secure_filename(dirname)):
142+
sub_folder = request.args.get('fetchDir')
143+
dirname = secure_filename(dir) + "/" + secure_filename(sub_folder)
144+
directory_name = os.path.abspath(os.path.join(concore_path, dirname))
145+
if not os.path.exists(directory_name):
144146
resp = jsonify({'message': 'Directory not found'})
145147
resp.status_code = 400
146148
return resp
147-
148149
try:
149150
return send_from_directory(directory_name, download_file, as_attachment=True)
150151
except:
@@ -155,6 +156,7 @@ def download(dir):
155156

156157
@app.route('/destroy/<dir>', methods=['DELETE'])
157158
def destroy(dir):
159+
dir = secure_filename(dir)
158160
proc = call(["./destroy", dir], cwd=concore_path)
159161
if(proc == 0):
160162
resp = jsonify({'message': 'Successfuly deleted Dirctory'})
@@ -167,7 +169,9 @@ def destroy(dir):
167169

168170
@app.route('/getFilesList/<dir>', methods=['POST'])
169171
def getFilesList(dir):
170-
dir_path = os.path.abspath(os.path.join(concore_path, dir))
172+
sub_dir = request.args.get('fetch')
173+
dirname = secure_filename(dir) + "/" + secure_filename(sub_dir)
174+
dir_path = os.path.abspath(os.path.join(concore_path, dirname))
171175
res = []
172176
res = os.listdir(dir_path)
173177
res = json.dumps(res)

fri/test.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@
88

99
def upload(files):
1010
url = "http://127.0.0.1:5000/upload/test?apikey=xyz"
11-
1211
payload={}
13-
# files=[
14-
# ('files[]',('example.py',open(path,'rb'),'application/octet-stream'))
15-
# ]
1612
headers = {}
17-
1813
response = requests.request("POST", url, headers=headers, data=payload, files=files)
19-
2014
print(response.text)
2115

2216

2317
# # *******
2418

25-
26-
2719
# function to check build
2820
def build(dir, graphml, apikey):
2921
url = "http://127.0.0.1:5000/build/"+dir+"?"+"fetch="+graphml+"&"+"apikey="+apikey
@@ -60,8 +52,8 @@ def destroy(dir):
6052

6153
print(response.text)
6254

63-
def getFilesList(dir):
64-
url = "http://127.0.0.1:5000/getFilesList/" + dir
55+
def getFilesList(dir, sub_dir = ""):
56+
url = "http://127.0.0.1:5000/getFilesList/" + dir + "?"+"fetch="+sub_dir
6557
response = requests.request("POST", url)
6658
print(response.text)
6759

@@ -71,29 +63,45 @@ def openJupyter():
7163
print(response.text)
7264

7365
# function to test download() method.
74-
def download():
75-
url = "http://127.0.0.1:5000/download/test?fetch=f1.txt&apikey=xyz"
76-
urllib.request.urlretrieve(url, "f1.txt")
66+
def download(dir, subDir, fileName ):
67+
url = "http://127.0.0.1:5000/download/"+dir+"?"+"fetchDir="+subDir+"&"+"fetch="+ fileName
68+
urllib.request.urlretrieve(url, fileName)
7769

7870
# file list to be uploaded
71+
cur_path = os.path.dirname(os.path.abspath(__file__))
72+
demo_path = os.path.abspath(os.path.join(cur_path, '../demo'))
73+
file_name1 = "controller.py"
74+
file_name2 = "pm.py"
75+
file_name3 = "sample1.graphml"
76+
path_file1 = demo_path + "/" +file_name1
77+
path_file2 = demo_path + "/" +file_name2
78+
path_file3 = demo_path + "/" +file_name3
7979
files=[
8080
#('files[]',(file_name,open(file_path,'rb'),'application/octet-stream'))
81-
('files[]',('controller.py',open('/home/amit/Desktop/test_xyz/controller.py','rb'),'application/octet-stream')),
82-
('files[]',('pm.py',open('/home/amit/Desktop/test_xyz/pm.py','rb'),'application/octet-stream')),
83-
('files[]',('sample1.graphml',open('/home/amit/Desktop/test_xyz/sample1.graphml','rb'),'application/octet-stream')),
84-
# ('files[]',('example.py',open('/home/amit/Desktop/fri/example.py','rb'),'application/octet-stream'))
81+
('files[]',(file_name1,open(path_file1,'rb'),'application/octet-stream')),
82+
('files[]',(file_name2,open(path_file2,'rb'),'application/octet-stream')),
83+
('files[]',(file_name3,open(path_file3,'rb'),'application/octet-stream')),
8584
]
8685

8786

88-
# upload(files)
89-
# build("test", "sample1", "xyz")
90-
# time.sleep(6)
91-
# debug("sample1")
92-
# run("sample1")
93-
# clear("sample1")
94-
# stop("sample1")
95-
# getFilesList("sample1")
96-
# destroy("sample1")
97-
# openJupyter()
98-
# download()
87+
upload(files)
88+
time.sleep(2)
89+
build("test", "sample1", "xyz")
90+
time.sleep(6)
91+
method = input("methods - 1 for debug, 0 for run :")
92+
if method == 1:
93+
debug("sample1")
94+
else:
95+
run("sample1")
96+
time.sleep(2)
97+
stop("sample1")
98+
time.sleep(2)
99+
getFilesList("sample1", "cu")
100+
getFilesList("sample1", "pym")
101+
time.sleep(5)
102+
download("sample1", "cu", "u")
103+
clear("sample1")
104+
destroy("sample1")
105+
openJupyter()
106+
99107

0 commit comments

Comments
 (0)