Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Fix issue allowing multiple files with the same name - auto-rename on upload and rename operations#309

Merged
danielalves96 merged 7 commits intonextfrom
copilot/auto-rename-duplicate-files
Oct 20, 2025
Merged

Fix issue allowing multiple files with the same name - auto-rename on upload and rename operations#309
danielalves96 merged 7 commits intonextfrom
copilot/auto-rename-duplicate-files

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 20, 2025

Summary

This PR implements automatic renaming of files and folders when a duplicate name is detected, similar to Windows behavior. When a duplicate is detected, the system automatically appends a numeric suffix with a space (e.g., file (1).txt, folder (2)) to ensure unique names.

Changes Made

  • Understand the existing file registration flow in the server
  • Create utility functions to generate unique filenames and folder names with suffix
  • Modify registerFile method to check for duplicate filenames in the same folder
  • Modify checkFile method to check for duplicate filenames and suggest renamed file
  • Modify updateFile method to auto-rename files on rename operations
  • Modify registerFolder method to auto-rename folders on creation
  • Modify updateFolder method to auto-rename folders on rename operations
  • Update the response to include the renamed filename if applicable
  • Test the implementation manually (all tests passed)
  • Run validation (lint and type-check) on the changes
  • Request code review (no issues found)
  • Run security scan (0 alerts)
  • Fix Next.js 15 TypeScript errors in layout files
  • Update suffix format to include space before number for better readability

Implementation Details

New Files

  • apps/server/src/utils/file-name-generator.ts: Contains utilities for auto-renaming
    • generateUniqueFileName(): For file uploads
    • generateUniqueFileNameForRename(): For file rename operations
    • generateUniqueFolderName(): For folder creation and rename operations
    • parseFileName(): Parse filename into base and extension

Modified Files

  • apps/server/src/modules/file/controller.ts:
    • Updated registerFile() to auto-rename on upload
    • Updated checkFile() to return suggested name
    • Updated updateFile() to auto-rename on rename
  • apps/server/src/modules/folder/controller.ts:
    • Updated registerFolder() to auto-rename on creation
    • Updated updateFolder() to auto-rename on rename (instead of returning error)
  • apps/server/.gitignore: Added build artifacts exclusion
  • apps/web/src/app/(shares)/r/[alias]/layout.tsx: Fixed Next.js 15 async headers() usage
  • apps/web/src/app/(shares)/s/[alias]/layout.tsx: Fixed Next.js 15 async headers() usage

Testing Results

All tests passed:

  • ✅ Parse filename with single extension
  • ✅ Parse filename with multiple dots
  • ✅ No duplicate returns original name
  • ✅ First duplicate gets (1) suffix with space
  • ✅ Second duplicate gets (2) suffix with space
  • ✅ Same name in different folder is allowed
  • ✅ Rename file - no conflict
  • ✅ Rename file - conflict exists
  • ✅ Rename folder - no conflict
  • ✅ Rename folder - conflict exists
  • ✅ Create folder with conflict

Validation Results

  • ✅ Server app: ESLint passes
  • ✅ Web app: ESLint and TypeScript passes
  • ✅ Docs app: ESLint and TypeScript passes

Behavior

File Operations:

  • Upload: Files with duplicate names in the same folder are automatically renamed: filename (N).extension
  • Rename: When renaming a file, if the new name conflicts with an existing file in the same folder, it's automatically renamed with a suffix

Folder Operations:

  • Create: When creating a folder, if the name conflicts with an existing folder in the same location, it's automatically renamed: foldername (N)
  • Rename: When renaming a folder, if the new name conflicts with an existing folder in the same location, it's automatically renamed with a suffix

General:

  • Files/folders with the same name in different folders are allowed (no conflict)
  • The checkFile API returns suggestedName if the filename will be renamed
  • Renaming logic respects folder boundaries
  • Behavior matches Windows OS file/folder naming conventions
  • Suffix format includes space before number for better readability (e.g., navarro (1) instead of navarro(1))

Security Summary

✅ CodeQL security scan completed with 0 alerts

  • No SQL injection risks (using Prisma ORM)
  • User isolation maintained
  • Folder access controls respected
  • No path traversal vulnerabilities

Fixes #266

Original prompt

This section details on the original issue you should resolve

<issue_title>multiple file with same name is allowed exist in the same directory</issue_title>
<issue_description>which can be confusing, better to auto rename it with extra suffix</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #266


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 20, 2025 14:21
Co-authored-by: danielalves96 <62755605+danielalves96@users.noreply.github.com>
Co-authored-by: danielalves96 <62755605+danielalves96@users.noreply.github.com>
@danielalves96
Copy link
Copy Markdown
Contributor

@copilot this error is related with linter. fix please.

Co-authored-by: danielalves96 <62755605+danielalves96@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 20, 2025

@copilot this error is related with linter. fix please.

Fixed TypeScript errors in Next.js 15 layout files by making getBaseUrl() async and awaiting the headers() call. All validation checks now pass. Commit: 72a3fab

@danielalves96
Copy link
Copy Markdown
Contributor

@copilot the auto renaming is working perfectly on uploads, but for renaming doesn`t works. i need this for renaming files or folders too. like windows os

Co-authored-by: danielalves96 <62755605+danielalves96@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 20, 2025

@copilot the auto renaming is working perfectly on uploads, but for renaming doesn`t works. i need this for renaming files or folders too. like windows os

Implemented auto-renaming for both file and folder rename operations. Now when you rename a file or folder to a name that already exists in the same location, it will automatically append a numeric suffix like (1), (2), etc., just like Windows does. This works for:

  • File renames (updateFile)
  • Folder renames (updateFolder)
  • Folder creation (registerFolder)

All tests passed. Commit: 6872c95

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkpoint.prisma.io
    • Triggering command: /usr/local/bin/node /home/REDACTED/work/Palmr/Palmr/apps/server/node_modules/.pnpm/prisma@6.11.1_typescript@5.8.3/node_modules/prisma/build/child {&#34;product&#34;:&#34;prisma&#34;,&#34;version&#34;:&#34;6.11.1&#34;,&#34;cli_install_type&#34;:&#34;local&#34;,&#34;information&#34;:&#34;&#34;,&#34;local_timestamp&#34;:&#34;2025-10-20T14:54:42Z&#34;,&#34;project_hash&#34;:&#34;91d7cb56&#34;,&#34;cli_path&#34;:&#34;/home/REDACTED/work/Palmr/Palmr/apps/server/node_modules/prisma/build/index.js&#34;,&#34;cli_path_hash&#34;:&#34;41b5463a&#34;,&#34;endpoint&#34;:&#34;REDACTED&#34;,&#34;disable&#34;:false,&#34;arch&#34;:&#34;x64&#34;,&#34;os&#34;:&#34;linux&#34;,&#34;node_version&#34;:&#34;v20.19.5&#34;,&#34;ci&#34;:true,&#34;ci_name&#34;:&#34;GitHub Actions&#34;,&#34;command&#34;:&#34;generate&#34;,&#34;schema_providers&#34;:[&#34;sqlite&#34;],&#34;schema_preview_features&#34;:[],&#34;schema_generators_providers&#34;:[&#34;prisma-client-js&#34;],&#34;cache_file&#34;:&#34;/home/REDACTED/.cache/checkpoint-nodejs/prisma-41b5463a&#34;,&#34;cache_duration&#34;:43200000,&#34;remind_duration&#34;:172800000,&#34;force&#34;:false,&#34;timeout&#34;:5000,&#34;unref&#34;:true,&#34;child_path&#34;:&#34;/home/REDACTED/work/Palmr/Palmr/apps/server/node_modules/.pnpm/prisma@6.11.1_typescript@5.8.3/node_modules/prisma/build/child&#34;,&#34;client_event_id&#34;:&#34;&#34;,&#34;previous_client_event_id&#34;:&#34;&#34;,&#34;check_if_update_available&#34;:true} (dns block)
    • Triggering command: /usr/local/bin/node /home/REDACTED/work/Palmr/Palmr/apps/server/node_modules/.pnpm/prisma@6.11.1_typescript@5.8.3/node_modules/prisma/build/child {&#34;product&#34;:&#34;prisma&#34;,&#34;version&#34;:&#34;6.11.1&#34;,&#34;cli_install_type&#34;:&#34;local&#34;,&#34;information&#34;:&#34;&#34;,&#34;local_timestamp&#34;:&#34;2025-10-20T14:55:55Z&#34;,&#34;project_hash&#34;:&#34;91d7cb56&#34;,&#34;cli_path&#34;:&#34;/home/REDACTED/work/Palmr/Palmr/apps/server/node_modules/prisma/build/index.js&#34;,&#34;cli_path_hash&#34;:&#34;41b5463a&#34;,&#34;endpoint&#34;:&#34;REDACTED&#34;,&#34;disable&#34;:false,&#34;arch&#34;:&#34;x64&#34;,&#34;os&#34;:&#34;linux&#34;,&#34;node_version&#34;:&#34;v20.19.5&#34;,&#34;ci&#34;:true,&#34;ci_name&#34;:&#34;GitHub Actions&#34;,&#34;command&#34;:&#34;db push --accept-data-loss&#34;,&#34;schema_providers&#34;:[&#34;sqlite&#34;],&#34;schema_preview_features&#34;:[],&#34;schema_generators_providers&#34;:[&#34;prisma-client-js&#34;],&#34;cache_file&#34;:&#34;/home/REDACTED/.cache/checkpoint-nodejs/prisma-41b5463a&#34;,&#34;cache_duration&#34;:43200000,&#34;remind_duration&#34;:172800000,&#34;force&#34;:false,&#34;timeout&#34;:5000,&#34;unref&#34;:true,&#34;child_path&#34;:&#34;/home/REDACTED/work/Palmr/Palmr/apps/server/node_modules/.pnpm/prisma@6.11.1_typescript@5.8.3/node_modules/prisma/build/child&#34;,&#34;client_event_id&#34;:&#34;&#34;,&#34;previous_client_event_id&#34;:&#34;&#34;,&#34;check_if_update_available&#34;:true} (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix issue allowing multiple files with the same name Fix issue allowing multiple files with the same name - auto-rename on upload and rename operations Oct 20, 2025
@danielalves96
Copy link
Copy Markdown
Contributor

danielalves96 commented Oct 20, 2025

@copilot would be nice add an space before the version of renaming example:
actual: navarro(1)
nice: navarro (1)

and when i renaming, the file doest loaded (for downloads and poreviews)
error in renamed file:
GET http://localhost:3000/api/filesystem/download/40b04ee188cf46012d78c97d187ad63f70c11a464fc531403cf421623f259bcf 500 (Internal Server Error)

Co-authored-by: danielalves96 <62755605+danielalves96@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 20, 2025

@copilot would be nice add an space before the version of renaming example:
actual: navarro(1)
nice: navarro (1)
...

Updated the suffix format to include a space before the number. Now it generates names like navarro (1) instead of navarro(1), matching the style you requested. All tests pass with the new format. Commit: b1a7bcf

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkpoint.prisma.io

If you need me to access, download, or install something from one of these locations, you can either:

@danielalves96
Copy link
Copy Markdown
Contributor

@copilot and when i renaming, the file doest loaded (for downloads and poreviews)
error in renamed file:
GET http://localhost:3000/api/filesystem/download/40b04ee188cf46012d78c97d187ad63f70c11a464fc531403cf421623f259bcf 500 (Internal Server Error)

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 20, 2025

@copilot and when i renaming, the file doest loaded (for downloads and poreviews)
error in renamed file:
GET http://localhost:3000/api/filesystem/download/40b04ee188cf46012d78c97d187ad63f70c11a464fc...

I need more information to diagnose the issue. Can you please check the server logs and share the full error message? The 500 error suggests something is failing on the server side.

Also, can you confirm:

  1. Does the error happen immediately after renaming a file?
  2. Does it happen for all renamed files or only specific ones?
  3. Can you try renaming a file without creating a duplicate (so no suffix is added) and see if the download works?

The rename operation only changes the file name in the database, not the underlying file location (objectName), so downloads should continue to work. There might be an issue with how the renamed filename is being processed.

@danielalves96
Copy link
Copy Markdown
Contributor

@copilot
Requested file with password undefined
[DOWNLOAD] Error in 1760979150189-1c8p2enqe: Error: ENOENT: no such file or directory, stat '/Users/daniel/clones/Palmr/apps/server/uploads/cmddjchw80000gmiimqnxga2g/1758805195682-Rjn9at692HdR.png'
at async Object.stat (node:internal/fs/promises:1032:18)
at async FilesystemController.download (/Users/daniel/clones/Palmr/apps/server/src/modules/filesystem/controller.ts:206:21) {
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: '/Users/daniel/clones/Palmr/apps/server/uploads/cmddjchw80000gmiimqnxga2g/1758805195682-Rjn9at692HdR.png'
}

@copilot and when i renaming, the file doest loaded (for downloads and poreviews)
error in renamed file:
GET http://localhost:3000/api/filesystem/download/40b04ee188cf46012d78c97d187ad63f70c11a464fc...

I need more information to diagnose the issue. Can you please check the server logs and share the full error message? The 500 error suggests something is failing on the server side.

Also, can you confirm:

  1. Does the error happen immediately after renaming a file?
  2. Does it happen for all renamed files or only specific ones?
  3. Can you try renaming a file without creating a duplicate (so no suffix is added) and see if the download works?

The rename operation only changes the file name in the database, not the underlying file location (objectName), so downloads should continue to work. There might be an issue with how the renamed filename is being processed.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 20, 2025

@copilot
Requested file with password undefined
[DOWNLOAD] Error in 1760979150189-1c8p2enqe: Error: ENOENT: no such file or directory, stat '/Users/daniel/clones/Palmr/apps/server/uploads/cmddjchw8...

The error shows the file doesn't exist at the expected path on disk. This is not related to the rename feature - the rename operation only changes the name field in the database, never the objectName (which is the actual file path).

The error ENOENT: no such file or directory means the physical file 1758805195682-Rjn9at692HdR.png is missing from the uploads folder, even though there's a database record for it. This can happen if:

  1. The file upload didn't complete successfully
  2. The file was manually deleted from the uploads folder
  3. There's a database/filesystem sync issue

To verify the rename feature isn't the cause, check if:

  • Newly uploaded files work correctly
  • Renamed files that were working before rename still work
  • The issue only affects old files

The rename logic only updates the display name, not the storage location, so it shouldn't affect file downloads.

- Added a new script to clean up orphan file records in the database.
- Introduced `fileExists` method in the StorageProvider interface and implemented it in S3StorageProvider.
- Enhanced FilesystemController to check for file existence before download.
- Minor formatting adjustments in the Prisma schema for consistency.
@danielalves96 danielalves96 marked this pull request as ready for review October 20, 2025 17:12
@danielalves96 danielalves96 merged commit df31b32 into next Oct 20, 2025
@danielalves96 danielalves96 deleted the copilot/auto-rename-duplicate-files branch October 21, 2025 17:11
anthony0030 pushed a commit to anthony0030/Palmr that referenced this pull request Jan 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants