-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (151 loc) · 4.58 KB
/
index.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
"use strict";
/*
=================================================================
|| CONFIG ||
=================================================================
*/
const host = "https://gg.com"; // DO NOT PUT A '/' AT THE END OF THE host. THERE'S BEEN AN EXAMPLE FOR YOU
const main = "Main"; // THIS WILL BE THE CONTAINER FOLDER FOR ALL THE API. IN THIS CASE, MAIN FOLDER IS THAT FOLDER AND YOU CAN RENAME IT
const PORT = process.env.PORT || 8080; //
/*
=================================================================
|| REQUIRE DEPENDENCIES ||
=================================================================
*/
import fs from "fs-extra"
import express from "express"
const app = express();
import http from "http"
import chalk from "chalk"
import AutoGitUpdate from 'auto-git-update';
import readline from "readline";
import { dirname } from "path";
import { fileURLToPath } from 'url';
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
var isUpdated = false;
console.log()
let config = {
repository: 'https://github.com/bhhoang/SHRImA',
tempLocation: `${path.join(__dirname, '..' , 'Temporary')}`,
branch: "main",
exitOnComplete: false
}
function up() {
log("Check for update [Y/N]?");
rl.question("", (ans) => {
switch (ans.toLowerCase()) {
case "y":
const updater = new AutoGitUpdate(config);
updater.forceUpdate()
setTimeout(() => {isUpdated = true},10000);
rl.close()
case "n":
isUpdated = true;
rl.close();
}
});
}
up()
// Some consosle function
function warn(p) {
console.log(chalk.red("[ WARNING ]") + " " + p);
}
function log(p) {
console.log(chalk.green("[ CONSOLE ]") + " " + p);
}
rl.on("close", () => {
/*
=================================================================
|| CHECK FOLDER EXIST OR CREATE ||
=================================================================
*/
if (isUpdated == true) {
let checker = new Promise((resolve, reject) => {
if (!fs.existsSync(`${__dirname}/${main}`)) {
console.warn(`Couldn't find folder ${main}, Creating one`);
setTimeout(crt, 1000);
async function crt() {
try {
fs.mkdirSync(`${__dirname}/${main}`);
} catch (e) {
warn(e);
}
}
setTimeout(resolve, 1000);
}
if (fs.readdirSync(main).length == 0) {
setTimeout(reject, 1000);
}
});
checker
.then()
.then(() => {
return skin();
})
.catch(() => {
warn(
"Folder existed but there's nothing in it, please add something to it"
);
setTimeout(skin, 1000);
});
}
/*
=================================================================
LIST API
=================================================================
*/
app.get("/", (req, res) => {
function obj(key, url) {
this.API_NAME = key;
this.url = url;
}
const array = new Array();
for (let i = 0; i < fs.readdirSync(main).length; i++) {
//create new array then append object
let temp = fs.readdirSync(main)[i];
let temp2 = `${host}/${temp}`;
array.push(new obj(temp, temp2));
}
res.send(array);
});
/*
=================================================================
RANDOM FOR EACH DIRECTORY
=================================================================
*/
app.get(`/:Folder`, (req, res) => {
const { Folder } = req.params;
const random = Math.floor(
Math.random() * fs.readdirSync(`${__dirname}/${main}/${Folder}`).length
);
const id = encodeURIComponent(
fs.readdirSync(__dirname + `/${main}/${Folder}`)[random]
);
var outobject = {
success: `${host}/${Folder}/${id}`,
};
res.send(outobject);
});
/*
=================================================================
SEND FILE ON REQUEST
=================================================================
*/
app.get(`/:dir/:name`, (req, res) => {
const { dir, name } = req.params;
const imagePath = __dirname + `/${main}/${dir}/${name}`;
res.sendFile(imagePath);
});
function skin() {
const credityuu = http.createServer(app);
credityuu.listen(PORT, () => {
log(`Api running successfully and listening on port ${PORT}!`);
});
}
});