Skip to content

Commit

Permalink
Merge pull request #3 from KillahB33/update-fix
Browse files Browse the repository at this point in the history
fixing update logic
  • Loading branch information
KillahB33 authored Aug 19, 2024
2 parents fcf7dfa + 821defa commit 18dc269
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ko_fi: killahb33
1 change: 1 addition & 0 deletions web/pages/api/list-mods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default async function handler(
version: packageJson.version,
sptVersion: packageJson.sptVersion,
update: updateAvailable,
githubUrl: packageJson.githubUrl,
};
})
);
Expand Down
29 changes: 21 additions & 8 deletions web/pages/api/update-mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";
import path from "path";
import { getDownloadUrl, extractAndClean } from "./github-util";
import {
getDownloadUrl,
extractAndClean,
getLatestReleaseVersion,
} from "./github-util";

const updateModFromGitHub = async (
req: NextApiRequest,
Expand All @@ -10,26 +14,35 @@ const updateModFromGitHub = async (
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}

console.log(req.body);
const { githubUrl, folderName } = req.body;

if (!githubUrl || !githubUrl.includes("github.com")) {
return res.status(400).json({ error: "Invalid GitHub URL." });
if (!githubUrl || !folderName) {
console.log("Invalid update request");
return res.status(400).json({ error: "Invalid Update Request." });
}

const regex =
/https:\/\/github\.com\/([^/]+)\/([^/]+)\/releases\/download\/([^/]+)\/.*/;

// Use match to extract the parts from the URL
const match = githubUrl.match(regex);

const [_, owner, repo] = match;

try {
const latestVersion = await getLatestReleaseVersion(githubUrl);
const latestReleaseUrl = `https://github.com/${owner}/${repo}/releases/tag/v${latestVersion}`;
// Fetch the specific release page content
const assetUrl = await getDownloadUrl(githubUrl);
const assetUrl = await getDownloadUrl(latestReleaseUrl);
if (!assetUrl) {
throw new Error("Failed to retrieve the asset URL");
}

// Extract mod path and save the file
const modPath = path.join("/app", "user", "mods", folderName);

// Clean existing files
fs.rmdirSync(modPath, { recursive: true });

await fs.promises.rm(modPath, { recursive: true });
const modName = await extractAndClean(assetUrl);

res.status(200).json({ message: `${modName} updated successfully` });
Expand Down

0 comments on commit 18dc269

Please sign in to comment.