diff --git a/backup.go b/backup.go index 02ab0be..cb116ab 100644 --- a/backup.go +++ b/backup.go @@ -25,6 +25,28 @@ func getBackupPath(gridDir string, game *Game) string { return filepath.Join(gridDir, "originals", game.ID+" "+hexHash+game.ImageExt) } +func RemoveExisting(gridDir string, gameId string) error { + images, err := filepath.Glob(filepath.Join(gridDir, gameId+".*")) + if err != nil { + return err + } + + backups, err := filepath.Glob(filepath.Join(gridDir, "originals", gameId+" *.*")) + if err != nil { + return err + } + + all := append(images, backups...) + for _, path := range all { + err = os.Remove(path) + if err != nil { + return err + } + } + + return nil +} + func loadImage(game *Game, sourceName string, imagePath string) error { imageBytes, err := ioutil.ReadFile(imagePath) if err == nil { diff --git a/steamgrid.go b/steamgrid.go index bf8578a..74dc841 100644 --- a/steamgrid.go +++ b/steamgrid.go @@ -72,15 +72,17 @@ func startApplication() { fmt.Println("Loading existing images and backups...") - // From this point onward we could delete the entire grid/ dir, because all relevant data is loaded in 'games'. - // We don't, and this builds up useless files (TODO), but is better than accidentally deleting data. - i := 0 for _, game := range games { i++ overridePath := filepath.Join(filepath.Dir(os.Args[0]), "games") LoadExisting(overridePath, gridDir, game) + // This cleans up unused backups and images for the same game but with different extensions. + err = RemoveExisting(gridDir, game.ID) + if err != nil { + fmt.Println(err.Error()) + } var name string if game.Name != "" {