@@ -20,6 +20,32 @@ def allowed_file(filename, allowed_extensions=ALLOWED_EXTENSIONS):
2020 filename .rsplit ('.' , 1 )[1 ].lower () in allowed_extensions
2121
2222
23+ def send_files (handler , file_path , filename ):
24+ with open (file_path , "rb" ) as f :
25+ handler .set_header ('Content-Type' , 'application/force-download' )
26+ handler .set_header ('Content-Disposition' , f"attachment; filename={ filename } " )
27+
28+ try :
29+ while True :
30+ _buffer = f .read (4096 )
31+ if _buffer :
32+ handler .write (_buffer )
33+ else :
34+ return
35+ except :
36+ handler .set_status (500 , f"Error sending files" )
37+
38+
39+ def get_file_paths (handler ):
40+ file_paths = False
41+ if 'uri' in handler .request .arguments :
42+ file_paths = []
43+ tmp_file_paths = [path .decode ('utf-8' ) for path in handler .request .arguments ['uri' ]]
44+ for path in tmp_file_paths :
45+ if os .path .exists (path ):
46+ file_paths .append (path )
47+
48+ return file_paths
2349
2450class NetPyNEController : # pytest: no cover
2551
@@ -57,39 +83,26 @@ def uploads(handler: IPythonHandler):
5783 @get ('/downloads' )
5884 def downloads (handler : IPythonHandler ):
5985
60- if 'uri' in handler .request .arguments :
61- file_paths = []
62- tmp_file_paths = [path .decode ('utf-8' ) for path in handler .request .arguments ['uri' ]]
63- for path in tmp_file_paths :
64- if os .path .exists (path ):
65- file_paths .append (path )
86+ file_paths = get_file_paths (handler )
87+
88+ if file_paths :
6689
6790 if len (file_paths ) == 0 :
6891 handler .set_status (400 , f"Files not found." )
6992 handler .finish ()
7093 return
7194
72- with TemporaryDirectory () as dir_path :
73- tar_gz_file_name = f'{ str (uuid .uuid4 ())} .tar.gz'
74- tar_gz_file_path = os .path .join (dir_path , tar_gz_file_name )
75- with tarfile .open (tar_gz_file_path , mode = 'w:gz' ) as tar :
76- for file_path in file_paths :
77- tar .add (file_path , os .path .join ('download' ,file_path .split ('/' )[- 1 ]))
78-
79- handler .set_header ('Content-Type' , 'application/force-download' )
80- handler .set_header ('Content-Disposition' , f'attachment; filename={ tar_gz_file_name } ' )
81-
82- with open (tar_gz_file_path , "rb" ) as f :
83- try :
84- while True :
85- _buffer = f .read (4096 )
86- if _buffer :
87- handler .write (_buffer )
88- else :
89- f .close ()
90- handler .finish ()
91- return
92- except :
93- handler .set_status (500 , f"Error sending files" )
95+ if len (file_paths ) == 1 :
96+ send_files (handler , file_paths [0 ], file_paths [0 ].split ('/' )[- 1 ])
97+
98+ else :
99+ with TemporaryDirectory () as dir_path :
100+ tar_gz_file_name = f'{ str (uuid .uuid4 ())} .tar.gz'
101+ tar_gz_file_path = os .path .join (dir_path , tar_gz_file_name )
102+ with tarfile .open (tar_gz_file_path , mode = 'w:gz' ) as tar :
103+ for file_path in file_paths :
104+ tar .add (file_path , os .path .join ('download' , file_path .split ('/' )[- 1 ]))
105+
106+ send_files (handler , tar_gz_file_path , tar_gz_file_name )
94107
95108 handler .finish ()
0 commit comments