/
exif.go
82 lines (68 loc) · 1.9 KB
/
exif.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
package helpers
import (
"bytes"
"encoding/binary"
"encoding/json"
"os"
"os/exec"
"path"
"strconv"
"go.mau.fi/libsignal/logger"
)
func WebpWriteExifData(inputData []byte, updateId int64) ([]byte, error) {
var (
startingBytes = []byte{0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00}
endingBytes = []byte{0x16, 0x00, 0x00, 0x00}
b bytes.Buffer
currUpdateId = strconv.FormatInt(updateId, 10)
currPath = path.Join("downloads", currUpdateId)
inputPath = path.Join(currPath, "input_exif.webm")
outputPath = path.Join(currPath, "output_exif.webp")
exifDataPath = path.Join(currPath, "raw.exif")
)
if _, err := b.Write(startingBytes); err != nil {
return nil, err
}
jsonData := map[string]interface{}{
"sticker-pack-id": "drizion.dev",
"sticker-pack-name": "wabot.net 🤖",
"sticker-pack-publisher": "@eu_drizion (segue aí rs)",
"emojis": []string{"😀"},
}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return nil, err
}
jsonLength := (uint32)(len(jsonBytes))
lenBuffer := make([]byte, 4)
binary.LittleEndian.PutUint32(lenBuffer, jsonLength)
if _, err := b.Write(lenBuffer); err != nil {
return nil, err
}
if _, err := b.Write(endingBytes); err != nil {
return nil, err
}
if _, err := b.Write(jsonBytes); err != nil {
return nil, err
}
if err := os.MkdirAll(currPath, os.ModePerm); err != nil {
return nil, err
}
defer os.RemoveAll(currPath)
if err := os.WriteFile(inputPath, inputData, os.ModePerm); err != nil {
return nil, err
}
if err := os.WriteFile(exifDataPath, b.Bytes(), os.ModePerm); err != nil {
return nil, err
}
cmd := exec.Command("webpmux",
"-set", "exif",
exifDataPath, inputPath,
"-o", outputPath,
)
if err := cmd.Run(); err != nil {
logger.Debug("failed to run webpmux command")
return nil, err
}
return os.ReadFile(outputPath)
}