Skip to content

Commit

Permalink
✨ Don't overwrite jzwbs in webdav folder
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Jan 8, 2025
1 parent e6c006f commit b26a23b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions fragdenstaat_de/fds_donation/remote_filing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from django.conf import settings

import requests
Expand All @@ -9,11 +11,28 @@ def backup_donation_file(file_handle, file_name):
if not crdedentials or not webdav_url:
return
webdav_username, webdav_password = crdedentials.split(":")
count = 0
original_file_stem, orignal_file_ext = os.path.splitext(file_name)
while webdav_file_exists(file_name, webdav_url, webdav_username, webdav_password):
count += 1
file_name = f"{original_file_stem}-{count}.{orignal_file_ext}"
upload_to_webdav(
file_handle, file_name, webdav_url, webdav_username, webdav_password
)


def webdav_file_exists(
file_name: str, webdav_url: str, webdav_username: str, webdav_password: str
) -> bool:
r = requests.get(
f"{webdav_url}/{file_name}",
auth=(webdav_username, webdav_password),
)
if r.status_code not in [200, 404]:
r.raise_for_status()
return r.status_code == 200


def upload_to_webdav(
file_handle, file_name, webdav_url, webdav_username, webdav_password
):
Expand Down

0 comments on commit b26a23b

Please sign in to comment.