poke/src/libpoketube/init/pages-static.js

165 lines
5.2 KiB
JavaScript
Raw Normal View History

2022-12-19 12:52:57 +01:00
const {
fetcher,
core,
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("../libpoketube-initsys.js");
2023-09-06 22:48:42 +02:00
2022-11-09 17:54:00 +01:00
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("../ptutils/libpt-coreutils.js");
2023-09-06 22:48:42 +02:00
const path = require("path");
const fs = require("node:fs");
const CleanCSS = require("clean-css");
2022-11-09 17:54:00 +01:00
const sha384 = modules.hash;
2023-09-06 22:48:42 +02:00
const notice =
"/* the code is Licensed in gpl-3.0-or-later. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public License for more detailsYou should have received a copy of the GNU General Public Licensealong with this program. If not, see <https://www.gnu.org/licenses/>. - add the param nomin to view source code. (eg poketube.fun/css/poketube.css?nomin=true) */";
2022-11-09 17:54:00 +01:00
module.exports = function (app, config, renderTemplate) {
2023-09-06 22:48:42 +02:00
var html_location = "./css/";
2022-12-19 12:52:57 +01:00
2023-09-06 22:48:42 +02:00
app.get("/privacy", function (req, res) {
if (req.hostname == "poketube.fun") {
renderTemplate(res, req, "priv.ejs", {
isMobile: req.useragent.isMobile,
});
} else {
renderTemplate(res, req, "priv-custom.ejs");
}
2022-12-19 12:52:57 +01:00
});
2023-09-06 22:48:42 +02:00
app.get("/143", function (req, res) {
var number_easteregg = getRandomArbitrary(0, 143);
2022-12-19 12:52:57 +01:00
2023-09-06 22:48:42 +02:00
if (number_easteregg == "143") {
renderTemplate(res, req, "143.ejs", {
something: req.query.something,
});
2022-12-19 12:52:57 +01:00
}
2023-08-29 11:06:36 +02:00
2023-09-06 22:48:42 +02:00
if (req.query.number == "143") {
renderTemplate(res, req, "143.ejs", {
something: req.query.something,
});
2023-08-29 11:06:36 +02:00
}
2023-09-06 22:48:42 +02:00
if (req.query.something == "143") {
renderTemplate(res, req, "143.ejs", {
something: req.query.something,
});
}
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
if (number_easteregg != "143") {
return res.redirect("/" + "?number=" + number_easteregg);
}
});
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
app.get("/domains", function (req, res) {
renderTemplate(res, req, "domains.ejs");
});
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
app.get("/license", function (req, res) {
renderTemplate(res, req, "license.ejs");
});
2023-04-12 22:41:37 +02:00
2023-09-06 22:48:42 +02:00
app.get("/credits", function (req, res) {
renderTemplate(res, req, "want-you-gone.ejs");
});
2023-02-23 16:58:19 +01:00
2023-09-06 22:48:42 +02:00
app.get("/customize", function (req, res) {
const tab = req.query.tab;
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
renderTemplate(res, req, "custom-css.ejs", {
tab,
2023-04-12 22:41:37 +02:00
});
2023-02-22 17:02:06 +01:00
});
2022-12-27 00:54:55 +01:00
2023-09-06 22:48:42 +02:00
const cssDir = "./css/";
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
app.get("/css/:id", (req, res) => {
const filePath = path.join(cssDir, req.params.id);
if (!fs.existsSync(filePath)) {
res.status(404).send("File not found");
return;
2023-08-29 11:06:36 +02:00
}
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
if (req.params.id.endsWith(".css") && !req.query.nomin) {
// Minimize the CSS file
const css = fs.readFileSync(filePath, "utf8");
const minimizedCss = new CleanCSS().minify(css).styles;
// Serve the minimized CSS file
res.header("Content-Type", "text/css");
res.send(notice + " " + minimizedCss);
2023-08-30 23:22:41 +02:00
} else {
2023-09-06 22:48:42 +02:00
// Serve the original file
res.sendFile(req.params.id, { root: html_location });
}
2023-08-30 23:22:41 +02:00
2023-09-06 22:48:42 +02:00
if (req.params.id.endsWith(".js")) {
res.redirect("/static/" + req.params.id);
}
});
2023-08-30 23:22:41 +02:00
2023-09-09 18:51:15 +02:00
app.get("/static/:id", (req, res) => {
if (req.params.id.endsWith(".css")) {
res.redirect("/css/" + req.params.id);
} else if (req.params.id.endsWith(".js")) {
if (req.params.id.endsWith(".bundle.js")) {
// Define the paths to the three input JavaScript files
const file1Path = path.join(html_location, 'app.js');
const file2Path = path.join(html_location, 'custom-css.js');
const file3Path = path.join(html_location, 'emojis.js');
// Read the contents of the three input files
const file1Content = fs.readFileSync(file1Path, 'utf-8');
const file2Content = fs.readFileSync(file2Path, 'utf-8');
const file3Content = fs.readFileSync(file3Path, 'utf-8');
// Combine the contents of the three files
const combinedContent = `${file1Content}\n${file2Content}\n${file3Content}`;
// Serve the combined content as JavaScript
res.header("Content-Type", "text/javascript");
const minimizedJs = require("uglify-js").minify(combinedContent).code;
res.send("// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0-or-later" + `\n` + `// Includes app.js, emojis.js and custom-css.js. source code can be found for these 3 files in https://codeberg.org/Ashley/poketube/src/branch/main/css/` +
`\n` + minimizedJs + `\n` +"// @license-end");
} else {
2023-09-06 22:48:42 +02:00
const filePath = path.join(html_location, req.params.id);
if (!fs.existsSync(filePath)) {
res.status(404).send("File not found");
return;
2023-08-30 23:22:41 +02:00
}
2023-09-06 22:48:42 +02:00
// Minimize the JavaScript file
const js = fs.readFileSync(filePath, "utf8");
const minimizedJs = require("uglify-js").minify(js).code;
// Serve the minimized JavaScript file
res.header("Content-Type", "text/javascript");
res.send(
"// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0-or-later" +
`\n` +
`// Source code can be found in: https://codeberg.org/Ashley/poketube/src/branch/main/css/${req.params.id}` +
`\n` +
minimizedJs +
`\n` +
"// @license-end"
2023-08-29 11:06:36 +02:00
);
}
2023-09-09 18:51:15 +02:00
} else {
res.sendFile(req.params.id, { root: html_location });
}
});
2022-12-19 12:52:57 +01:00
};