@rexxhayanasi/elaina-baileys 1.2.0-rc.2 → 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.
@@ -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);
@@ -88,18 +88,13 @@ const getImageProcessingLibrary = async () => {
88
88
  const hkdfInfoKey = (type) => {
89
89
  if (type === 'sticker-pack') return 'WhatsApp Image Keys';
90
90
  if (type === 'ptv') return 'WhatsApp Video Keys';
91
-
92
- // Support Newsletter keys explicitly or fallback
93
91
  if (type === 'newsletter-image') return 'WhatsApp Image Keys';
94
92
  if (type === 'newsletter-video') return 'WhatsApp Video Keys';
95
93
 
96
94
  const hkdfInfo = Defaults_1.MEDIA_HKDF_KEY_MAPPING[type];
97
-
98
- // Fallback if type not found to prevent undefined key error
99
95
  if (!hkdfInfo) {
100
96
  return 'WhatsApp Image Keys';
101
97
  }
102
-
103
98
  return `WhatsApp ${hkdfInfo} Keys`;
104
99
  };
105
100
  exports.hkdfInfoKey = hkdfInfoKey;
@@ -770,9 +765,7 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
770
765
  const hosts = [...customUploadHosts, ...uploadInfo.hosts];
771
766
  const chunks = [];
772
767
 
773
- // --- MODIFIKASI: Panggil via objek toAsyncIterable_1 ---
774
768
  try {
775
- // Kita akses properti .toAsyncIterable karena diimpor sebagai objek
776
769
  stream = toAsyncIterable_1.toAsyncIterable(stream);
777
770
  } catch (error) {
778
771
  throw new boom_1.Boom(error.message, { statusCode: 400 });
@@ -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
- delete uploadData['ptv'];
122
+ }
123
+ delete uploadData['ptv'];
122
124
 
123
125
  const cacheableKey = typeof uploadData.media === 'object' &&
124
126
  ('url' in uploadData.media) &&
@@ -145,19 +147,20 @@ const assertColor = async (color) => {
145
147
  }
146
148
 
147
149
  const isNewsletter = options.newsletter || (options.jid && (0, WABinary_1.isJidNewsletter)(options.jid));
148
-
149
150
  if (isNewsletter) {
150
151
  logger === null || logger === void 0 ? void 0 : logger.debug({ key: cacheableKey }, 'Preparing raw media for newsletter');
151
152
 
152
- const { bodyPath, fileSha256, fileLength, didSaveToTmpPath } = await (0, messages_media_1.prepareStream)(
153
+
154
+ const { bodyPath, fileSha256, fileLength, didSaveToTmpPath, encWriteStream } = await (0, messages_media_1.prepareStream)(
153
155
  uploadData.media,
154
156
  options.mediaTypeOverride || mediaType,
155
157
  { logger, opts: options.options }
156
158
  );
157
159
 
158
160
  const fileEncSha256B64 = fileSha256.toString('base64');
161
+ const streamToUpload = bodyPath || encWriteStream;
159
162
 
160
- const { mediaUrl, directPath } = await options.upload(bodyPath, {
163
+ const { mediaUrl, directPath } = await options.upload(streamToUpload, {
161
164
  fileEncSha256B64,
162
165
  mediaType,
163
166
  timeoutMs: options.mediaUploadTimeoutMs,
@@ -183,6 +186,10 @@ const assertColor = async (color) => {
183
186
  })
184
187
  });
185
188
 
189
+ if (mediaType === 'audio' && uploadData.ptt) {
190
+ obj.audioMessage.ptt = true;
191
+ }
192
+
186
193
  if (isPtv) {
187
194
  obj.ptvMessage = obj.videoMessage;
188
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rexxhayanasi/elaina-baileys",
3
- "version": "1.2.0-rc.2",
3
+ "version": "1.2.0-rc.4",
4
4
  "description": "Custom Baileys WhatsApp API",
5
5
  "keywords": [
6
6
  "baileys",