summary refs log tree commit diff
path: root/src/api/middlewares/TestClient.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 22:18:59 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 22:18:59 +1000
commit0cd9a46eea260c299db2e2983f7214ab8b119d29 (patch)
tree5fbb98e7adcfeab81594732089474afdde5893f9 /src/api/middlewares/TestClient.ts
parentMerge branch 'master' into feat/captchaVerify (diff)
parentMerge remote-tracking branch 'Puyodead1/patch/prettier-config' into staging (diff)
downloadserver-0cd9a46eea260c299db2e2983f7214ab8b119d29.tar.xz
Merge remote-tracking branch 'upstream/staging' into feat/captchaVerify
Diffstat (limited to '')
-rw-r--r--src/api/middlewares/TestClient.ts (renamed from api/src/middlewares/TestClient.ts)41
1 files changed, 24 insertions, 17 deletions
diff --git a/api/src/middlewares/TestClient.ts b/src/api/middlewares/TestClient.ts

index 7292868c..c8ea57f6 100644 --- a/api/src/middlewares/TestClient.ts +++ b/src/api/middlewares/TestClient.ts
@@ -1,17 +1,19 @@ import express, { Request, Response, Application } from "express"; -import fs, { writeFile } from "fs"; +import fs from "fs"; import path from "path"; import fetch, { Response as FetchResponse, Headers } from "node-fetch"; import ProxyAgent from 'proxy-agent'; import { Config } from "@fosscord/util"; import { AssetCacheItem } from "../util/entities/AssetCacheItem" -import { FileLogger } from "typeorm"; +import { green } from "picocolors"; + +const AssetsPath = path.join(__dirname, "..", "..", "..", "assets") export default function TestClient(app: Application) { const agent = new ProxyAgent(); //build client page - let html = fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "index.html"), { encoding: "utf8" }); + let html = fs.readFileSync(path.join(AssetsPath, "index.html"), { encoding: "utf8" }); html = applyEnv(html); html = applyInlinePlugins(html); html = applyPlugins(html); @@ -19,15 +21,20 @@ export default function TestClient(app: Application) { //load asset cache let newAssetCache: Map<string, AssetCacheItem> = new Map<string, AssetCacheItem>(); - if(!fs.existsSync(path.join(__dirname, "..", "..", "assets", "cache"))) { - fs.mkdirSync(path.join(__dirname, "..", "..", "assets", "cache")); + let assetCacheDir = path.join(AssetsPath, "cache"); + if(process.env.ASSET_CACHE_DIR) + assetCacheDir = process.env.ASSET_CACHE_DIR + + console.log(`[TestClient] ${green(`Using asset cache path: ${assetCacheDir}`)}`) + if(!fs.existsSync(assetCacheDir)) { + fs.mkdirSync(assetCacheDir); } - if(fs.existsSync(path.join(__dirname, "..", "..", "assets", "cache", "index.json"))) { - let rawdata = fs.readFileSync(path.join(__dirname, "..", "..", "assets", "cache", "index.json")); + if(fs.existsSync(path.join(assetCacheDir, "index.json"))) { + let rawdata = fs.readFileSync(path.join(assetCacheDir, "index.json")); newAssetCache = new Map<string, AssetCacheItem>(Object.entries(JSON.parse(rawdata.toString()))); } - app.use("/assets", express.static(path.join(__dirname, "..", "..", "assets"))); + app.use("/assets", express.static(path.join(AssetsPath))); app.get("/assets/:file", async (req: Request, res: Response) => { delete req.headers.host; let response: FetchResponse; @@ -40,7 +47,7 @@ export default function TestClient(app: Application) { }); } else { - console.log(`CACHE MISS! Asset file: ${req.params.file}`); + console.log(`[TestClient] Downloading file not yet cached! Asset file: ${req.params.file}`); response = await fetch(`https://discord.com/assets/${req.params.file}`, { agent, // @ts-ignore @@ -51,11 +58,11 @@ export default function TestClient(app: Application) { //set cache info assetCacheItem.Headers = Object.fromEntries(stripHeaders(response.headers)); - assetCacheItem.FilePath = path.join(__dirname, "..", "..", "assets", "cache", req.params.file); + assetCacheItem.FilePath = path.join(assetCacheDir, req.params.file); assetCacheItem.Key = req.params.file; //add to cache and save newAssetCache.set(req.params.file, assetCacheItem); - fs.writeFileSync(path.join(__dirname, "..", "..", "assets", "cache", "index.json"), JSON.stringify(Object.fromEntries(newAssetCache), null, 4)); + fs.writeFileSync(path.join(assetCacheDir, "index.json"), JSON.stringify(Object.fromEntries(newAssetCache), null, 4)); //download file fs.writeFileSync(assetCacheItem.FilePath, await response.buffer()); } @@ -72,7 +79,7 @@ export default function TestClient(app: Application) { if(!useTestClient) return res.send("Test client is disabled on this instance. Use a stand-alone client to connect this instance.") - res.send(fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "developers.html"), { encoding: "utf8" })); + res.send(fs.readFileSync(path.join(__dirname, "..", "..", "..", "assets", "developers.html"), { encoding: "utf8" })); }); app.get("*", (req: Request, res: Response) => { const { useTestClient } = Config.get().client; @@ -108,7 +115,7 @@ function applyEnv(html: string): string { function applyPlugins(html: string): string { // plugins - let files = fs.readdirSync(path.join(__dirname, "..", "..", "assets", "plugins")); + let files = fs.readdirSync(path.join(AssetsPath, "plugins")); let plugins = ""; files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script src='/assets/plugins/${x}'></script>\n`; }); return html.replaceAll("<!-- plugin marker -->", plugins); @@ -116,7 +123,7 @@ function applyPlugins(html: string): string { function applyInlinePlugins(html: string): string{ // inline plugins - let files = fs.readdirSync(path.join(__dirname, "..", "..", "assets", "inline-plugins")); + let files = fs.readdirSync(path.join(AssetsPath, "inline-plugins")); let plugins = ""; files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script src='/assets/inline-plugins/${x}'></script>\n\n`; }); return html.replaceAll("<!-- inline plugin marker -->", plugins); @@ -124,9 +131,9 @@ function applyInlinePlugins(html: string): string{ function applyPreloadPlugins(html: string): string{ //preload plugins - let files = fs.readdirSync(path.join(__dirname, "..", "..", "assets", "preload-plugins")); + let files = fs.readdirSync(path.join(AssetsPath, "preload-plugins")); let plugins = ""; - files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script>${fs.readFileSync(path.join(__dirname, "..", "..", "assets", "preload-plugins", x))}</script>\n`; }); + files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script>${fs.readFileSync(path.join(AssetsPath, "preload-plugins", x))}</script>\n`; }); return html.replaceAll("<!-- preload plugin marker -->", plugins); } @@ -144,4 +151,4 @@ function stripHeaders(headers: Headers): Headers { headers.delete(headerName); }); return headers; -} \ No newline at end of file +}