This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
183 lines (162 loc) · 4.97 KB
/
utils.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const axios = require("axios");
const crypto = require("crypto");
const fs = require('fs');
const FormData = require("form-data");
const word = [
'A', 'Á', 'À', 'Ả', 'Ã', 'Ạ', 'a', 'á', 'à', 'ả', 'ã', 'ạ',
'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ',
'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ',
'B', 'b',
'C', 'c',
'D', 'Đ', 'd', 'đ',
'E', 'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'e', 'é', 'è', 'ẻ', 'ẽ', 'ẹ',
'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ',
'F', 'f',
'G', 'g',
'H', 'h',
'I', 'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'i', 'í', 'ì', 'ỉ', 'ĩ', 'ị',
'J', 'j',
'K', 'k',
'L', 'l',
'M', 'm',
'N', 'n',
'O', 'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'o', 'ó', 'ò', 'ỏ', 'õ', 'ọ',
'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ',
'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ',
'P', 'p',
'Q', 'q',
'R', 'r',
'S', 's',
'T', 't',
'U', 'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'u', 'ú', 'ù', 'ủ', 'ũ', 'ụ',
'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự',
'V', 'v',
'W', 'w',
'X', 'x',
'Y', 'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'y', 'ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ',
'Z', 'z',
' '
];
const regexURL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
async function getStream(url = "", pathName = "", options = {}) {
// @NTKhang03
if (!options && typeof pathName === "object") {
options = pathName;
pathName = "";
}
try {
if (!url || typeof url !== "string")
throw new Error(`The first argument (url) must be a string`);
const response = await axios({
url,
method: "GET",
responseType: "stream",
...options
});
if (!pathName)
pathName = utils.randomString(10) + (response.headers["content-type"] ? '.' + utils.getExtFromMimeType(response.headers["content-type"]) : ".noext");
response.data.path = pathName;
if (response.status === 200) {
return response.data;
} else {
throw { error: true, status: response.status }
}
}
catch (err) {
throw err;
}
}
async function sleep(ms) {
return await new Promise(resolve => setTimeout(resolve, ms))
}
function uuid() {
return crypto.randomUUID();
}
function configSync(json) {
const currentConfig = fs.existsSync("config_handler.json") ? JSON.parse(fs.readFileSync("config_handler.json", 'utf8')) : {};
fs.writeFileSync("config_handler.json", JSON.stringify({ ...currentConfig, ...json }, null, 2), 'utf8');
global.config_handler = require("./config_handler.json");
return true;
};
function translate(text, lang = "en") {
// Ntkhang03
return new Promise((resolve, reject) => {
axios.get(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=${lang}&dt=t&q=${encodeURIComponent(text)}`)
.then(res => {
resolve(res.data[0][0][0]);
})
.catch(err => {
reject(err);
});
});
}
async function uploadImgbb(file) {
let type = "file";
try {
if (!file)
throw new Error('The first argument (file) must be a stream or a image url');
if (regCheckURL.test(file) == true)
type = "url";
if (
(type != "url" && (!(typeof file._read === 'function' && typeof file._readableState === 'object'))) ||
(type == "url" && !regCheckURL.test(file))
)
throw new Error('The first argument (file) must be a stream or an image URL');
const res_ = await axios({
method: 'GET',
url: 'https://imgbb.com'
});
const auth_token = res_.data.match(/auth_token="([^"]+)"/)[1];
const timestamp = Date.now();
const res = await axios({
method: 'POST',
url: 'https://imgbb.com/json',
headers: {
"content-type": "multipart/form-data"
},
data: {
source: file,
type: type,
action: 'upload',
timestamp: timestamp,
auth_token: auth_token
}
});
return res.data;
}
catch (err) {
throw new CustomError(err.response ? err.response.data : err);
}
}
async function downloadFile(url = "", path = "") {
// @NTkhang03
if (!url || typeof url !== "string")
throw new Error(`The first argument (url) must be a string`);
if (!path || typeof path !== "string")
throw new Error(`The second argument (path) must be a string`);
const getFile = await axios.get(url, {
responseType: "arraybuffer"
});
fs.writeFileSync(path, Buffer.from(getFile.data));
return path;
}
downloadFile.stream = async function(url, path) {
try {
const returnedPath = await downloadFile(url, path);
if (!returnedPath) throw new Error("No Path Returned");
const stream = fs.createReadStream(returnedPath);
fs.unlinkSync(returnedPath);
return stream;
} catch (err) {
throw err;
}
}
module.exports = {
getStream,
sleep,
uuid,
configSync,
translate,
uploadImgbb,
downloadFile
}