forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia.go
More file actions
106 lines (93 loc) · 2.38 KB
/
Copy pathmedia.go
File metadata and controls
106 lines (93 loc) · 2.38 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
package main
import (
"context"
"mime"
"os"
"path/filepath"
"time"
"github.com/rs/zerolog/log"
"go.mau.fi/whatsmeow"
)
const (
downloadTimeoutImage = 2 * time.Minute
downloadTimeoutAudio = 5 * time.Minute
downloadTimeoutDocument = 10 * time.Minute
downloadTimeoutVideo = 10 * time.Minute
downloadTimeoutSticker = 1 * time.Minute
)
type mediaS3Config struct {
Enabled string
MediaDelivery string
}
func (mycli *MyClient) processMedia(
msg whatsmeow.DownloadableMessage,
mimeType string,
fallbackExt string,
timeout time.Duration,
isIncoming bool,
chatJID string,
messageID string,
s3cfg mediaS3Config,
postmap map[string]interface{},
extraKeys map[string]interface{},
) {
tmpDir := filepath.Join("/tmp", "user_"+mycli.userID)
if err := os.MkdirAll(tmpDir, 0751); err != nil {
log.Error().Err(err).Msg("Could not create temporary directory")
return
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
data, err := mycli.WAClient.Download(ctx, msg)
if err != nil {
log.Error().Err(err).Msg("Failed to download media")
return
}
ext := fallbackExt
if exts, _ := mime.ExtensionsByType(mimeType); len(exts) > 0 {
ext = exts[0]
}
tmpPath := filepath.Join(tmpDir, messageID+ext)
if err := os.WriteFile(tmpPath, data, 0600); err != nil {
log.Error().Err(err).Msg("Failed to save media to temporary file")
return
}
defer func() {
if err := os.Remove(tmpPath); err != nil {
log.Error().Err(err).Msg("Failed to delete temporary file")
} else {
log.Info().Str("path", tmpPath).Msg("Temporary file deleted")
}
}()
if s3cfg.Enabled == "true" && (s3cfg.MediaDelivery == "s3" || s3cfg.MediaDelivery == "both") {
s3Data, err := GetS3Manager().ProcessMediaForS3(
ctx,
mycli.userID,
chatJID,
messageID,
data,
mimeType,
filepath.Base(tmpPath),
isIncoming,
)
if err != nil {
log.Error().Err(err).Msg("Failed to upload media to S3")
} else {
postmap["s3"] = s3Data
}
}
if s3cfg.MediaDelivery == "base64" || s3cfg.MediaDelivery == "both" {
b64, mime_, err := fileToBase64(tmpPath)
if err != nil {
log.Error().Err(err).Msg("Failed to convert media to base64")
return
}
postmap["base64"] = b64
postmap["mimeType"] = mime_
postmap["fileName"] = filepath.Base(tmpPath)
}
for k, v := range extraKeys {
postmap[k] = v
}
log.Info().Str("path", tmpPath).Msg("Media processed")
}