Skip to content

Commit

Permalink
changed nextcloud code
Browse files Browse the repository at this point in the history
  • Loading branch information
christianlouis committed Feb 11, 2025
1 parent 144be79 commit 9695348
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions app/tasks/upload_to_nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@
@celery.task(base=BaseTaskWithRetry)
def upload_to_nextcloud(file_path: str):
"""Uploads a file to Nextcloud in the configured folder."""

if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")

# Extract filename
filename = os.path.basename(file_path)

# 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
with open(file_path, "rb") as file_data:
response = requests.put(
nextcloud_url,
auth=(settings.NEXTCLOUD_USERNAME, settings.NEXTCLOUD_PASSWORD),
auth=(settings.nextcloud_username, settings.nextcloud_password),
data=file_data
)

# Check if upload was successful
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}
else:
print(f"[ERROR] Failed to upload {filename} to Nextcloud: {response.status_code}, {response.text}")
raise Exception(f"Upload failed: {response.status_code} - {response.text}")
error_msg = f"[ERROR] Failed to upload {filename} to Nextcloud: {response.status_code} - {response.text}"
print(error_msg)
raise Exception(error_msg)

0 comments on commit 9695348

Please sign in to comment.