Skip to content

Commit ee1a5bc

Browse files
committed
Fix #1165. Respect convert to pdf when downloading to memory
1 parent c75d3af commit ee1a5bc

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

O365/drive.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525

2626
# 5 MB --> Must be a multiple of CHUNK_SIZE_BASE
2727
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"}
3232

3333

3434
class DownloadableMixin:
3535

3636
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,
3838
output: Optional[BytesIO] = None):
3939
""" Downloads this file to the local drive. Can download the
4040
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,
6464
to_path = Path(to_path)
6565

6666
if not to_path.exists():
67-
raise FileNotFoundError('{} does not exist'.format(to_path))
67+
raise FileNotFoundError("{} does not exist".format(to_path))
6868

6969
if name and not Path(name).suffix and self.name:
7070
name = name + Path(self.name).suffix
@@ -76,12 +76,12 @@ def download(self, to_path: Union[None, str, Path] = None, name: str = None,
7676
to_path = to_path / name
7777

7878
url = self.build_url(
79-
self._endpoints.get('download').format(id=self.object_id))
79+
self._endpoints.get("download").format(id=self.object_id))
8080

8181
try:
8282
if chunk_size is None:
8383
stream = False
84-
elif chunk_size == 'auto':
84+
elif chunk_size == "auto":
8585
if self.size and self.size > SIZE_THERSHOLD:
8686
stream = True
8787
else:
@@ -94,12 +94,16 @@ def download(self, to_path: Union[None, str, Path] = None, name: str = None,
9494
"or any integer number representing bytes")
9595

9696
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"
99103

100104
with self.con.get(url, stream=stream, params=params) as response:
101105
if not response:
102-
log.debug('Downloading driveitem Request failed: {}'.format(
106+
log.debug("Downloading driveitem Request failed: {}".format(
103107
response.reason))
104108
return False
105109

@@ -115,12 +119,12 @@ def write_output(out):
115119
if output:
116120
write_output(output)
117121
else:
118-
with to_path.open(mode='wb') as output:
122+
with to_path.open(mode="wb") as output:
119123
write_output(output)
120124

121125
except Exception as e:
122126
log.error(
123-
'Error downloading driveitem {}. Error: {}'.format(self.name,
127+
"Error downloading driveitem {}. Error: {}".format(self.name,
124128
str(e)))
125129
return False
126130

0 commit comments

Comments
 (0)