diff --git a/scripts/studioServer.ts b/scripts/studioServer.ts index b1505e583..05f82cd07 100644 --- a/scripts/studioServer.ts +++ b/scripts/studioServer.ts @@ -44,6 +44,36 @@ async function prepareFile(url: string): Promise { } return null } +async function handleDelete(req: http.IncomingMessage, res: ServerResponse) { + let body = "" + req.on("data", (chunk) => { + body = body + chunk + }) + const paths = req.url.split("/") + console.log("Got a valid delete to:", paths.join("/")) + for (let i = 1; i < paths.length; i++) { + const p = paths.slice(0, i) + const dir = STATIC_PATH + p.join("/") + if (!fs.existsSync(dir)) { + res.writeHead(304, { "Content-Type": MIME_TYPES.html }) + res.write("No parent directory, nothing deleted", "utf8") + res.end() + return + } + } + const path = STATIC_PATH + paths.join("/") + if(!fs.existsSync(path)){ + res.writeHead(304, { "Content-Type": MIME_TYPES.html }) + res.write("File not found", "utf8") + res.end() + return + } + + fs.renameSync(path, path+".bak") + res.writeHead(200, { "Content-Type": MIME_TYPES.html }) + res.write("File moved to backup", "utf8") + res.end() +} async function handlePost(req: http.IncomingMessage, res: ServerResponse) { let body = "" @@ -101,6 +131,11 @@ http.createServer(async (req: http.IncomingMessage, res) => { return } + if(req.method === "DELETE"){ + await handleDelete(req, res) + return + } + const url = new URL(`http://127.0.0.1/` + req.url) console.log("URL pathname is") if (url.pathname.endsWith("overview")) {