-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_zip.go
72 lines (58 loc) · 1.27 KB
/
handle_zip.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
package main
import (
"bytes"
"fmt"
"image/png"
"os"
"path/filepath"
"strings"
"golang.org/x/image/webp"
tb "gopkg.in/tucnak/telebot.v2"
)
func handleZip(bot *tb.Bot) func(*tb.Message) {
return func(m *tb.Message) {
if m.Document == nil {
bot.Send(m.Sender, "Invalid file")
return
}
if m.Document.MIME != "application/zip" {
bot.Send(m.Sender, "This doesn't look like a zip...")
}
if uint64(m.Document.FileSize) > maxSize {
bot.Send(m.Sender, fmt.Sprintf("File too big: max zip size %d", maxSize))
return
}
dest, err := unzip(&m.Document.File, bot)
if err != nil {
bot.Send(m.Sender, fmt.Sprintf("An error occurred: %s"), err.Error())
return
}
filepath.Walk(dest, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".webp") {
return nil
}
r, err := os.Open(path)
if err != nil {
return err
}
defer r.Close()
wa, err := webp.Decode(r)
if err != nil {
return err
}
buf := bytes.NewBuffer(make([]byte, int(info.Size())))
err = png.Encode(buf, wa)
if err != nil {
return err
}
stk := bot.UploadStickerFile(m.Sender, &tb.FromReader(buf))
return nil
})
}
}