From f1d63246413b28abab4e97360011ab28763b7b93 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Tue, 30 May 2023 01:06:10 +0000 Subject: [PATCH] use `CacheService` --- .../server/src/classes/CacheService/index.js | 46 +++++++++++++++++++ .../src/controllers/FilesController/index.js | 2 + .../services/uploadBodyFiles.js | 5 +- .../server/src/lib/chunkedUpload/index.js | 5 +- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 packages/server/src/classes/CacheService/index.js diff --git a/packages/server/src/classes/CacheService/index.js b/packages/server/src/classes/CacheService/index.js new file mode 100644 index 00000000..911ce318 --- /dev/null +++ b/packages/server/src/classes/CacheService/index.js @@ -0,0 +1,46 @@ +import fs from "fs" +import path from "path" + +export default class CacheService { + watchingFiles = new Set() + + static deletionInterval = 1000 * 60 * 5 + + appendToDeletion(filepath) { + // create a interval of 5 minutes to delete the file + // check the last time the file was accessed and if it was accessed in the last 5 minutes + // reset the interval until the file is not accessed for 5 minutes and then delete it + try { + const createInterval = () => { + return setInterval(() => { + const stats = fs.statSync(filepath) + + stats.atime = new Date(stats.atime) + + if (stats.atime.getTime() + CacheService.deletionInterval < Date.now()) { + clearInterval(this.watchingFiles.get(filepath).interval) + + this.watchingFiles.delete(filepath) + + fs.promises.unlink(filepath) + } else { + console.log(`[${filepath}] was accessed in the last 5 minutes, resetting deletion interval`) + + clearInterval(this.watchingFiles.get(filepath).interval) + + this.watchingFiles.get(filepath).interval = createInterval() + } + }) + } + + this.watchingFiles.add({ + filepath, + interval: createInterval() + }) + } catch (error) { + console.error(error) + + return fs.promises.unlink(filepath) + } + } +} \ No newline at end of file diff --git a/packages/server/src/controllers/FilesController/index.js b/packages/server/src/controllers/FilesController/index.js index 5354b9da..bcb60489 100755 --- a/packages/server/src/controllers/FilesController/index.js +++ b/packages/server/src/controllers/FilesController/index.js @@ -225,6 +225,8 @@ export default class FilesController extends Controller { try { // remove file from cache await fs.promises.unlink(req.fileResult.filepath) + + // programatically remove file from cache in the } catch (error) { console.log("Failed to remove file from cache", error) diff --git a/packages/server/src/controllers/FilesController/services/uploadBodyFiles.js b/packages/server/src/controllers/FilesController/services/uploadBodyFiles.js index 415a4d5e..0fe4560b 100755 --- a/packages/server/src/controllers/FilesController/services/uploadBodyFiles.js +++ b/packages/server/src/controllers/FilesController/services/uploadBodyFiles.js @@ -200,9 +200,6 @@ export default async (payload) => { return reject(err) }) - // remove file from cache - await fs.promises.unlink(file.filepath) - // get url location const remoteUrlObj = global.storage.composeRemoteURL(uploadPath) @@ -237,6 +234,8 @@ export default async (payload) => { { concurrency: 10 } ) + + return resolve({ files: processedFiles, failed: failedFiles, diff --git a/packages/server/src/lib/chunkedUpload/index.js b/packages/server/src/lib/chunkedUpload/index.js index df98eeff..976a3527 100644 --- a/packages/server/src/lib/chunkedUpload/index.js +++ b/packages/server/src/lib/chunkedUpload/index.js @@ -74,7 +74,7 @@ export default class ChunkedUpload { } } - fs.rmdirSync(chunkPartsPath, { recursive: true }) + //fs.rmdirSync(chunkPartsPath, { recursive: true }) return mergedFilePath } @@ -192,12 +192,15 @@ export default class ChunkedUpload { if (buildResult) { req.isLastPart = true req.fileResult = { + fileHash, filepath: buildResult, filename: finalFilename, mimetype: realMimeType, size: fileSize, } + global.cacheService.appendToDeletion(buildResult) + next() } } catch (error) {