1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import * as bluebird from 'bluebird'
import { readdir, remove, stat } from 'fs-extra'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { getResumableUploadPath } from '@server/helpers/upload'
import { SCHEDULER_INTERVALS_MS } from '@server/initializers/constants'
import { METAFILE_EXTNAME } from '@uploadx/core'
import { AbstractScheduler } from './abstract-scheduler'
const lTags = loggerTagsFactory('scheduler', 'resumable-upload', 'cleaner')
export class RemoveDanglingResumableUploadsScheduler extends AbstractScheduler {
private static instance: AbstractScheduler
private lastExecutionTimeMs: number
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.removeDanglingResumableUploads
private constructor () {
super()
this.lastExecutionTimeMs = new Date().getTime()
}
protected async internalExecute () {
const path = getResumableUploadPath()
const files = await readdir(path)
const metafiles = files.filter(f => f.endsWith(METAFILE_EXTNAME))
if (metafiles.length === 0) return
logger.debug('Reading resumable video upload folder %s with %d files', path, metafiles.length, lTags())
try {
await bluebird.map(metafiles, metafile => {
return this.deleteIfOlderThan(metafile, this.lastExecutionTimeMs)
}, { concurrency: 5 })
} catch (error) {
logger.error('Failed to handle file during resumable video upload folder cleanup', { error, ...lTags() })
} finally {
this.lastExecutionTimeMs = new Date().getTime()
}
}
private async deleteIfOlderThan (metafile: string, olderThan: number) {
const metafilePath = getResumableUploadPath(metafile)
const statResult = await stat(metafilePath)
// Delete uploads that started since a long time
if (statResult.ctimeMs < olderThan) {
await remove(metafilePath)
const datafile = metafilePath.replace(new RegExp(`${METAFILE_EXTNAME}$`), '')
await remove(datafile)
}
}
static get Instance () {
return this.instance || (this.instance = new this())
}
}
|