-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathhandle_attachment.go
More file actions
147 lines (141 loc) · 4.07 KB
/
Copy pathhandle_attachment.go
File metadata and controls
147 lines (141 loc) · 4.07 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
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
143
144
145
146
147
package main
/*
#include "bridge.h"
*/
import "C"
import (
"context"
"encoding/hex"
"mime"
"os"
"path/filepath"
"strings"
"time"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/types"
)
func extension_from_mimetype(mimeType *string) string {
extension := ".data"
if mimeType != nil {
// use the most poplular default for some common mimetypes
if *mimeType == "image/jpeg" {
return ".jpg"
}
if *mimeType == "image/png" {
return ".png"
}
if *mimeType == "video/mp4" {
return ".mp4"
}
// anything else is looked up
extensions, _ := mime.ExtensionsByType(*mimeType)
if extensions != nil {
extension = extensions[0]
}
}
return extension
}
// based on https://github.com/FKLC/WhatsAppToDiscord/blob/master/WA2DC.go
func (handler *Handler) handle_attachment(message *waE2E.Message, id string, source types.MessageSource, timestamp time.Time) {
var (
caption = ""
length uint64 = 0
hash = ""
filename = ""
extension = ""
data_type C.int = C.gowhatsapp_attachment_type_none
downloadable whatsmeow.DownloadableMessage = nil
mimetype = ""
)
{
im := message.GetImageMessage()
if im != nil {
downloadable = im
hash = hex.EncodeToString(im.GetFileSHA256())
extension = extension_from_mimetype(im.Mimetype)
data_type = C.gowhatsapp_attachment_type_image
mimetype = im.GetMimetype()
length = im.GetFileLength()
caption = im.GetCaption()
}
}
{
vm := message.GetVideoMessage()
if vm != nil {
downloadable = vm
hash = hex.EncodeToString(vm.GetFileSHA256())
extension = extension_from_mimetype(vm.Mimetype)
data_type = C.gowhatsapp_attachment_type_video
mimetype = vm.GetMimetype()
length = vm.GetFileLength()
caption = vm.GetCaption()
}
}
{
ptv := message.GetPtvMessage()
if ptv != nil {
downloadable = ptv
hash = hex.EncodeToString(ptv.GetFileSHA256())
extension = extension_from_mimetype(ptv.Mimetype)
data_type = C.gowhatsapp_attachment_type_video
mimetype = ptv.GetMimetype()
length = ptv.GetFileLength()
caption = ptv.GetCaption()
}
}
{
am := message.GetAudioMessage()
if am != nil {
downloadable = am
hash = hex.EncodeToString(am.GetFileSHA256())
extension = extension_from_mimetype(am.Mimetype)
data_type = C.gowhatsapp_attachment_type_audio
mimetype = am.GetMimetype()
length = am.GetFileLength()
}
}
{
sm := message.GetStickerMessage()
if sm != nil {
downloadable = sm
hash = hex.EncodeToString(sm.GetFileSHA256())
extension = extension_from_mimetype(sm.Mimetype)
data_type = C.gowhatsapp_attachment_type_sticker
mimetype = sm.GetMimetype()
length = sm.GetFileLength()
}
}
{
dm := message.GetDocumentMessage()
if dm != nil {
downloadable = dm
hash = hex.EncodeToString(dm.GetFileSHA256())
filename = dm.GetFileName() // TODO: sanitize filename
extension = filepath.Ext(filename)
if extension != "" {
// remove extension from filename for consistency when using file-naming template
filename = strings.TrimSuffix(filename, extension)
} else {
extension = extension_from_mimetype(dm.Mimetype)
}
data_type = C.gowhatsapp_attachment_type_document
mimetype = dm.GetMimetype()
length = dm.GetFileLength()
caption = dm.GetCaption()
}
}
if data_type != C.gowhatsapp_attachment_type_none {
chat := source.Chat.ToNonAD().String()
sender := source.Sender.ToNonAD().String()
purple_handle_attachment(handler.account, chat, source.IsGroup, sender, caption, id, timestamp, data_type, filename, extension, mimetype, hash, length, downloadable)
}
}
func (handler *Handler) download_attachment(local_file_path string, message whatsmeow.DownloadableMessage) error {
os.MkdirAll(filepath.Dir(local_file_path), 0o755)
file, err := os.Create(local_file_path)
if err != nil {
return err
}
return handler.client.DownloadToFile(context.TODO(), message, file)
}