diff --git a/modules/interfaces/interfaceComponentObjects.py b/modules/interfaces/interfaceComponentObjects.py index edba0a66..59b10ddf 100644 --- a/modules/interfaces/interfaceComponentObjects.py +++ b/modules/interfaces/interfaceComponentObjects.py @@ -350,6 +350,9 @@ class ComponentObjects: # If fileName is provided, check for exact name+hash match first if fileName: for file in accessibleFiles: + # Skip files without fileName key or with None/empty fileName + if "fileName" not in file or not file["fileName"]: + continue if file["fileName"] == fileName: return FileItem( id=file["id"], @@ -361,16 +364,24 @@ class ComponentObjects: creationDate=file["creationDate"] ) - # Return first file with matching hash (for general duplicate detection) - return FileItem( - id=accessibleFiles[0]["id"], - mandateId=accessibleFiles[0]["mandateId"], - fileName=accessibleFiles[0]["fileName"], - mimeType=accessibleFiles[0]["mimeType"], - fileHash=accessibleFiles[0]["fileHash"], - fileSize=accessibleFiles[0]["fileSize"], - creationDate=accessibleFiles[0]["creationDate"] - ) + # Return first valid file with matching hash (for general duplicate detection) + for file in accessibleFiles: + # Skip files without fileName key or with None/empty fileName + if "fileName" not in file or not file["fileName"]: + continue + # Use first valid file + return FileItem( + id=file["id"], + mandateId=file["mandateId"], + fileName=file["fileName"], + mimeType=file["mimeType"], + fileHash=file["fileHash"], + fileSize=file["fileSize"], + creationDate=file["creationDate"] + ) + + # If no valid files found, return None + return None def getMimeType(self, fileName: str) -> str: """Determines the MIME type based on the file extension.""" @@ -454,15 +465,23 @@ class ComponentObjects: fileItems = [] for file in filteredFiles: try: + # Ensure proper values, use defaults for invalid data + creationDate = file.get("creationDate") + if creationDate is None or not isinstance(creationDate, (int, float)) or creationDate <= 0: + creationDate = get_utc_timestamp() + + fileName = file.get("fileName") + if not fileName or fileName == "None": + continue # Skip records with invalid fileName + fileItem = FileItem( id=file.get("id"), mandateId=file.get("mandateId"), - fileName=file.get("fileName"), + fileName=fileName, mimeType=file.get("mimeType"), - workflowId=file.get("workflowId"), fileHash=file.get("fileHash"), fileSize=file.get("fileSize"), - creationDate=file.get("creationDate") + creationDate=creationDate ) fileItems.append(fileItem) except Exception as e: @@ -511,6 +530,9 @@ class ComponentObjects: # Check if fileName exists (excluding the current file if updating) for file in files: + # Skip files without fileName key or with None/empty fileName + if "fileName" not in file or not file["fileName"]: + continue if file["fileName"] == fileName and (excludeFileId is None or file["id"] != excludeFileId): return False return True @@ -544,9 +566,12 @@ class ComponentObjects: fileSize = len(content) fileHash = hashlib.sha256(content).hexdigest() + # Ensure mandateId is valid + mandateId = self.currentUser.mandateId or "default" + # Create FileItem instance fileItem = FileItem( - mandateId=self.currentUser.mandateId, + mandateId=mandateId, fileName=uniqueName, mimeType=mimeType, fileSize=fileSize, diff --git a/modules/routes/routeDataFiles.py b/modules/routes/routeDataFiles.py index 7f63dac4..2be59f21 100644 --- a/modules/routes/routeDataFiles.py +++ b/modules/routes/routeDataFiles.py @@ -86,13 +86,13 @@ async def upload_file( ) # Save file via LucyDOM interface in the database - fileItem, duplicateType = managementInterface.saveUploadedFile(fileContent, file.fileName) + fileItem, duplicateType = managementInterface.saveUploadedFile(fileContent, file.filename) # Determine response message based on duplicate type if duplicateType == "exact_duplicate": - message = f"File '{file.fileName}' already exists with identical content. Reusing existing file." + message = f"File '{file.filename}' already exists with identical content. Reusing existing file." elif duplicateType == "name_conflict": - message = f"File '{file.fileName}' already exists with different content. Uploaded as '{fileItem.fileName}'." + message = f"File '{file.filename}' already exists with different content. Uploaded as '{fileItem.fileName}'." else: # new_file message = "File uploaded successfully" @@ -110,7 +110,7 @@ async def upload_file( "message": message, "file": fileMeta, "duplicateType": duplicateType, - "originalFileName": file.fileName, + "originalFileName": file.filename, "storedFileName": fileItem.fileName, "isDuplicate": duplicateType != "new_file" }) @@ -315,11 +315,15 @@ async def download_file( ) # Return file as response + # Properly encode filename for Content-Disposition header to handle Unicode characters + import urllib.parse + encoded_filename = urllib.parse.quote(fileData.fileName) + return Response( content=fileContent, media_type=fileData.mimeType, headers={ - "Content-Disposition": f"attachment; fileName={fileData.fileName}" + "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}" } ) except HTTPException: