@rexxhayanasi/elaina-baileys 1.2.0-rc.6 → 1.2.0-rc.7
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/newslettermedia.js +73 -112
- package/package.json +1 -1
|
@@ -1,142 +1,103 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
exports.prepareWAMessageMediaNewsletter = prepareWAMessageMediaNewsletter;
|
|
5
|
-
exports.getWAUploadToServerNewsletter = getWAUploadToServerNewsletter;
|
|
6
|
-
|
|
7
|
-
const fs = require("fs");
|
|
3
|
+
const Crypto = require("crypto");
|
|
8
4
|
const Boom = require("@hapi/boom");
|
|
9
5
|
const { WAProto } = require("../../WAProto");
|
|
10
|
-
const {
|
|
11
|
-
MEDIA_PATH_MAP,
|
|
12
|
-
uploadMedia,
|
|
13
|
-
generateThumbnail,
|
|
14
|
-
encryptedStream,
|
|
15
|
-
getAudioDuration,
|
|
16
|
-
getAudioWaveform,
|
|
17
|
-
assertColor,
|
|
18
|
-
unixTimestampSeconds
|
|
19
|
-
} = require("./index");
|
|
20
|
-
const { DEFAULT_ORIGIN } = require("../Defaults");
|
|
21
6
|
|
|
22
7
|
/**
|
|
23
|
-
*
|
|
8
|
+
* Upload khusus newsletter (RAW)
|
|
9
|
+
* CODE BY REXXHAYANASI REFERENCE BY WHISKEYSOCKETS
|
|
24
10
|
*/
|
|
25
|
-
function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
function getWAUploadToServerNewsletter(config, refreshMediaConn) {
|
|
12
|
+
return async (buffer, uploadOpts = {}) => {
|
|
13
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
14
|
+
throw new Boom("Newsletter upload expects Buffer");
|
|
15
|
+
}
|
|
29
16
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
const mediaConn = await refreshMediaConn();
|
|
18
|
+
if (!mediaConn || !mediaConn.hosts?.length) {
|
|
19
|
+
throw new Boom("Failed to obtain media connection for newsletter");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const host = mediaConn.hosts[0];
|
|
23
|
+
const url = `https://${host.hostname}${host.uploadPath}`;
|
|
24
|
+
|
|
25
|
+
const res = await fetch(url, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
body: buffer,
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/octet-stream",
|
|
30
|
+
"Content-Length": buffer.length,
|
|
31
|
+
},
|
|
32
|
+
agent: config.fetchAgent,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
throw new Boom(`Newsletter upload failed: ${res.status}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const json = await res.json();
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
mediaUrl: json.url,
|
|
43
|
+
directPath: json.direct_path,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
35
46
|
}
|
|
36
47
|
|
|
37
48
|
/**
|
|
38
|
-
*
|
|
49
|
+
* Ambil metadata RAW newsletter
|
|
39
50
|
*/
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
|
|
47
|
-
|
|
48
|
-
const headers = {
|
|
49
|
-
"Content-Type": "application/octet-stream",
|
|
50
|
-
Origin: DEFAULT_ORIGIN,
|
|
51
|
-
...(options?.headers || {}),
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
for (const { hostname } of hosts) {
|
|
55
|
-
logger.debug(`uploading to "${hostname}"`);
|
|
56
|
-
const auth = encodeURIComponent(uploadInfo.auth);
|
|
57
|
-
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
58
|
-
|
|
59
|
-
let result;
|
|
60
|
-
try {
|
|
61
|
-
result = await uploadMedia({ url, filePath, headers, timeoutMs, agent: fetchAgent }, logger);
|
|
62
|
-
|
|
63
|
-
if (result?.url || result?.direct_path) {
|
|
64
|
-
urls = {
|
|
65
|
-
mediaUrl: result.url,
|
|
66
|
-
directPath: result.direct_path,
|
|
67
|
-
meta_hmac: result.meta_hmac,
|
|
68
|
-
fbid: result.fbid,
|
|
69
|
-
ts: result.ts,
|
|
70
|
-
};
|
|
71
|
-
break;
|
|
72
|
-
} else {
|
|
73
|
-
uploadInfo = await refreshMediaConn(true);
|
|
74
|
-
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
75
|
-
}
|
|
76
|
-
} catch (error) {
|
|
77
|
-
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
|
|
78
|
-
logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname}${isLast ? "" : ", retrying..."}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!urls) throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
|
|
83
|
-
return urls;
|
|
51
|
+
async function getRawMediaUploadDataNewsletter(buffer) {
|
|
52
|
+
const fileSha256 = Crypto.createHash("sha256").update(buffer).digest();
|
|
53
|
+
return {
|
|
54
|
+
buffer,
|
|
55
|
+
fileSha256,
|
|
56
|
+
fileLength: buffer.length,
|
|
84
57
|
};
|
|
85
58
|
}
|
|
86
59
|
|
|
87
60
|
/**
|
|
88
|
-
*
|
|
61
|
+
* Builder message newsletter (PTV / media)
|
|
89
62
|
*/
|
|
90
63
|
async function prepareWAMessageMediaNewsletter(message, options) {
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
let mediaType;
|
|
94
|
-
for (const key of Object.keys(MEDIA_PATH_MAP)) {
|
|
95
|
-
if (key in message) {
|
|
96
|
-
mediaType = key;
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
64
|
+
const mediaType = Object.keys(message)[0];
|
|
65
|
+
if (!mediaType) throw new Boom("Invalid newsletter media");
|
|
100
66
|
|
|
101
|
-
|
|
102
|
-
|
|
67
|
+
const buffer = message[mediaType];
|
|
68
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
69
|
+
throw new Boom("Newsletter media must be Buffer");
|
|
103
70
|
}
|
|
104
71
|
|
|
105
|
-
const uploadData =
|
|
106
|
-
delete uploadData[mediaType];
|
|
72
|
+
const uploadData = await getRawMediaUploadDataNewsletter(buffer);
|
|
107
73
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
throw new Boom("This function is only for newsletter", { statusCode: 400 });
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, mediaType, logger);
|
|
117
|
-
const fileSha256B64 = fileSha256.toString("base64");
|
|
118
|
-
|
|
119
|
-
const urls = await getWAUploadToServerNewsletter(options, options.refreshMediaConn)(
|
|
120
|
-
filePath,
|
|
121
|
-
{ mediaType, fileEncSha256B64: fileSha256B64, timeoutMs: options.mediaUploadTimeoutMs }
|
|
74
|
+
const upload = getWAUploadToServerNewsletter(
|
|
75
|
+
{
|
|
76
|
+
fetchAgent: options.fetchAgent,
|
|
77
|
+
},
|
|
78
|
+
options.refreshMediaConn
|
|
122
79
|
);
|
|
123
80
|
|
|
124
|
-
await
|
|
81
|
+
const uploaded = await upload(buffer);
|
|
125
82
|
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
url:
|
|
129
|
-
directPath:
|
|
130
|
-
fileSha256,
|
|
131
|
-
fileLength,
|
|
132
|
-
|
|
133
|
-
media: undefined,
|
|
83
|
+
const proto = {};
|
|
84
|
+
proto[`${mediaType}Message`] = {
|
|
85
|
+
url: uploaded.mediaUrl,
|
|
86
|
+
directPath: uploaded.directPath,
|
|
87
|
+
fileSha256: uploadData.fileSha256,
|
|
88
|
+
fileLength: uploadData.fileLength,
|
|
89
|
+
mimetype: options.mimetype,
|
|
134
90
|
};
|
|
135
91
|
|
|
136
|
-
if (
|
|
137
|
-
|
|
138
|
-
delete messageObj.videoMessage;
|
|
92
|
+
if (mediaType === "video") {
|
|
93
|
+
proto.videoMessage.ptv = true;
|
|
139
94
|
}
|
|
140
95
|
|
|
141
|
-
return WAProto.Message.fromObject(
|
|
96
|
+
return WAProto.Message.fromObject(proto);
|
|
142
97
|
}
|
|
98
|
+
|
|
99
|
+
module.exports = {
|
|
100
|
+
prepareWAMessageMediaNewsletter,
|
|
101
|
+
getRawMediaUploadDataNewsletter,
|
|
102
|
+
getWAUploadToServerNewsletter,
|
|
103
|
+
};
|