Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize file paths when downloading files #102

Open
DeannaDS opened this issue Dec 22, 2021 · 1 comment
Open

Sanitize file paths when downloading files #102

DeannaDS opened this issue Dec 22, 2021 · 1 comment

Comments

@DeannaDS
Copy link

If you pass in a label_key field to the downloads function, and the data in that field has characters that are not allowed in a file name, it does not escape them or gracefully fail. It hard fails with an "invalid argument"

OSError Traceback (most recent call last)
in
10 field="field_508",
11 out_dir="_downloads",
---> 12 label_keys=["field_499"]
13 )

~.conda\envs\ds775\lib\site-packages\knackpy\app.py in download(self, container, field, out_dir, label_keys)
501 )
502
--> 503 download_count = self._download_files(downloads)
504
505 logger.debug(f"{download_count} files downloaded.")

~.conda\envs\ds775\lib\site-packages\knackpy\app.py in _download_files(self, downloads)
458 res.raise_for_status()
459
--> 460 with open(filename, "wb") as fout:
461 fout.write(res.content)
462 count += 1

OSError: [Errno 22] Invalid argument: '_downloads\Youth Define "Constructive" Activity_definitionofconstructive2ndgroup.jpg'

@johnclary
Copy link
Member

@DeannaDS fair enough—thanks for flagging this.

if you're up for opening a PR, this should be a fairly quick fix to this bit of code, perhaps incorporating a new utility function that can live here.

this might suffice as the utility method.

we could also let the user optionally pass in a function that defines the scheme for handling their own file name formatting.

@johnclary johnclary changed the title Downloads Function Label Keys Character Escaping Sanitize file paths when downloading files Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants