summary refs log tree commit diff
path: root/api/src/middlewares
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-08 04:27:28 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-09 23:28:27 +0200
commitd52d9c62fc30e31e2c01bf6b63f9aedebdde216f (patch)
treee0f7fd130aeeecb9964e1f359eea6b4e124de7da /api/src/middlewares
parentClean dependencies (diff)
downloadserver-d52d9c62fc30e31e2c01bf6b63f9aedebdde216f.tar.xz
Bunch of fixes and improvements, everything appears to work now
Diffstat (limited to 'api/src/middlewares')
-rw-r--r--api/src/middlewares/TestClient.ts19
1 files changed, 13 insertions, 6 deletions
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<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(__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<string, AssetCacheItem>(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());
 		}