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