From d52d9c62fc30e31e2c01bf6b63f9aedebdde216f Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 8 Aug 2022 04:27:28 +0200 Subject: Bunch of fixes and improvements, everything appears to work now --- api/src/middlewares/TestClient.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'api/src/middlewares') diff --git a/api/src/middlewares/TestClient.ts b/api/src/middlewares/TestClient.ts index 167b4c47..4fe631cb 100644 --- a/api/src/middlewares/TestClient.ts +++ b/api/src/middlewares/TestClient.ts @@ -5,6 +5,7 @@ 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 { green } from "picocolors"; export default function TestClient(app: Application) { const agent = new ProxyAgent(); @@ -18,11 +19,16 @@ export default function TestClient(app: Application) { //load asset cache let newAssetCache: Map = new Map(); - if(!fs.existsSync(path.join(__dirname, "..", "..", "assets", "cache"))) { - fs.mkdirSync(path.join(__dirname, "..", "..", "assets", "cache")); + let assetCacheDir = path.join(__dirname, "..", "..", "assets", "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(Object.entries(JSON.parse(rawdata.toString()))); } @@ -39,6 +45,7 @@ export default function TestClient(app: Application) { }); } else { + 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 @@ -49,11 +56,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()); } -- cgit 1.5.1