diff options
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r-- | server/helpers/utils.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 8fa861281..834d788c8 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts | |||
@@ -104,6 +104,36 @@ function computeResolutionsToTranscode (videoFileHeight: number) { | |||
104 | return resolutionsEnabled | 104 | return resolutionsEnabled |
105 | } | 105 | } |
106 | 106 | ||
107 | const timeTable = { | ||
108 | ms: 1, | ||
109 | second: 1000, | ||
110 | minute: 60000, | ||
111 | hour: 3600000, | ||
112 | day: 3600000 * 24, | ||
113 | week: 3600000 * 24 * 7, | ||
114 | month: 3600000 * 24 * 30 | ||
115 | } | ||
116 | export function parseDuration (duration: number | string, defaultDuration: number): number { | ||
117 | if (typeof duration === 'number') return duration | ||
118 | |||
119 | if (typeof duration === 'string') { | ||
120 | const split = duration.match(/^([\d\.,]+)\s?(\w+)$/) | ||
121 | |||
122 | if (split.length === 3) { | ||
123 | const len = parseFloat(split[1]) | ||
124 | let unit = split[2].replace(/s$/i,'').toLowerCase() | ||
125 | if (unit === 'm') { | ||
126 | unit = 'ms' | ||
127 | } | ||
128 | |||
129 | return (len || 1) * (timeTable[unit] || 0) | ||
130 | } | ||
131 | } | ||
132 | |||
133 | logger.error('Duration could not be properly parsed, defaulting to ' + defaultDuration) | ||
134 | return defaultDuration | ||
135 | } | ||
136 | |||
107 | function resetSequelizeInstance (instance: Model<any>, savedFields: object) { | 137 | function resetSequelizeInstance (instance: Model<any>, savedFields: object) { |
108 | Object.keys(savedFields).forEach(key => { | 138 | Object.keys(savedFields).forEach(key => { |
109 | const value = savedFields[key] | 139 | const value = savedFields[key] |