Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from paulhovey/existingFileRename
Browse files Browse the repository at this point in the history
Attempt at fixing existing filename issue when exporting
  • Loading branch information
ua741 authored Jan 5, 2024
2 parents e8558b1 + cc0ea56 commit 395d776
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
16 changes: 16 additions & 0 deletions pkg/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ func (a *albumDiskInfo) IsMetaFileNamePresent(metaFileName string) bool {
return ok
}

// GenerateUniqueMetaFileName generates a unique metafile name.
func (a *albumDiskInfo) GenerateUniqueMetaFileName(baseFileName, extension string) string {
potentialDiskFileName := fmt.Sprintf("%s%s.json", baseFileName, extension)
count := 1
for a.IsMetaFileNamePresent(potentialDiskFileName) {
// separate the file name and extension
fileName := fmt.Sprintf("%s_%d", baseFileName, count)
potentialDiskFileName = fmt.Sprintf("%s%s.json", fileName, extension)
count++
if !a.IsMetaFileNamePresent(potentialDiskFileName) {
break
}
}
return potentialDiskFileName
}

// GenerateUniqueFileName generates a unique file name.
func (a *albumDiskInfo) GenerateUniqueFileName(baseFileName, extension string) string {
fileName := fmt.Sprintf("%s%s", baseFileName, extension)
Expand Down
32 changes: 32 additions & 0 deletions pkg/disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pkg

import (
"path/filepath"
"strings"
"testing"
)

func TestGenerateUniqueFileName(t *testing.T) {
existingFilenames := make(map[string]bool)
testFilename := "FullSizeRender.jpg" // what Apple calls shared files

existingFilenames[strings.ToLower(testFilename)] = true

a := &albumDiskInfo{
FileNames: &existingFilenames,
}

// this is taken from downloadEntry()
extension := filepath.Ext(testFilename)
baseFileName := strings.TrimSuffix(filepath.Clean(filepath.Base(testFilename)), extension)

for i := 0; i < 100; i++ {
newFilename := a.GenerateUniqueFileName(baseFileName, extension)
if strings.Contains(newFilename, "_1_2") {
t.Fatalf("Filename contained _1_2")
} else {
// add generated name to existing files
existingFilenames[strings.ToLower(newFilename)] = true
}
}
}
16 changes: 3 additions & 13 deletions pkg/remote_to_disk_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,7 @@ func (c *ClICtrl) downloadEntry(ctx context.Context,
// Get the extension
extension := filepath.Ext(fileDiskMetadata.Title)
baseFileName := strings.TrimSuffix(filepath.Clean(filepath.Base(fileDiskMetadata.Title)), extension)
potentialDiskFileName := fmt.Sprintf("%s%s.json", baseFileName, extension)
count := 1
for diskInfo.IsMetaFileNamePresent(potentialDiskFileName) {
// separate the file name and extension
baseFileName = fmt.Sprintf("%s_%d", baseFileName, count)
potentialDiskFileName = fmt.Sprintf("%s%s.json", baseFileName, extension)
count++
if !diskInfo.IsMetaFileNamePresent(potentialDiskFileName) {
break
}
}
diskMetaFileName := diskInfo.GenerateUniqueMetaFileName(baseFileName, extension)
if file.IsLivePhoto() {
imagePath, videoPath, err := UnpackLive(*decrypt)
if err != nil {
Expand Down Expand Up @@ -176,13 +166,13 @@ func (c *ClICtrl) downloadEntry(ctx context.Context,
fileDiskMetadata.AddFileName(fileName)
}

fileDiskMetadata.MetaFileName = potentialDiskFileName
fileDiskMetadata.MetaFileName = diskMetaFileName
err = diskInfo.AddEntry(fileDiskMetadata)
if err != nil {
return err
}

err = writeJSONToFile(filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, ".meta", potentialDiskFileName), fileDiskMetadata)
err = writeJSONToFile(filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, ".meta", diskMetaFileName), fileDiskMetadata)
if err != nil {
return err
}
Expand Down

0 comments on commit 395d776

Please sign in to comment.