9
9
from notebook .utils import url2path
10
10
11
11
12
+ # The delay in ms at which we send the chunk of data
13
+ # to the client.
14
+ ARCHIVE_DOWNLOAD_FLUSH_DELAY = 100
15
+
16
+
12
17
class ArchiveStream ():
13
18
def __init__ (self , handler ):
14
19
self .handler = handler
@@ -92,23 +97,20 @@ def get(self, archive_path, include_body=False):
92
97
self .set_header ('content-disposition' ,
93
98
'attachment; filename={}' .format (archive_filename ))
94
99
95
- self .flush_cb = ioloop .PeriodicCallback (self .flush , 100 )
100
+ self .canceled = False
101
+ self .flush_cb = ioloop .PeriodicCallback (self .flush , ARCHIVE_DOWNLOAD_FLUSH_DELAY )
96
102
self .flush_cb .start ()
97
103
98
104
args = (archive_path , archive_format , archive_token )
99
- try :
100
- yield ioloop .IOLoop .current ().run_in_executor (None , self .archive_and_download , * args )
105
+ yield ioloop .IOLoop .current ().run_in_executor (None , self .archive_and_download , * args )
101
106
102
- ## FIXME: Catching the exception does not work.
103
- except Exception :
107
+ if self .canceled :
104
108
self .log .info ('Download canceled.' )
105
-
106
109
else :
107
110
self .flush ()
108
- self .set_cookie ("archiveToken" , archive_token )
109
-
110
111
self .log .info ('Finished downloading {}.' .format (archive_filename ))
111
112
113
+ self .set_cookie ("archiveToken" , archive_token )
112
114
self .flush_cb .stop ()
113
115
self .finish ()
114
116
@@ -119,8 +121,16 @@ def archive_and_download(self, archive_path, archive_format, archive_token):
119
121
for root , _ , files in os .walk (archive_path ):
120
122
for file_ in files :
121
123
file_name = os .path .join (root , file_ )
122
- self .log .debug ("{}\n " .format (file_name ))
123
- archive .add (file_name , os .path .join (root [prefix :], file_ ))
124
+ if not self .canceled :
125
+ self .log .debug ("{}\n " .format (file_name ))
126
+ archive .add (file_name , os .path .join (root [prefix :], file_ ))
127
+ else :
128
+ break
129
+
130
+ def on_connection_close (self ):
131
+ super ().on_connection_close ()
132
+ self .canceled = True
133
+ self .flush_cb .stop ()
124
134
125
135
126
136
class ExtractArchiveHandler (IPythonHandler ):
0 commit comments