cityofaustin / knackpy

A Python client for interacting with Knack applications
https://cityofaustin.github.io/knackpy/docs/user-guide/
Other
39 stars 18 forks source link

Sanitize file paths when downloading files #102

Open DeannaDS opened 2 years ago

DeannaDS commented 2 years ago

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 commented 2 years ago

@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.