use CacheService

This commit is contained in:
SrGooglo 2023-05-30 01:06:10 +00:00
parent ff49cadde0
commit f1d6324641
4 changed files with 54 additions and 4 deletions

View File

@ -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)
}
}
}

View File

@ -225,6 +225,8 @@ export default class FilesController extends Controller {
try { try {
// remove file from cache // remove file from cache
await fs.promises.unlink(req.fileResult.filepath) await fs.promises.unlink(req.fileResult.filepath)
// programatically remove file from cache in the
} catch (error) { } catch (error) {
console.log("Failed to remove file from cache", error) console.log("Failed to remove file from cache", error)

View File

@ -200,9 +200,6 @@ export default async (payload) => {
return reject(err) return reject(err)
}) })
// remove file from cache
await fs.promises.unlink(file.filepath)
// get url location // get url location
const remoteUrlObj = global.storage.composeRemoteURL(uploadPath) const remoteUrlObj = global.storage.composeRemoteURL(uploadPath)
@ -237,6 +234,8 @@ export default async (payload) => {
{ concurrency: 10 } { concurrency: 10 }
) )
return resolve({ return resolve({
files: processedFiles, files: processedFiles,
failed: failedFiles, failed: failedFiles,

View File

@ -74,7 +74,7 @@ export default class ChunkedUpload {
} }
} }
fs.rmdirSync(chunkPartsPath, { recursive: true }) //fs.rmdirSync(chunkPartsPath, { recursive: true })
return mergedFilePath return mergedFilePath
} }
@ -192,12 +192,15 @@ export default class ChunkedUpload {
if (buildResult) { if (buildResult) {
req.isLastPart = true req.isLastPart = true
req.fileResult = { req.fileResult = {
fileHash,
filepath: buildResult, filepath: buildResult,
filename: finalFilename, filename: finalFilename,
mimetype: realMimeType, mimetype: realMimeType,
size: fileSize, size: fileSize,
} }
global.cacheService.appendToDeletion(buildResult)
next() next()
} }
} catch (error) { } catch (error) {