-
Notifications
You must be signed in to change notification settings - Fork 42
/
backup.go
142 lines (124 loc) · 3.96 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"unicode"
)
// BackupGame if a game has a custom image, backs it up by appending "(original)" to the
// file name.
func backupGame(gridDir string, game *Game, artStyleExtensions []string) error {
if game.CleanImageBytes != nil {
return ioutil.WriteFile(getBackupPath(gridDir, game, artStyleExtensions), game.CleanImageBytes, 0666)
}
return nil
}
func getBackupPath(gridDir string, game *Game, artStyleExtensions []string) string {
hash := sha256.Sum256(game.OverlayImageBytes)
// [:] is required to convert a fixed length byte array to a byte slice.
hexHash := hex.EncodeToString(hash[:])
return filepath.Join(gridDir, "originals", game.ID+artStyleExtensions[0]+" "+hexHash+game.ImageExt)
}
func removeExisting(gridDir string, gameID string, artStyleExtensions []string) error {
images, err := filepath.Glob(filepath.Join(gridDir, gameID+artStyleExtensions[0]+".*"))
if err != nil {
return err
}
images = filterForImages(images)
backups, err := filepath.Glob(filepath.Join(gridDir, "originals", gameID+artStyleExtensions[0]+" *.*"))
if err != nil {
return err
}
backups = filterForImages(backups)
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 {
game.ImageExt = filepath.Ext(imagePath)
game.CleanImageBytes = imageBytes
game.ImageSource = sourceName
}
return err
}
// https://wenzr.wordpress.com/2018/04/09/go-glob-case-insensitive/
func insensitiveFilepath(path string) string {
if runtime.GOOS == "windows" {
return path
}
p := ""
for _, r := range path {
if unicode.IsLetter(r) {
p += fmt.Sprintf("[%c%c]", unicode.ToLower(r), unicode.ToUpper(r))
} else {
p += string(r)
}
}
return p
}
func filterForImages(paths []string) []string {
var matchedPaths []string
for _, path := range paths {
ext := filepath.Ext(path)
switch ext {
case ".png":
matchedPaths = append(matchedPaths, path)
case ".jpg":
matchedPaths = append(matchedPaths, path)
case ".jpeg":
matchedPaths = append(matchedPaths, path)
}
}
return matchedPaths
}
func loadExisting(overridePath string, gridDir string, game *Game, artStyleExtensions []string) {
overridenIDs, _ := filepath.Glob(filepath.Join(overridePath, game.ID+artStyleExtensions[0]+".*"))
if overridenIDs != nil && len(overridenIDs) > 0 {
loadImage(game, "local file in directory 'games'", overridenIDs[0])
return
}
overridenIDs = filterForImages(overridenIDs)
if game.Name != "" {
re := regexp.MustCompile(`\W+`)
globName := re.ReplaceAllString(game.Name, "*")
overridenNames, _ := filepath.Glob(filepath.Join(overridePath, insensitiveFilepath(globName)+artStyleExtensions[1]+".*"))
if overridenNames != nil && len(overridenNames) > 0 {
loadImage(game, "local file in directory games/", overridenNames[0])
return
}
}
// If there are any old-style backups (without hash), load them over the existing (with overlay) images.
oldBackups, err := filepath.Glob(filepath.Join(gridDir, game.ID+artStyleExtensions[0]+" (original)*"))
if err == nil && len(oldBackups) > 0 {
err = loadImage(game, "legacy backup (now converted)", oldBackups[0])
if err == nil {
os.Remove(oldBackups[0])
return
}
}
files, err := filepath.Glob(filepath.Join(gridDir, game.ID+artStyleExtensions[0]+".*"))
files = filterForImages(files)
if err == nil && len(files) > 0 {
err = loadImage(game, "manual customization", files[0])
if err == nil {
// set as overlay to check for hash in getBackupPath()
game.OverlayImageBytes = game.CleanImageBytes
// See if there exists a backup image with no overlays or modifications.
loadImage(game, "backup", getBackupPath(gridDir, game, artStyleExtensions))
// remove overlay
game.OverlayImageBytes = nil
}
}
}