From 7fc2d8027c30b4c86139a5d3398f13d3e3a3755b Mon Sep 17 00:00:00 2001
From: Rafael Araujo Lehmkuhl <rafael.lehmkuhl93@gmail.com>
Date: Fri, 24 Nov 2023 15:25:45 -0300
Subject: [PATCH] Offer download of video recording backups if they are
 available

Remove them, independently if they were downloaded or not.
---
 src/stores/video.ts | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/stores/video.ts b/src/stores/video.ts
index 3e9718275..5243fd643 100644
--- a/src/stores/video.ts
+++ b/src/stores/video.ts
@@ -1,4 +1,6 @@
 import { useStorage } from '@vueuse/core'
+import { saveAs } from 'file-saver'
+import localforage from 'localforage'
 import { defineStore } from 'pinia'
 import { reactive } from 'vue'
 
@@ -6,5 +8,22 @@ export const useVideoStore = defineStore('video', () => {
   const availableIceIps = reactive<string[]>([])
   const allowedIceIps = useStorage<string[]>('cockpit-allowed-stream-ips', [])
 
+  // Offer download of backuped videos
+  const cockpitVideoDB = localforage.createInstance({
+    driver: localforage.INDEXEDDB,
+    name: 'CockpitVideoDB',
+    storeName: 'cockpit-video-db',
+    version: 1.0,
+    description: 'Local backups of Cockpit video recordings to be retrieved in case of failure.',
+  })
+
+  cockpitVideoDB.iterate((videoFile, videoName) => {
+    const blob = (videoFile as Blob[]).reduce((a, b) => new Blob([a, b], { type: 'video/webm' }))
+    saveAs(blob, videoName)
+  })
+  cockpitVideoDB.iterate((_, videoName) => {
+    cockpitVideoDB.removeItem(videoName)
+  })
+
   return { availableIceIps, allowedIceIps }
 })