changed nextcloud code

This commit is contained in:
Christian Krakau-Louis
2025-02-11 23:42:19 +01:00
parent 8e7d380e71
commit c21f683c0f
+7 -6
View File
@@ -9,7 +9,7 @@ from app.celery_app import celery
@celery.task(base=BaseTaskWithRetry) @celery.task(base=BaseTaskWithRetry)
def upload_to_nextcloud(file_path: str): def upload_to_nextcloud(file_path: str):
"""Uploads a file to Nextcloud in the configured folder.""" """Uploads a file to Nextcloud in the configured folder."""
if not os.path.exists(file_path): if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}") raise FileNotFoundError(f"File not found: {file_path}")
@@ -17,20 +17,21 @@ def upload_to_nextcloud(file_path: str):
filename = os.path.basename(file_path) filename = os.path.basename(file_path)
# Construct the full upload URL # Construct the full upload URL
nextcloud_url = f"{settings.NEXTCLOUD_UPLOAD_URL}/{settings.NEXTCLOUD_FOLDER}/{filename}" nextcloud_url = f"{settings.nextcloud_upload_url}/{settings.nextcloud_folder}/{filename}"
# Read file content # Read file content
with open(file_path, "rb") as file_data: with open(file_path, "rb") as file_data:
response = requests.put( response = requests.put(
nextcloud_url, nextcloud_url,
auth=(settings.NEXTCLOUD_USERNAME, settings.NEXTCLOUD_PASSWORD), auth=(settings.nextcloud_username, settings.nextcloud_password),
data=file_data data=file_data
) )
# Check if upload was successful # Check if upload was successful
if response.status_code in (200, 201): if response.status_code in (200, 201):
print(f"[INFO] Successfully uploaded {filename} to Nextcloud.") print(f"[INFO] Successfully uploaded {filename} to Nextcloud at {nextcloud_url}.")
return {"status": "Completed", "file": file_path} return {"status": "Completed", "file": file_path}
else: else:
print(f"[ERROR] Failed to upload {filename} to Nextcloud: {response.status_code}, {response.text}") error_msg = f"[ERROR] Failed to upload {filename} to Nextcloud: {response.status_code} - {response.text}"
raise Exception(f"Upload failed: {response.status_code} - {response.text}") print(error_msg)
raise Exception(error_msg)