Skip to content

Commit 3a77a5c

Browse files
author
cyk
committed
feat(google_drive): 添加处理重复文件名的功能,确保文件名唯一
1 parent 2b0d33e commit 3a77a5c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

drivers/google_drive/util.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,60 @@ func (d *GoogleDrive) getFiles(id string) ([]File, error) {
296296

297297
res = append(res, resp.Files...)
298298
}
299+
300+
// Handle duplicate filenames by adding suffixes like (1), (2), etc.
301+
// Google Drive allows multiple files with the same name in one folder,
302+
// but OpenList uses path-based file system which requires unique names
303+
res = handleDuplicateNames(res)
304+
299305
return res, nil
300306
}
301307

308+
// handleDuplicateNames adds suffixes to duplicate filenames to make them unique
309+
// For example: file.txt, file (1).txt, file (2).txt
310+
func handleDuplicateNames(files []File) []File {
311+
if len(files) <= 1 {
312+
return files
313+
}
314+
315+
// Track how many files with each name we've seen
316+
nameCount := make(map[string]int)
317+
318+
// First pass: count occurrences of each name
319+
for _, file := range files {
320+
nameCount[file.Name]++
321+
}
322+
323+
// Second pass: add suffixes to duplicates
324+
nameIndex := make(map[string]int)
325+
for i := range files {
326+
name := files[i].Name
327+
if nameCount[name] > 1 {
328+
index := nameIndex[name]
329+
nameIndex[name]++
330+
331+
if index > 0 {
332+
// Add suffix for all except the first occurrence
333+
// Split name into base and extension
334+
ext := ""
335+
base := name
336+
for j := len(name) - 1; j >= 0; j-- {
337+
if name[j] == '.' {
338+
ext = name[j:]
339+
base = name[:j]
340+
break
341+
}
342+
}
343+
344+
// Add (1), (2), etc. suffix
345+
files[i].Name = fmt.Sprintf("%s (%d)%s", base, index, ext)
346+
}
347+
}
348+
}
349+
350+
return files
351+
}
352+
302353
// getTargetFileInfo gets target file details for shortcuts
303354
func (d *GoogleDrive) getTargetFileInfo(targetId string) (File, error) {
304355
var targetFile File

0 commit comments

Comments
 (0)