+const timeTable = {
+ ms: 1,
+ second: 1000,
+ minute: 60000,
+ hour: 3600000,
+ day: 3600000 * 24,
+ week: 3600000 * 24 * 7,
+ month: 3600000 * 24 * 30
+}
+export function parseDuration (duration: number | string): number {
+ if (typeof duration === 'number') return duration
+
+ if (typeof duration === 'string') {
+ const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
+
+ if (split.length === 3) {
+ const len = parseFloat(split[1])
+ let unit = split[2].replace(/s$/i,'').toLowerCase()
+ if (unit === 'm') {
+ unit = 'ms'
+ }
+
+ return (len || 1) * (timeTable[unit] || 0)
+ }
+ }
+
+ throw new Error('Duration could not be properly parsed')
+}
+
+function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
+ Object.keys(savedFields).forEach(key => {
+ const value = savedFields[key]
+ instance.set(key, value)
+ })
+}
+
+let serverActor: ActorModel
+async function getServerActor () {
+ if (serverActor === undefined) {
+ const application = await ApplicationModel.load()
+ if (!application) throw Error('Could not load Application from database.')
+
+ serverActor = application.Account.Actor
+ }
+
+ if (!serverActor) {
+ logger.error('Cannot load server actor.')
+ process.exit(0)
+ }
+
+ return Promise.resolve(serverActor)
+}
+
+function generateVideoTmpPath (target: string | ParseTorrent) {
+ const id = typeof target === 'string' ? target : target.infoHash
+
+ const hash = sha256(id)
+ return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
+}
+
+function getSecureTorrentName (originalName: string) {
+ return sha256(originalName) + '.torrent'
+}
+