Skip to content

Commit 0318b95

Browse files
Luckiboppreh
authored andcommitted
Filter search results…
…for images. This prevents the deletion of other files than images like `appid.json` which holds special information like the logo position in the client. Fixes boppreh#64
1 parent d9b8f43 commit 0318b95

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

backup.go

+20
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ func RemoveExisting(gridDir string, gameId string, artStyleExtensions []string)
3333
if err != nil {
3434
return err
3535
}
36+
images = filterForImages(images)
3637

3738
backups, err := filepath.Glob(filepath.Join(gridDir, "originals", gameId + artStyleExtensions[0] + " *.*"))
3839
if err != nil {
3940
return err
4041
}
42+
backups = filterForImages(backups)
4143

4244
all := append(images, backups...)
4345
for _, path := range all {
@@ -77,12 +79,29 @@ func InsensitiveFilepath(path string) string {
7779
return p
7880
}
7981

82+
func filterForImages(paths []string) []string {
83+
var matchedPaths []string
84+
for _, path := range paths {
85+
ext := filepath.Ext(path)
86+
switch ext {
87+
case ".png":
88+
matchedPaths = append(matchedPaths, path)
89+
case ".jpg":
90+
matchedPaths = append(matchedPaths, path)
91+
case ".jpeg":
92+
matchedPaths = append(matchedPaths, path)
93+
}
94+
}
95+
return matchedPaths
96+
}
97+
8098
func LoadExisting(overridePath string, gridDir string, game *Game, artStyleExtensions []string) {
8199
overridenIDs, _ := filepath.Glob(filepath.Join(overridePath, game.ID + artStyleExtensions[0] + ".*"))
82100
if overridenIDs != nil && len(overridenIDs) > 0 {
83101
loadImage(game, "local file in directory 'games'", overridenIDs[0])
84102
return
85103
}
104+
overridenIDs = filterForImages(overridenIDs)
86105

87106
if game.Name != "" {
88107
re := regexp.MustCompile(`\W+`)
@@ -105,6 +124,7 @@ func LoadExisting(overridePath string, gridDir string, game *Game, artStyleExten
105124
}
106125

107126
files, err := filepath.Glob(filepath.Join(gridDir, game.ID + artStyleExtensions[0] + ".*"))
127+
files = filterForImages(files)
108128
if err == nil && len(files) > 0 {
109129
err = loadImage(game, "manual customization", files[0])
110130
if err == nil {

0 commit comments

Comments
 (0)