2022-07-24 20:32:05 +02:00
|
|
|
const { AudioMimeType } = require('../utils/constants')
|
|
|
|
const { getAudioMimeTypeFromExtname } = require('../utils/fileUtils')
|
2021-09-08 16:15:54 +02:00
|
|
|
const DEFAULT_EXPIRATION = 1000 * 60 * 60 // 60 minutes
|
2022-06-12 22:59:36 +02:00
|
|
|
const DEFAULT_TIMEOUT = 1000 * 60 * 30 // 30 minutes
|
2022-07-24 20:32:05 +02:00
|
|
|
|
2022-09-27 01:07:31 +02:00
|
|
|
class AbManagerTask {
|
|
|
|
constructor() {
|
2021-09-04 21:17:26 +02:00
|
|
|
this.id = null
|
2022-04-22 01:52:28 +02:00
|
|
|
this.libraryItemId = null
|
2022-09-27 01:07:31 +02:00
|
|
|
this.libraryItemPath = null
|
2021-09-04 21:17:26 +02:00
|
|
|
this.type = null
|
|
|
|
|
|
|
|
this.dirpath = null
|
2022-04-22 01:52:28 +02:00
|
|
|
this.path = null
|
2021-09-04 21:17:26 +02:00
|
|
|
this.ext = null
|
|
|
|
this.filename = null
|
|
|
|
this.size = 0
|
2022-09-27 01:07:31 +02:00
|
|
|
this.toneMetadataObject = null
|
|
|
|
this.originalTrackPaths = []
|
2021-09-04 21:17:26 +02:00
|
|
|
|
|
|
|
this.userId = null
|
|
|
|
this.isReady = false
|
2021-09-15 03:45:00 +02:00
|
|
|
this.isTimedOut = false
|
2021-09-04 21:17:26 +02:00
|
|
|
|
|
|
|
this.startedAt = null
|
|
|
|
this.finishedAt = null
|
|
|
|
this.expiresAt = null
|
|
|
|
|
|
|
|
this.expirationTimeMs = 0
|
2021-09-15 03:45:00 +02:00
|
|
|
this.timeoutTimeMs = 0
|
|
|
|
|
|
|
|
this.timeoutTimer = null
|
|
|
|
this.expirationTimer = null
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get mimeType() {
|
2022-07-24 20:32:05 +02:00
|
|
|
return getAudioMimeTypeFromExtname(this.ext) || AudioMimeType.MP3
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2022-04-22 01:52:28 +02:00
|
|
|
libraryItemId: this.libraryItemId,
|
2021-09-04 21:17:26 +02:00
|
|
|
type: this.type,
|
|
|
|
dirpath: this.dirpath,
|
2022-04-22 01:52:28 +02:00
|
|
|
path: this.path,
|
2021-09-04 21:17:26 +02:00
|
|
|
ext: this.ext,
|
|
|
|
filename: this.filename,
|
|
|
|
size: this.size,
|
|
|
|
userId: this.userId,
|
|
|
|
isReady: this.isReady,
|
|
|
|
startedAt: this.startedAt,
|
|
|
|
finishedAt: this.finishedAt,
|
2022-09-27 01:07:31 +02:00
|
|
|
expirationSeconds: this.expirationSeconds,
|
|
|
|
toneMetadataObject: this.toneMetadataObject
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 01:07:31 +02:00
|
|
|
setData(downloadData) {
|
|
|
|
downloadData.startedAt = Date.now()
|
|
|
|
downloadData.isProcessing = true
|
|
|
|
this.construct(downloadData)
|
|
|
|
}
|
|
|
|
|
2021-09-04 21:17:26 +02:00
|
|
|
construct(download) {
|
|
|
|
this.id = download.id
|
2022-04-22 01:52:28 +02:00
|
|
|
this.libraryItemId = download.libraryItemId
|
2022-09-27 01:07:31 +02:00
|
|
|
this.libraryItemPath = download.libraryItemPath
|
2021-09-04 21:17:26 +02:00
|
|
|
this.type = download.type
|
|
|
|
|
|
|
|
this.dirpath = download.dirpath
|
2022-04-22 01:52:28 +02:00
|
|
|
this.path = download.path
|
2021-09-04 21:17:26 +02:00
|
|
|
this.ext = download.ext
|
|
|
|
this.filename = download.filename
|
|
|
|
this.size = download.size || 0
|
2022-09-27 01:07:31 +02:00
|
|
|
this.originalTrackPaths = download.originalTrackPaths
|
2021-09-04 21:17:26 +02:00
|
|
|
|
|
|
|
this.userId = download.userId
|
|
|
|
this.isReady = !!download.isReady
|
|
|
|
|
|
|
|
this.startedAt = download.startedAt
|
|
|
|
this.finishedAt = download.finishedAt || null
|
|
|
|
|
|
|
|
this.expirationTimeMs = download.expirationTimeMs || DEFAULT_EXPIRATION
|
2021-09-15 03:45:00 +02:00
|
|
|
this.timeoutTimeMs = download.timeoutTimeMs || DEFAULT_TIMEOUT
|
|
|
|
|
2021-09-04 21:17:26 +02:00
|
|
|
this.expiresAt = download.expiresAt || null
|
|
|
|
}
|
|
|
|
|
|
|
|
setComplete(fileSize) {
|
|
|
|
this.finishedAt = Date.now()
|
|
|
|
this.size = fileSize
|
|
|
|
this.isReady = true
|
|
|
|
this.expiresAt = this.finishedAt + this.expirationTimeMs
|
|
|
|
}
|
|
|
|
|
|
|
|
setExpirationTimer(callback) {
|
2021-09-15 03:45:00 +02:00
|
|
|
this.expirationTimer = setTimeout(() => {
|
2021-09-04 21:17:26 +02:00
|
|
|
if (callback) {
|
|
|
|
callback(this)
|
|
|
|
}
|
|
|
|
}, this.expirationTimeMs)
|
|
|
|
}
|
2021-09-15 03:45:00 +02:00
|
|
|
|
|
|
|
setTimeoutTimer(callback) {
|
|
|
|
this.timeoutTimer = setTimeout(() => {
|
|
|
|
if (callback) {
|
|
|
|
this.isTimedOut = true
|
|
|
|
callback(this)
|
|
|
|
}
|
|
|
|
}, this.timeoutTimeMs)
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeoutTimer() {
|
|
|
|
clearTimeout(this.timeoutTimer)
|
|
|
|
}
|
|
|
|
|
|
|
|
clearExpirationTimer() {
|
|
|
|
clearTimeout(this.expirationTimer)
|
|
|
|
}
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
2022-09-27 01:07:31 +02:00
|
|
|
module.exports = AbManagerTask
|