diff --git a/db/localSwitchFilesDB.go b/db/localSwitchFilesDB.go index ddff383..f31192b 100644 --- a/db/localSwitchFilesDB.go +++ b/db/localSwitchFilesDB.go @@ -96,12 +96,12 @@ func scanFolder(folder string, recursive bool, files *[]ExtendedFileInfo, progre if path == folder { return nil } - if info.IsDir() { + if err != nil { + zap.S().Error("Error while scanning folders", err) return nil } - if err != nil { - zap.S().Error("Error while scanning folders", err) + if info.IsDir() { return nil } @@ -184,6 +184,9 @@ func (ldb *LocalSwitchDBManager) processLocalFiles(files []ExtendedFileInfo, } switchTitle.Updates[metadata.Version] = SwitchFileInfo{ExtendedInfo: file, Metadata: metadata} if metadata.Version > switchTitle.LatestUpdate { + if switchTitle.LatestUpdate != 0 { + skipped[switchTitle.Updates[switchTitle.LatestUpdate].ExtendedInfo] = "old update file, newer update exist locally" + } switchTitle.LatestUpdate = metadata.Version } else { skipped[file] = "old update file, newer update exist locally" diff --git a/gui.go b/gui.go index 112bb6c..17fa250 100644 --- a/gui.go +++ b/gui.go @@ -314,7 +314,9 @@ func (g *GUI) buildLocalDB(localDbManager *db.LocalSwitchDBManager) (*db.LocalSw func (g *GUI) organizeLibrary() { folderToScan := settings.ReadSettings(g.baseFolder).Folder process.OrganizeByFolders(folderToScan, g.state.localDB, g.state.switchDB, g) - + if settings.ReadSettings(g.baseFolder).OrganizeOptions.DeleteOldUpdateFiles { + process.DeleteOldUpdates(g.state.localDB, g) + } } func (g *GUI) UpdateProgress(curr int, total int, message string) { diff --git a/process/organizefolderStructure.go b/process/organizefolderStructure.go index f1156e0..d6b7b96 100644 --- a/process/organizefolderStructure.go +++ b/process/organizefolderStructure.go @@ -10,7 +10,6 @@ import ( "path/filepath" "regexp" "robpike.io/nihongo" - "sort" "strconv" "strings" ) @@ -22,33 +21,28 @@ var ( func DeleteOldUpdates(localDB *db.LocalSwitchFilesDB, updateProgress db.ProgressUpdater) { i := 0 - for _, v := range localDB.TitlesMap { + for k, v := range localDB.TitlesMap { + if !v.BaseExist { + continue + } + i++ if updateProgress != nil { - updateProgress.UpdateProgress(i, len(localDB.TitlesMap), v.File.ExtendedInfo.Info.Name()) + updateProgress.UpdateProgress(i, len(localDB.TitlesMap), v.File.ExtendedInfo.Info.Name()+k) } if len(v.Updates) > 1 { - //sort the available local versions - localVersions := make([]int, len(v.Updates)) - i := 0 - for k := range v.Updates { - localVersions[i] = k - i++ - } - sort.Ints(localVersions) - for i := 0; i < len(localVersions)-1; i++ { - if localVersions[i] == 0 { - //should not happen, but make sure we do not delete base - continue - } - fileToRemove := filepath.Join(v.Updates[localVersions[i]].ExtendedInfo.BaseFolder, v.Updates[localVersions[i]].ExtendedInfo.Info.Name()) - zap.S().Infof("--> [Delete] Old update file: %v [latest update:%v]\n", fileToRemove, localVersions[len(localVersions)-1]) - err := os.Remove(fileToRemove) - if err != nil { - zap.S().Errorf("Failed to delete file %v [%v]\n", fileToRemove, err) + for version, update := range v.Updates { + if version < v.LatestUpdate && version != 0 { + fileToRemove := filepath.Join(update.ExtendedInfo.BaseFolder, update.ExtendedInfo.Info.Name()) + zap.S().Infof("--> [Delete] Old update file: %v [latest update:%v]\n", fileToRemove, v.LatestUpdate) + /*err := os.Remove(fileToRemove) + if err != nil { + zap.S().Errorf("Failed to delete file %v [%v]\n", fileToRemove, err) + }*/ } + } - v.Updates = map[int]db.SwitchFileInfo{localVersions[len(localVersions)-1]: v.Updates[localVersions[len(localVersions)-1]]} + //v.Updates = map[int]db.SwitchFileInfo{localVersions[len(localVersions)-1]: v.Updates[localVersions[len(localVersions)-1]]} } }