Skip to content

Commit 3420437

Browse files
committed
fix(api): lifetime of served images is too short
1 parent 549b3d4 commit 3420437

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/api/src/utils/fileserv.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sanitize from 'sanitize-filename';
55
export class FileServer {
66
static #instance: FileServer;
77
folder: string;
8+
/** default file's lifetime in seconds */
89
#defaultLifeTime = 60*60*24;
910
#timeouts: {filename: string, timeout: NodeJS.Timeout}[];
1011
/**
@@ -88,7 +89,7 @@ export class FileServer {
8889
return false;
8990
}
9091

91-
#initFile(filename: string, buffer:Buffer, lifetime = 600) {
92+
#initFile(filename: string, buffer:Buffer, lifetime = this.#defaultLifeTime) {
9293
try {
9394
writeFileSync(this.#resolveFile(filename), buffer);
9495
this.#timeouts.push({filename, timeout: setTimeout(() => {
@@ -104,12 +105,13 @@ export class FileServer {
104105
*
105106
* @param data the data to serv
106107
* @param filename its filename
108+
* @param lifetime how lang is the file available (in seconds)
107109
*/
108-
serv (data: Buffer, filename:string, lifetime?: number) {
109-
const exist = this.#resetFile(filename, lifetime||this.#defaultLifeTime);
110+
serv (data: Buffer, filename:string, lifetime = this.#defaultLifeTime) {
111+
const exist = this.#resetFile(filename, lifetime);
110112
if(exist) return `/files/${filename}`;
111113

112-
const create = this.#initFile(filename, data, lifetime||this.#defaultLifeTime);
114+
const create = this.#initFile(filename, data, lifetime);
113115
if(create) return `/files/${filename}`;
114116

115117
return `/files/${filename}`;
@@ -130,8 +132,8 @@ export class FileServer {
130132
}
131133

132134
/** renew lifetime */
133-
renew(filename:string, lifetime?:number) {
134-
return this.#resetFile(filename, lifetime||this.#defaultLifeTime);
135+
renew(filename:string, lifetime = this.#defaultLifeTime) {
136+
return this.#resetFile(filename, lifetime);
135137
}
136138

137139
/** get file */

0 commit comments

Comments
 (0)