@rexxhayanasi/elaina-baileys 1.2.0-rc.3 → 1.2.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Utils/index.js +1 -0
- package/lib/Utils/messages.js +7 -1
- package/lib/Utils/newslettermedia.js +143 -0
- package/package.json +1 -1
package/lib/Utils/index.js
CHANGED
|
@@ -34,3 +34,4 @@ __exportStar(require("./process-message"), exports);
|
|
|
34
34
|
__exportStar(require("./message-retry-manager"), exports);
|
|
35
35
|
__exportStar(require("./browser-utils"), exports);
|
|
36
36
|
__exportStar(require("./resolveJid"), exports);
|
|
37
|
+
__exportStar(require("./newslettermedia"), exports);
|
package/lib/Utils/messages.js
CHANGED
|
@@ -117,8 +117,10 @@ const assertColor = async (color) => {
|
|
|
117
117
|
media: message[mediaType] || message['ptv']
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
+
if (!(mediaType === 'audio' && uploadData.ptt)) {
|
|
120
121
|
delete uploadData[mediaType];
|
|
121
|
-
|
|
122
|
+
}
|
|
123
|
+
delete uploadData['ptv'];
|
|
122
124
|
|
|
123
125
|
const cacheableKey = typeof uploadData.media === 'object' &&
|
|
124
126
|
('url' in uploadData.media) &&
|
|
@@ -184,6 +186,10 @@ const assertColor = async (color) => {
|
|
|
184
186
|
})
|
|
185
187
|
});
|
|
186
188
|
|
|
189
|
+
if (mediaType === 'audio' && uploadData.ptt) {
|
|
190
|
+
obj.audioMessage.ptt = true;
|
|
191
|
+
}
|
|
192
|
+
|
|
187
193
|
if (isPtv) {
|
|
188
194
|
obj.ptvMessage = obj.videoMessage;
|
|
189
195
|
delete obj.videoMessage;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const Boom = require("@hapi/boom");
|
|
5
|
+
const { WAProto } = require("../../WAProto");
|
|
6
|
+
const { MEDIA_PATH_MAP, uploadMedia } = require("./Utils");
|
|
7
|
+
const { DEFAULT_ORIGIN } = require("../Defaults");
|
|
8
|
+
const { unixTimestampSeconds } = require("./generics");
|
|
9
|
+
const {
|
|
10
|
+
generateThumbnail,
|
|
11
|
+
encryptedStream,
|
|
12
|
+
getAudioDuration,
|
|
13
|
+
getAudioWaveform,
|
|
14
|
+
assertColor,
|
|
15
|
+
} = require("./messages-media");
|
|
16
|
+
|
|
17
|
+
function encodeBase64EncodedStringForUpload(str) {
|
|
18
|
+
if (!str) return "";
|
|
19
|
+
return Buffer.from(str).toString("base64");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async function getRawMediaUploadData(media, mediaType, logger) {
|
|
24
|
+
throw new Error("Implement getRawMediaUploadData sesuai Baileys v6 mu");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getWAUploadToServerNewsletter({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) {
|
|
28
|
+
return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
|
|
29
|
+
let uploadInfo = await refreshMediaConn(false);
|
|
30
|
+
let urls;
|
|
31
|
+
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
32
|
+
|
|
33
|
+
fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
|
|
34
|
+
|
|
35
|
+
const headers = {
|
|
36
|
+
"Content-Type": "application/octet-stream",
|
|
37
|
+
Origin: DEFAULT_ORIGIN,
|
|
38
|
+
...(options?.headers || {}),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
for (const { hostname } of hosts) {
|
|
42
|
+
logger.debug(`uploading to "${hostname}"`);
|
|
43
|
+
const auth = encodeURIComponent(uploadInfo.auth);
|
|
44
|
+
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
45
|
+
|
|
46
|
+
let result;
|
|
47
|
+
try {
|
|
48
|
+
result = await uploadMedia({ url, filePath, headers, timeoutMs, agent: fetchAgent }, logger);
|
|
49
|
+
|
|
50
|
+
if (result?.url || result?.direct_path) {
|
|
51
|
+
urls = {
|
|
52
|
+
mediaUrl: result.url,
|
|
53
|
+
directPath: result.direct_path,
|
|
54
|
+
meta_hmac: result.meta_hmac,
|
|
55
|
+
fbid: result.fbid,
|
|
56
|
+
ts: result.ts,
|
|
57
|
+
};
|
|
58
|
+
break;
|
|
59
|
+
} else {
|
|
60
|
+
uploadInfo = await refreshMediaConn(true);
|
|
61
|
+
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
|
|
65
|
+
logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? "" : ", retrying..."}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!urls) throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
|
|
70
|
+
return urls;
|
|
71
|
+
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function prepareWAMessageMediaNewsletter(message, options) {
|
|
76
|
+
const logger = options.logger;
|
|
77
|
+
|
|
78
|
+
let mediaType;
|
|
79
|
+
for (const key of Object.keys(MEDIA_PATH_MAP)) {
|
|
80
|
+
if (key in message) {
|
|
81
|
+
mediaType = key;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (!mediaType) {
|
|
85
|
+
throw new Boom("Invalid media type", { statusCode: 400 });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const uploadData = {
|
|
89
|
+
...message,
|
|
90
|
+
media: message[mediaType],
|
|
91
|
+
};
|
|
92
|
+
delete uploadData[mediaType];
|
|
93
|
+
|
|
94
|
+
if (mediaType === "document" && !uploadData.fileName) {
|
|
95
|
+
uploadData.fileName = "file";
|
|
96
|
+
}
|
|
97
|
+
if (!uploadData.mimetype) {
|
|
98
|
+
uploadData.mimetype = MEDIA_PATH_MAP[mediaType]?.mimetype || "application/octet-stream";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const isNewsletter = options.newsletter || (options.jid && options.jid.endsWith("@newsletter"));
|
|
102
|
+
if (!isNewsletter) {
|
|
103
|
+
throw new Boom("This function is only for newsletter", { statusCode: 400 });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(
|
|
107
|
+
uploadData.media,
|
|
108
|
+
mediaType,
|
|
109
|
+
logger
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const fileSha256B64 = fileSha256.toString("base64");
|
|
113
|
+
|
|
114
|
+
const urls = await getWAUploadToServerNewsletter(options, options.refreshMediaConn)(
|
|
115
|
+
filePath,
|
|
116
|
+
{ mediaType, fileEncSha256B64: fileSha256B64, timeoutMs: options.mediaUploadTimeoutMs }
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
await fs.promises.unlink(filePath);
|
|
120
|
+
|
|
121
|
+
const obj = WAProto.Message.fromObject({
|
|
122
|
+
[${mediaType}Message]: {
|
|
123
|
+
url: urls.mediaUrl,
|
|
124
|
+
directPath: urls.directPath,
|
|
125
|
+
fileSha256,
|
|
126
|
+
fileLength,
|
|
127
|
+
...uploadData,
|
|
128
|
+
media: undefined,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (uploadData.ptv) {
|
|
133
|
+
obj.ptvMessage = obj.videoMessage;
|
|
134
|
+
delete obj.videoMessage;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return obj;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
prepareWAMessageMediaNewsletter,
|
|
142
|
+
getWAUploadToServerNewsletter,
|
|
143
|
+
};
|