Ensure directory exists, don't crash if we fail to write

We should be creating the directory because the static tree from the
repo isn't actually copied into the container, so we can't rely on it
existing.

And if we can't write it, we should catch that error instead of crashing
the whole thing, oops!
This commit is contained in:
Joel Bradshaw 2022-02-01 21:45:13 -08:00
parent 0c53f4e003
commit d6abd9b32d

View file

@ -13,11 +13,15 @@ logger = logging.getLogger(__name__)
def download_file(url, destination):
"""Downloads a file to the given path"""
try:
# Ensure our destination directory exists
os.makedirs(os.path.dirname(destination))
with urllib.request.urlopen(url) as stream:
with open(destination, "b+w") as outfile:
outfile.write(stream.read())
except (urllib.error.HTTPError, urllib.error.URLError):
logger.error("Failed to download file %s", url)
except OSError:
logger.error("Couldn't open font file %s for writing", destination)
class BookwyrmConfig(AppConfig):