feat: Enhance upload session creation by properly encoding folder paths and filenames

This commit is contained in:
Christian Krakau-Louis
2025-04-12 21:33:25 +02:00
parent 36582030c2
commit b9f909ccd2
+29 -7
View File
@@ -5,6 +5,7 @@ import time
import logging import logging
import requests import requests
import msal import msal
import urllib.parse
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery from app.celery_app import celery
@@ -99,30 +100,51 @@ def create_upload_session(filename, folder_path, access_token):
# Construct the API endpoint # Construct the API endpoint
base_url = "https://graph.microsoft.com/v1.0/me/drive" base_url = "https://graph.microsoft.com/v1.0/me/drive"
# Format the folder path correctly # Format the folder path correctly and properly encode for URL
if folder_path: if folder_path:
# Remove leading/trailing slashes # Remove leading/trailing slashes
folder_path = folder_path.strip('/') folder_path = folder_path.strip('/')
# Replace spaces with %20
folder_path = folder_path.replace(' ', '%20') # URL encode the path components separately
item_path = f"/root:/{folder_path}/{filename}:/createUploadSession" path_components = folder_path.split('/')
encoded_path = '/'.join(urllib.parse.quote(component) for component in path_components)
# Also encode the filename
encoded_filename = urllib.parse.quote(filename)
item_path = f"/root:/{encoded_path}/{encoded_filename}:/createUploadSession"
else: else:
item_path = f"/root:/{filename}:/createUploadSession" # Just encode the filename
encoded_filename = urllib.parse.quote(filename)
item_path = f"/root:/{encoded_filename}:/createUploadSession"
url = f"{base_url}{item_path}" url = f"{base_url}{item_path}"
# Add required request body (can be empty JSON object)
request_body = {
"item": {
"@microsoft.graph.conflictBehavior": "replace"
}
}
headers = { headers = {
"Authorization": f"Bearer {access_token}", "Authorization": f"Bearer {access_token}",
"Content-Type": "application/json" "Content-Type": "application/json"
} }
response = requests.post(url, headers=headers) logger.info(f"Creating upload session for {filename} at path {folder_path}")
response = requests.post(url, headers=headers, json=request_body)
if response.status_code == 200: if response.status_code == 200:
return response.json().get("uploadUrl") upload_url = response.json().get("uploadUrl")
logger.info(f"Upload session created successfully for {filename}")
return upload_url
else: else:
error_msg = f"Failed to create upload session: {response.status_code} - {response.text}" error_msg = f"Failed to create upload session: {response.status_code} - {response.text}"
logger.error(error_msg) logger.error(error_msg)
logger.error(f"Request URL was: {url}")
logger.error(f"Request headers: {headers}")
logger.error(f"Request body: {request_body}")
raise Exception(error_msg) raise Exception(error_msg)
def upload_large_file(file_path, upload_url): def upload_large_file(file_path, upload_url):