Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit c7982ae

Browse files
authored
fix: allow upload file with same name (#1801)
1 parent 2ef085a commit c7982ae

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

engine/repositories/file_fs_repository.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,28 @@ cpp::result<void, std::string> FileFsRepository::StoreFile(
1818
}
1919

2020
cortex::db::File db;
21-
auto file_full_path = file_container_path / file_metadata.filename;
22-
if (std::filesystem::exists(file_full_path)) {
23-
return cpp::fail("File already exists: " + file_full_path.string());
21+
auto original_filename = file_metadata.filename;
22+
auto file_full_path = file_container_path / original_filename;
23+
24+
// Handle duplicate filenames
25+
int counter = 1;
26+
while (std::filesystem::exists(file_full_path)) {
27+
auto dot_pos = original_filename.find_last_of('.');
28+
std::string name_part;
29+
std::string ext_part;
30+
31+
if (dot_pos != std::string::npos) {
32+
name_part = original_filename.substr(0, dot_pos);
33+
ext_part = original_filename.substr(dot_pos);
34+
} else {
35+
name_part = original_filename;
36+
ext_part = "";
37+
}
38+
39+
auto new_filename = name_part + "_" + std::to_string(counter) + ext_part;
40+
file_full_path = file_container_path / new_filename;
41+
file_metadata.filename = new_filename;
42+
counter++;
2443
}
2544

2645
try {

0 commit comments

Comments
 (0)