const express = require("express"); const fspath = require("path"); const fsp = require("fs/promises"); const fs = require("fs"); const yaml = require("js-yaml"); const zip = require("@zip.js/zip.js"); const os = require("os"); const app = express(); app.use(express.json()); app.use(express.text()); let plugin_path = null; const configDir = fspath.join(os.homedir(), ".config", "pga-plugin-manager"); const yaml_filename = fspath.join(configDir, "config.yaml"); const appDir = __dirname; async function set_yaml(data) { let current = {}; try { fs.mkdirSync(configDir, { recursive: true }); const yamlstr = await fsp.readFile(yaml_filename, "utf8"); current = yaml.load(yamlstr); } catch (err) { if (err.code !== "ENOENT") { throw err; } } const updated = { ...current, ...data, }; await fsp.writeFile(yaml_filename, yaml.dump(updated), "utf8"); } app.get("/api/geturl/", async (req, res) => { const yamlstr = yaml.load(fs.readFileSync(yaml_filename, "utf8")); res.send(yamlstr.URL); }); app.post("/api/seturl/", async (req, res) => { res.setHeader("Content-Type", "text/plain; charset=utf-8"); res.setHeader("Cache-Control", "no-cache, no-transform"); res.setHeader("Connection", "keep-alive"); res.flushHeaders(); const send = (message) => { res.write(message + "\n"); }; const base_url = "https://modrinth.com/collection/"; if (req.body.startsWith(base_url)) { const data = { URL: req.body, }; set_yaml(data); send("Success (If the URL is valid collection)!"); } else { send( "ERR: Please submit a URL like https://modrinth.com/collection/(id_here)", ); } }); app.get("/api/getpath/", async (req, res) => { const yamlstr = yaml.load(fs.readFileSync(yaml_filename, "utf8")); res.send(yamlstr.PATH); }); app.post("/api/setpath/", async (req, res) => { res.setHeader("Content-Type", "text/plain; charset=utf-8"); res.setHeader("Cache-Control", "no-cache, no-transform"); res.setHeader("Connection", "keep-alive"); res.flushHeaders(); const send = (message) => { res.write(message + "\n"); }; try { await fsp.access(req.body, fsp.constants.W_OK).then(() => { const data = { PATH: req.body, }; set_yaml(data); send("File set successfully to: " + req.body); }); } catch (err) { send( "ERR: Path failed to set properly. Either the directory doesn't exist, or the program has no read or write access. " + err, ); } }); // Download as zip to browser app.post("/api/downloader/direct", async (req, res) => { res.setHeader("Content-Type", "application/octet-stream; charset=utf-8"); res.setHeader("Cache-Control", "no-cache, no-transform"); res.setHeader("Connection", "keep-alive"); res.flushHeaders(); const zipFileWriter = new zip.BlobWriter(); const zipWriter = new zip.ZipWriter(zipFileWriter); for (const versionId of req.body) { const response = await fetch( `https://api.modrinth.com/v2/version/${versionId}`, ); const version = await response.json(); // Get the first file attached to the version const file = version.files[0]; if (!file) { continue; } const fileResponse = await fetch(file.url); const buffer = Buffer.from(await fileResponse.arrayBuffer()); const reader = new zip.Uint8ArrayReader(buffer); await zipWriter.add(file.filename, reader); } await zipWriter.close(); const blob = await zipFileWriter.getData(); const buffer = Buffer.from(await blob.arrayBuffer()); res.end(buffer); }); // Download to server app.post("/api/downloader/", async (req, res) => { res.setHeader("Content-Type", "text/plain; charset=utf-8"); res.setHeader("Cache-Control", "no-cache, no-transform"); res.setHeader("Connection", "keep-alive"); res.flushHeaders(); const send = (message) => { res.write(message + "\n"); }; if (!req.body) { return; } if (!plugin_path) { send("Unable to update server. No mod/plugin path specified."); console.log("Unable to update server. No mod/plugin path specified."); return; } // Remove all files in Plugins try { const files = await fsp.readdir(plugin_path); for (const file of files) { try { if (file.startsWith("!")) { continue; } else { await fsp.unlink(fspath.join(plugin_path, file)); } } catch (err) { if (err.code === "EISDIR") { continue; } else { send(err); throw err; } } } send("Removed all existing plugins..."); // Download new files from modrinth for (const versionId of req.body) { const response = await fetch( `https://api.modrinth.com/v2/version/${versionId}`, ); const version = await response.json(); // Get the first file attached to the version const file = version.files[0]; if (!file) { send("No files found for version", version, ", skipping.."); continue; } const fileResponse = await fetch(file.url); const buffer = Buffer.from(await fileResponse.arrayBuffer()); await fsp.writeFile(fspath.join(plugin_path, file.filename), buffer); send(`Downloaded ${file.filename}`); } } catch (err) { if (err.code === "ENOENT") { send( "Unable to download plugins, the set directory no longer exists. Please set a new one.", ); console.log( "Unable to download plugins, the set directory no longer exists. Please set a new one.", ); return; } else { send(err); throw err; } } send("Downloads Complete! Restart your server to finish changes."); res.end(); }); app.get("/api/modrinth/collection/:id", async (req, res) => { const { id } = req.params; const response = await fetch( `https://api.modrinth.com/v3/collection/${encodeURIComponent(id)}`, ); if (!response.ok) { return res.status(response.status).json({ error: "Modrinth API request failed", }); } const collection = await response.json(); res.json(collection); }); app.get("/", async (req, res) => { try { const yamlstr = yaml.load(fs.readFileSync(yaml_filename, "utf8")); plugin_path = yamlstr.PATH; res.sendFile(fspath.join(appDir, "index.html")); } catch (err) { if (err.code === "ENOENT") { const data = { PATH: "", URL: "", }; await set_yaml(data); const yamlstr = yaml.load(fs.readFileSync(yaml_filename, "utf8")); plugin_path = yamlstr.PATH; res.sendFile(fspath.join(appDir, "index.html")); } else { throw err; } } }); app.listen(3000, () => { console.log("Server is running on http://localhost:3000"); });