-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.go
More file actions
97 lines (79 loc) · 1.76 KB
/
file.go
File metadata and controls
97 lines (79 loc) · 1.76 KB
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func processFile(filePath string, decryptionKey []byte) error {
outFile := getOutputFilePath(filePath)
err := decryptFile(filePath, outFile, decryptionKey)
if err != nil {
log.Fatal(err)
}
// Delete old encrypted file
err = os.Remove(filePath)
if err != nil {
log.Fatal(err)
}
return nil
}
func isEncryptedFile(ext string) bool {
switch ext {
case ".rpgmvp", ".rpgmvm", ".rpgmvo": // MV
return true
case ".png_", ".m4a_", ".ogg_": // MZ
return true
}
return false
}
func getRealExt(oldExt string) (string, error) {
switch strings.ToLower(oldExt) {
case ".rpgmvp":
return ".png", nil
case ".rpgmvm":
return ".m4a", nil
case ".rpgmvo":
return ".ogg", nil
case ".png_":
return ".png", nil
case ".m4a_":
return ".m4a", nil
case ".ogg_":
return ".ogg", nil
}
return "", fmt.Errorf("unknown extension")
}
func getOutputFilePath(filePath string) string {
oldExt := filepath.Ext(filePath)
newExt, err := getRealExt(oldExt)
if err != nil {
log.Fatal(err)
}
fileName := filepath.Base(filePath)
fileName = fileName[0 : len(fileName)-len(oldExt)]
fileName = fileName + newExt
filePath = filepath.Join(filepath.Dir(filePath), fileName)
return filePath
}
func readFileContents(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("can't open %s: %s", filePath, err)
}
defer file.Close()
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %s", filePath, err)
}
return bytes, nil
}
func writeFileContents(filePath string, content *[]byte) error {
err := ioutil.WriteFile(filePath, *content, 0644)
if err != nil {
return err
}
return nil
}