Compare commits
2
Commits
aa3addbeba
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4d580b6e1 | ||
|
|
6c642935cf |
No files matched your search
Binary file not shown.
@@ -0,0 +1,8 @@
|
|||||||
|
Package: pga-plugin-manager
|
||||||
|
Version: 1.0.2
|
||||||
|
Section: utils
|
||||||
|
Priority: optional
|
||||||
|
Architecture: amd64
|
||||||
|
Depends: nodejs, npm
|
||||||
|
Maintainer: Evan Niederwerfer
|
||||||
|
Description: Minecraft infrustructure for plugins
|
||||||
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cd /usr/lib/pga-plugin-manager
|
||||||
|
npm install --omit=dev
|
||||||
|
|
||||||
|
cp /usr/lib/pga-plugin-manager/pga-plugin-manager.service /etc/systemd/system/pga-plugin-manager.service
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable --now pga-plugin-manager.service
|
||||||
|
echo "Now running at http://localhost:3000, and will run on every boot. "
|
||||||
|
exit 0
|
||||||
Executable
+5
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
cd /usr/lib/pga-plugin-manager || exit 1
|
||||||
|
exec ./app.sh "$@"
|
||||||
|
EOF
|
||||||
Executable
+2
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
exec node /usr/lib/pga-plugin-manager/updater.js "$@"
|
||||||
@@ -0,0 +1,423 @@
|
|||||||
|
<!doctype html>
|
||||||
|
|
||||||
|
<h3>PGA Modification Downloader for Minecraft - Notice: for internal (LAN) use only. NEVER expose to the internet!</h3>
|
||||||
|
<inp>
|
||||||
|
<p>Absolute Plugin/Mod Folder Path:</p>
|
||||||
|
<p style="color: red"> Warning: Script removes all files excluding folders. Script ignores files that start with "!" in name as an override.</p>
|
||||||
|
<input type="text" id="path"></input>
|
||||||
|
<button onclick="set_path();">Set</button>
|
||||||
|
<p>Modrinth Collection URL (Full):</p>
|
||||||
|
<input type="text" id="url">
|
||||||
|
<button onclick="set_url();">Set</button>
|
||||||
|
<p>Server Type & Minecraft Version:</p>
|
||||||
|
<select id="type">
|
||||||
|
<option value="paper">Paper (default)</option>
|
||||||
|
<option value="spigot">Spigot</option>
|
||||||
|
<option value="fabric">Fabric</option>
|
||||||
|
<option value="forge">Forge</option>
|
||||||
|
<option value="velocity">Velocity</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="options"></select>
|
||||||
|
<button onclick="get_plugins()" type="button" id="submit">Submit</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<p>Log:</p>
|
||||||
|
<div id="log" class="log" style="background-color: black"></div>
|
||||||
|
|
||||||
|
<div class="log_buttons">
|
||||||
|
<button
|
||||||
|
disabled
|
||||||
|
id="btn_download_compatible"
|
||||||
|
onclick="download_compatible()"
|
||||||
|
>
|
||||||
|
Install Compatible Plugins
|
||||||
|
</button>
|
||||||
|
<button disabled id="btn_download_all" onclick="download_all()">
|
||||||
|
Install Compatible AND Incompatible Plugins (may cause issues)
|
||||||
|
</button>
|
||||||
|
<button disabled id="btn_download_zip" onclick="download_zip()">
|
||||||
|
Download zip with no plugin folder changes (safest)
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<input type="file" id="plugins_folder" webkitdirectory multiple hidden />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
//Call on every refresh
|
||||||
|
rq_vers()
|
||||||
|
url = get_url();
|
||||||
|
get_path();
|
||||||
|
|
||||||
|
// vars
|
||||||
|
let path_set = false;
|
||||||
|
let url_set = false;
|
||||||
|
let all_compatible_versions = [];
|
||||||
|
let all_incompatible_versions = [];
|
||||||
|
|
||||||
|
|
||||||
|
async function set_url() {
|
||||||
|
const url = document.getElementById("url");
|
||||||
|
|
||||||
|
const response = await fetch("http://localhost:3000/api/seturl", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
},
|
||||||
|
body: url.value,
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const text of response.body.pipeThrough(
|
||||||
|
new TextDecoderStream(),
|
||||||
|
)) {
|
||||||
|
alert(text);
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async function get_url() {
|
||||||
|
const url = document.getElementById("url");
|
||||||
|
const response = await fetch("http://localhost:3000/api/geturl", {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.text();
|
||||||
|
|
||||||
|
if (data === "") {
|
||||||
|
url.placeholder = "URL not set. URL must be set to continue."
|
||||||
|
url_set = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
url.innerText = data;
|
||||||
|
url.value = data;
|
||||||
|
url_set = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function get_path() {
|
||||||
|
const path = document.getElementById("path");
|
||||||
|
const response = await fetch("http://localhost:3000/api/getpath", {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.text();
|
||||||
|
|
||||||
|
if (data === "") {
|
||||||
|
path.placeholder = "Path not set. Path must be set to continue."
|
||||||
|
path_set = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
path.innerText = data;
|
||||||
|
path.value = data;
|
||||||
|
path_set = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async function set_path() {
|
||||||
|
const path = document.getElementById("path");
|
||||||
|
|
||||||
|
const response = await fetch("http://localhost:3000/api/setpath", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
},
|
||||||
|
body: path.value,
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const text of response.body.pipeThrough(
|
||||||
|
new TextDecoderStream(),
|
||||||
|
)) {
|
||||||
|
alert(text);
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function rq_vers() {
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
xhr.onload = () => {
|
||||||
|
const data = JSON.parse(xhr.responseText);
|
||||||
|
|
||||||
|
const sel = document.getElementById("options");
|
||||||
|
|
||||||
|
for (let i = 0; i < data["versions"]["length"]; i++) {
|
||||||
|
if (data["versions"][i]["type"] === "release") {
|
||||||
|
var opt = document.createElement("option");
|
||||||
|
opt.value = data["versions"][i]["id"];
|
||||||
|
opt.innerHTML = data["versions"][i]["id"];
|
||||||
|
sel.appendChild(opt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.open(
|
||||||
|
"GET",
|
||||||
|
"https://piston-meta.mojang.com/mc/game/version_manifest_v2.json",
|
||||||
|
);
|
||||||
|
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function log_server_callback(array) {
|
||||||
|
document.getElementById("btn_download_compatible").disabled = true;
|
||||||
|
document.getElementById("btn_download_all").disabled = true;
|
||||||
|
document.getElementById("btn_download_zip").disabled = true;
|
||||||
|
const log = document.getElementById("log");
|
||||||
|
|
||||||
|
|
||||||
|
const response = await fetch("http://localhost:3000/api/downloader/", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(array),
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const chunk of response.body.pipeThrough(
|
||||||
|
new TextDecoderStream(),
|
||||||
|
)) {
|
||||||
|
for (const line of chunk.split("\n")) {
|
||||||
|
if (!line.trim()) continue;
|
||||||
|
|
||||||
|
log.insertAdjacentHTML("beforeend", `${line}<br>`);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.scrollTop = log.scrollHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function download_compatible() {
|
||||||
|
log_server_callback(all_compatible_versions);
|
||||||
|
all_compatible_versions = [];
|
||||||
|
all_incompatible_versions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function download_all() {
|
||||||
|
let combined = all_compatible_versions.concat(
|
||||||
|
all_incompatible_versions,
|
||||||
|
);
|
||||||
|
log_server_callback(combined);
|
||||||
|
all_compatible_versions = [];
|
||||||
|
all_incompatible_versions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetch_zip(array, file_name) {
|
||||||
|
const response = await fetch("http://localhost:3000/api/downloader/direct", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(array),
|
||||||
|
});
|
||||||
|
|
||||||
|
const blob = await response.blob();
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
const a = document.createElement("a");
|
||||||
|
|
||||||
|
a.href = url;
|
||||||
|
a.download = file_name;
|
||||||
|
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
a.remove();
|
||||||
|
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function download_zip() {
|
||||||
|
|
||||||
|
document.getElementById("btn_download_compatible").disabled = true;
|
||||||
|
document.getElementById("btn_download_all").disabled = true;
|
||||||
|
document.getElementById("btn_download_zip").disabled = true;
|
||||||
|
const log = document.getElementById("log");
|
||||||
|
var lv = document.createElement("p");
|
||||||
|
var lv2 = document.createElement("p");
|
||||||
|
|
||||||
|
lv.innerHTML = `Downloading Zips.. Please wait..`;
|
||||||
|
lv.style.color = "white";
|
||||||
|
lv.style.font = "Arial";
|
||||||
|
log.appendChild(lv);
|
||||||
|
|
||||||
|
if (all_compatible_versions.length > 0) {
|
||||||
|
await fetch_zip(all_compatible_versions, "compatible_versions.zip")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (all_incompatible_versions.length > 0) {
|
||||||
|
await fetch_zip(all_incompatible_versions, "incompatible_versions.zip")
|
||||||
|
}
|
||||||
|
|
||||||
|
all_compatible_versions = [];
|
||||||
|
all_incompatible_versions = [];
|
||||||
|
|
||||||
|
lv2.innerHTML = `All downloads complete!`;
|
||||||
|
lv2.style.color = "green";
|
||||||
|
lv2.style.font = "Arial";
|
||||||
|
log.appendChild(lv2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async function get_plugins() {
|
||||||
|
|
||||||
|
if (path_set == false) {
|
||||||
|
alert("Please set your plugin/mod folder before continuing.")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url_set == false) {
|
||||||
|
alert("Please set your Modrinth Collection URL before continuing.")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const sel = document.getElementById("options");
|
||||||
|
const log = document.getElementById("log");
|
||||||
|
const submit = document.getElementById("submit");
|
||||||
|
const type = document.getElementById("type");
|
||||||
|
const url = document.getElementById("url");
|
||||||
|
submit.disabled = true;
|
||||||
|
document.getElementById("btn_download_compatible").disabled = true;
|
||||||
|
document.getElementById("btn_download_all").disabled = true;
|
||||||
|
document.getElementById("btn_download_zip").disabled = true;
|
||||||
|
all_compatible_versions = [];
|
||||||
|
all_incompatible_versions = [];
|
||||||
|
compatible_mods_ids = [];
|
||||||
|
incompatible_mods_ids = [];
|
||||||
|
|
||||||
|
log.innerHTML = "";
|
||||||
|
const id = url.value.split("/").pop();
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`http://localhost:3000/api/modrinth/collection/${id}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!data.projects) {
|
||||||
|
var not_found = document.createElement("p");
|
||||||
|
|
||||||
|
not_found.innerHTML = "The specified collection is empty or does not exist!";
|
||||||
|
not_found.style.color = "#fc5d07";
|
||||||
|
not_found.style.font = "Arial";
|
||||||
|
|
||||||
|
log.appendChild(not_found);
|
||||||
|
|
||||||
|
submit.disabled = false;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < data.projects["length"]; i++) {
|
||||||
|
let id = data.projects[i];
|
||||||
|
|
||||||
|
const versions = await fetch(
|
||||||
|
`https://api.modrinth.com/v2/project/${id}/version`,
|
||||||
|
).then((r) => r.json());
|
||||||
|
|
||||||
|
const project = await fetch(
|
||||||
|
`https://api.modrinth.com/v2/project/${id}`,
|
||||||
|
).then((r) => r.json());
|
||||||
|
|
||||||
|
let latest = null;
|
||||||
|
let latest_incompatible = null;
|
||||||
|
|
||||||
|
for (const version of versions) {
|
||||||
|
if (!version.loaders.includes(type.value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (version.game_versions.includes(sel.value)) {
|
||||||
|
if (latest === null ||new Date(version.date_published) > new Date(latest.date_published)) {
|
||||||
|
latest = version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
if (latest_incompatible === null || new Date(version.date_published) > new Date(latest_incompatible.date_published)
|
||||||
|
) {
|
||||||
|
latest_incompatible = version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var lv = document.createElement("p");
|
||||||
|
|
||||||
|
var project_link = `https://modrinth.com/plugin/${project.slug}`;
|
||||||
|
|
||||||
|
if (latest) {
|
||||||
|
lv.innerHTML = `Found matching version with name: ${latest["name"]}, <a href="${project_link}" target="_blank">link: ${project_link}</a>`;
|
||||||
|
lv.style.color = "green";
|
||||||
|
lv.style.font = "Arial";
|
||||||
|
all_compatible_versions.push(latest.id);
|
||||||
|
log.appendChild(lv);
|
||||||
|
} else if (latest_incompatible) {
|
||||||
|
lv.innerHTML = `No compatible version found for ${id}, name: ${project.title}, <a href="${project_link}" target="_blank">link: ${project_link}</a>`;
|
||||||
|
lv.style.color = "#fc5d07";
|
||||||
|
lv.style.font = "Arial";
|
||||||
|
all_incompatible_versions.push(latest_incompatible.id);
|
||||||
|
|
||||||
|
log.appendChild(lv);
|
||||||
|
} else {
|
||||||
|
lv.innerHTML = `Mod/Plugin ${project.title} does not have an option for the selected loader`
|
||||||
|
lv.style.color = "#fc5d07";
|
||||||
|
lv.style.font = "Arial";
|
||||||
|
log.appendChild(lv);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!versions.length) {
|
||||||
|
console.warn(`No versions found for ${project.id}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
submit.disabled = false;
|
||||||
|
|
||||||
|
if (all_compatible_versions.length > 0 || all_incompatible_versions > 0) {
|
||||||
|
document.getElementById("btn_download_compatible").disabled = false;
|
||||||
|
document.getElementById("btn_download_all").disabled = false;
|
||||||
|
document.getElementById("btn_download_zip").disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
a {
|
||||||
|
color: #c4c9ce;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:visited {
|
||||||
|
color: #c4c9ce;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log {
|
||||||
|
width: 100%;
|
||||||
|
height: 350px;
|
||||||
|
color: white;
|
||||||
|
overflow: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log_buttons {
|
||||||
|
margin-top: 20px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log_buttons button:disabled {
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "pgapluginupdater",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"author": "",
|
||||||
|
"type": "commonjs",
|
||||||
|
"main": "updater.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@zip.js/zip.js": "^2.8.52",
|
||||||
|
"express": "^5.2.1",
|
||||||
|
"js-yaml": "^5.3.0",
|
||||||
|
"node-deb": "^0.10.7",
|
||||||
|
"os": "^0.1.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=PGA Plugin Manager
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=exec
|
||||||
|
WorkingDirectory=/usr/lib/pga-plugin-manager
|
||||||
|
ExecStart=/usr/bin/pga-plugin-manager
|
||||||
|
Restart=on-failure
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -0,0 +1,273 @@
|
|||||||
|
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");
|
||||||
|
});
|
||||||
Reference in new issue
Block a user