/* eslint-disable no-console */ import { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js"; import { join } from "path"; import { globalEnv } from "../../../common/globalEnv.js"; import { link } from "node:fs/promises"; import FileSize from "../../../common/FileSize.js"; import ensureDir from "../../../common/FileApi.js"; import { LocalFile } from "@entity-access/server-pages/dist/core/LocalFile.js"; import { randomUUID } from "crypto"; import BaseDiskCache from "@entity-access/server-pages/dist/cache/BaseDiskCache.js"; const cacheRoot = globalEnv.cacheDir; const serverCache = globalEnv.cacheDir; ensureDir(serverCache); const tempSize = FileSize.parse(globalEnv.tmpSize); const minSize = tempSize / 2; @RegisterSingleton export default class DiskCacheService extends BaseDiskCache { private tmp: BaseDiskCache; constructor() { super({ root: join(cacheRoot, "fc"), keepTTLSeconds: 86400, minSize, maxAge:7 }); this.tmp = new BaseDiskCache({ root: join(cacheRoot, "tmp"), keepTTLSeconds: 60*60, maxAge: 1 }); } async link(localFile: LocalFile, fileName: string, contentType: string) { const file = this.tmp.createTempFileDeleteOnExit([ randomUUID(), fileName], fileName, contentType); await link(localFile.path, file.path); return file; } } // constructor() { // // it is important to clean // // in the beginning to avoid no file space errors. // setTimeout(() => this.clear().catch(console.error), 1000); // } // async getOrCreate(path: string, factory: (filePath) => Promise, ext = ".dat" ) { // path = join(serverCache, path); // const parsedPath = parse(path); // ensureDir(parsedPath.dir); // let error: Error; // for (let index = 0; index < 10; index++) { // using lock = await LockFile.lock(path); // if (existsSync(path)) { // // change // return new LocalFile(path, void 0, void 0, doNothing); // } // const tmpPath = join(serverCache, randomUUID() + (parsedPath.ext || ext)); // await factory(tmpPath); // try { // await fsp.rename(tmpPath, path); // } catch (e) { // if (existsSync(tmpPath)) { // unlink(tmpPath).catch(doNothing); // } // error = e; // await sleep(3000); // continue; // } // return new LocalFile(path, void 0, void 0, doNothing); // } // throw new EntityAccessError(`Failed to write file due to error ${error.stack ?? error}`); // } // async clear() { // try { // using l = await LockFile.lock("clean-" + cacheRoot, void 0, 10000); // const s = await fsp.statfs(cacheRoot); // const freeSize = s.bavail * s.bsize; // if (freeSize < minSize) { // let target = minSize - freeSize; // const finalTarget = target; // console.log(`Clearing temp files to delete ${target} bytes.`); // let old = DateTime.now.addDays(-8); // while(target > 0) { // old = old.addDays(1); // const dir = await fsp.opendir(cacheRoot, { recursive: true }); // const filesToDelete = []; // try { // for await (const entry of dir) { // if (!entry.isFile()) { // continue; // } // try { // const filePath = join(entry.parentPath, entry.name); // const st = await fsp.stat(filePath); // if (st.mtimeMs < old.msSinceEpoch) { // target -= st.size; // if (filesToDelete.length < 100) { // filesToDelete.push(filePath); // } else { // break; // } // // console.log(`${filePath} deleted.`); // // await fsp.unlink(filePath); // if (target < 0) { // break; // } // } // } catch { // // we will ignore the error... // } // } // } finally { // try { // await dir.close(); // } catch { // // ignore error.. // } // } // for (const element of filesToDelete) { // console.log(`${element} deleted.`); // try { // await fsp.unlink(element); // } catch { // // ignore this error as someone else might have deleted // // it // } // } // } // console.log(`Temp folder cleared, ${finalTarget-target} bytes freed.`); // } // } catch (error) { // console.error(error); // } // await sleep(30000); // setTimeout(() => this.clear().catch(console.error), 60000); // } // async link(localFile: LocalFile, fileName: string, contentType: string) { // const dir = join(serverCache, "tmp", randomUUID()); // ensureDir(dir); // const path = join(dir, fileName); // await link(localFile.path, path); // return new LocalFile(path, fileName, contentType, () => unlinkSync(path)); // } // }