-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreqfilemanager.js
More file actions
65 lines (58 loc) · 2.28 KB
/
Copy pathreqfilemanager.js
File metadata and controls
65 lines (58 loc) · 2.28 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
const axios = require("axios");
const reqmanager = require("./reqmanager");
const fs = require("fs");
const path = require("path");
//IDK! Idk what to do here, I don't really understand how that work
function Multipart() {
this.boundary =
"NodeDiscordIO" + "-" + "2.5.3";
this.result = "";
};
Multipart.prototype.append = function (data) {
/* Header */
var str = "\r\n--";
str += this.boundary + "\r\n";
str += 'Content-Disposition: form-data; name="' + data[0] + '"';
if (data[2]) {
str += '; filename="' + data[2] + '"\r\n';
str += 'Content-Type: application/octet-stream';
};
/* Body */
str += "\r\n\r\n" + (data[1] instanceof Buffer ? data[1] : new Buffer.from(String(data[1]), 'utf-8')).toString('binary');
this.result += str;
};
Multipart.prototype.finalize = function () {
this.result += "\r\n--" + this.boundary + "--";
};
module.exports = uploadFile = function (url, token, data, file, filename) {
/* After like 15 minutes of fighting with Request, turns out Discord doesn't allow multiple files in one message...
despite having an attachments array.*/
var startfile, multi, message, isBuffer, isString;
startfile = file;
multi = new Multipart();
message = data;
console.log(message)
isBuffer = (file instanceof Buffer);
isString = (typeof file === 'string');
if (!isBuffer && !isString) throw "uploadFile requires a String or Buffer as the 'file' value";
if (isBuffer) {
if (!filename) throw "uploadFile requires a 'filename' value to be set if using a Buffer";
file = file;
};
if (isString) try { file = fs.readFileSync(file); } catch (e) { throw "File does not exist: " + file };
let tab = [
// ["content", message.content],
["mentions", ""],
["tts", false],
["nonce", message.nonce],
["file", file, filename || path.basename(startfile)]
];
Object.keys(message).forEach(key => {
if (!Array.isArray(message[key])||(Array.isArray(message[key]) &&message[key].length>0)) {
tab.push([key, message[key]]);
};
});
tab.forEach(multi.append, multi);
multi.finalize();
return reqmanager(url, token, multi.result, { method: "post", header: "multipart/form-data; boundary=" + multi.boundary });
};