-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
57 lines (47 loc) · 1.31 KB
/
convert.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
package main
import (
"encoding/hex"
"fmt"
"io"
"mime/multipart"
"path/filepath"
)
type ConverterFunction func([]byte) []byte
const (
trimBytes = 122
additionalBytesHex = "7C3C2D2D536E69702061626F7665206865726520746F2063726561746520612072617720736176206279206578636C7564696E672074686973204465536D754D4520736176656461746120666F6F7465723A0000010000000100030000000200000000000100000000007C2D4445534D554D4520534156452D7C"
)
var (
additionalBytes = MustDecode(hex.DecodeString(additionalBytesHex))
converterFunctions = map[string]ConverterFunction{
".dsv": DsvToSav,
".sav": SavToDsv,
}
)
func MustDecode(content []byte, err error) []byte {
if err != nil {
panic(42)
}
return content
}
func DsvToSav(input []byte) []byte {
return input[:len(input)-trimBytes]
}
func SavToDsv(input []byte) []byte {
return append(input, additionalBytes...)
}
func Convert(file multipart.File, h *multipart.FileHeader) ([]byte, error) {
// Check file extension
fileExt := filepath.Ext(h.Filename)
converterFunction, exists := converterFunctions[fileExt]
if !exists {
return nil, fmt.Errorf("invalid file format: only .dsv and .sav files are allowed")
}
// Read file contents
content, err := io.ReadAll(file)
if err != nil {
return nil, err
}
// Return converted file
return converterFunction(content), nil
}