Skip to content

Commit

Permalink
Fix: properly handle duplicate files on delete (#6173)
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare authored Feb 6, 2025
1 parent 0b7368e commit c8db2fc
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/backend/base/langflow/api/v2/files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import uuid
from collections.abc import AsyncGenerator
from http import HTTPStatus
Expand Down Expand Up @@ -101,10 +102,19 @@ async def upload_user_file(

# If there are files with the same name, append a count to the filename
if files:
count = len(files) # Count occurrences
counts = []

# Extract the count from the filename
for my_file in files:
match = re.search(r"\((\d+)\)(?=\.\w+$|$)", my_file.name) # Match (number) before extension or at end
if match:
counts.append(int(match.group(1)))

# Get the max count and increment by 1
count = max(counts) if counts else 0 # Default to 0 if no matches found

# Split the extension from the filename
root_filename = f"{root_filename} ({count})"
root_filename = f"{root_filename} ({count + 1})"

# Compute the file size based on the path
file_size = await storage_service.get_file_size(flow_id=folder, file_name=anonymized_file_name)
Expand Down Expand Up @@ -226,3 +236,32 @@ async def delete_file(
raise HTTPException(status_code=500, detail=f"Error deleting file: {e}") from e

return {"message": "File deleted successfully"}


@router.delete("")
async def delete_all_files(
current_user: CurrentActiveUser,
session: DbSession,
storage_service: Annotated[StorageService, Depends(get_storage_service)],
):
"""Delete all files for the current user."""
try:
# Fetch all files from the DB
stmt = select(UserFile).where(UserFile.user_id == current_user.id)
results = await session.exec(stmt)
files = results.all()

# Delete all files from the storage service
for file in files:
await storage_service.delete_file(flow_id=str(current_user.id), file_name=file.path)
await session.delete(file)

# Delete all files from the database
await session.flush() # Ensures delete is staged
await session.commit() # Commit deletion

except Exception as e:
await session.rollback() # Rollback on failure
raise HTTPException(status_code=500, detail=f"Error deleting files: {e}") from e

return {"message": "All files deleted successfully"}

0 comments on commit c8db2fc

Please sign in to comment.