+import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models'
+import { CacheFileObject, VideoStreamingPlaylistType } from '@shared/models'
+import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
+
+async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
+ const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
+
+ if (redundancyModel) {
+ return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
+ }
+
+ return createCacheFile(cacheFileObject, video, byActor, t)
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+ createOrUpdateCacheFile
+}
+
+// ---------------------------------------------------------------------------
+
+function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
+ const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
+
+ return VideoRedundancyModel.create(attributes, { transaction: t })
+}
+
+function updateCacheFile (
+ cacheFileObject: CacheFileObject,
+ redundancyModel: MVideoRedundancy,
+ video: MVideoWithAllFiles,
+ byActor: MActorId,
+ t: Transaction
+) {
+ if (redundancyModel.actorId !== byActor.id) {
+ throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.')
+ }
+
+ const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)