25
25
26
26
# 5 MB --> Must be a multiple of CHUNK_SIZE_BASE
27
27
DEFAULT_UPLOAD_CHUNK_SIZE = 1024 * 1024 * 5
28
- ALLOWED_PDF_EXTENSIONS = {' .csv' , ' .doc' , ' .docx' , ' .odp' , ' .ods' , ' .odt' ,
29
- ' .pot' , ' .potm' , ' .potx' ,
30
- ' .pps' , ' .ppsx' , ' .ppsxm' , ' .ppt' , ' .pptm' , ' .pptx' ,
31
- ' .rtf' , ' .xls' , ' .xlsx' }
28
+ ALLOWED_PDF_EXTENSIONS = {" .csv" , " .doc" , " .docx" , " .odp" , " .ods" , " .odt" ,
29
+ " .pot" , " .potm" , " .potx" ,
30
+ " .pps" , " .ppsx" , " .ppsxm" , " .ppt" , " .pptm" , " .pptx" ,
31
+ " .rtf" , " .xls" , " .xlsx" }
32
32
33
33
34
34
class DownloadableMixin :
35
35
36
36
def download (self , to_path : Union [None , str , Path ] = None , name : str = None ,
37
- chunk_size : Union [str , int ] = ' auto' , convert_to_pdf : bool = False ,
37
+ chunk_size : Union [str , int ] = " auto" , convert_to_pdf : bool = False ,
38
38
output : Optional [BytesIO ] = None ):
39
39
""" Downloads this file to the local drive. Can download the
40
40
file in chunks with multiple requests to the server.
@@ -64,7 +64,7 @@ def download(self, to_path: Union[None, str, Path] = None, name: str = None,
64
64
to_path = Path (to_path )
65
65
66
66
if not to_path .exists ():
67
- raise FileNotFoundError (' {} does not exist' .format (to_path ))
67
+ raise FileNotFoundError (" {} does not exist" .format (to_path ))
68
68
69
69
if name and not Path (name ).suffix and self .name :
70
70
name = name + Path (self .name ).suffix
@@ -76,12 +76,12 @@ def download(self, to_path: Union[None, str, Path] = None, name: str = None,
76
76
to_path = to_path / name
77
77
78
78
url = self .build_url (
79
- self ._endpoints .get (' download' ).format (id = self .object_id ))
79
+ self ._endpoints .get (" download" ).format (id = self .object_id ))
80
80
81
81
try :
82
82
if chunk_size is None :
83
83
stream = False
84
- elif chunk_size == ' auto' :
84
+ elif chunk_size == " auto" :
85
85
if self .size and self .size > SIZE_THERSHOLD :
86
86
stream = True
87
87
else :
@@ -94,12 +94,16 @@ def download(self, to_path: Union[None, str, Path] = None, name: str = None,
94
94
"or any integer number representing bytes" )
95
95
96
96
params = {}
97
- if convert_to_pdf and Path (name ).suffix in ALLOWED_PDF_EXTENSIONS :
98
- params ['format' ] = 'pdf'
97
+ if convert_to_pdf :
98
+ if not output :
99
+ if Path (name ).suffix in ALLOWED_PDF_EXTENSIONS :
100
+ params ["format" ] = "pdf"
101
+ else :
102
+ params ["format" ] = "pdf"
99
103
100
104
with self .con .get (url , stream = stream , params = params ) as response :
101
105
if not response :
102
- log .debug (' Downloading driveitem Request failed: {}' .format (
106
+ log .debug (" Downloading driveitem Request failed: {}" .format (
103
107
response .reason ))
104
108
return False
105
109
@@ -115,12 +119,12 @@ def write_output(out):
115
119
if output :
116
120
write_output (output )
117
121
else :
118
- with to_path .open (mode = 'wb' ) as output :
122
+ with to_path .open (mode = "wb" ) as output :
119
123
write_output (output )
120
124
121
125
except Exception as e :
122
126
log .error (
123
- ' Error downloading driveitem {}. Error: {}' .format (self .name ,
127
+ " Error downloading driveitem {}. Error: {}" .format (self .name ,
124
128
str (e )))
125
129
return False
126
130
0 commit comments