Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

feat: implement cleanup functions for old recordings, thumbnails, uploads, and library entries

CristianOlivera19f4e1f4c0c28
diff --git a/hooks/useScreenRecording.ts b/hooks/useScreenRecording.ts
index d4f277b..47e708c 100644
--- a/hooks/useScreenRecording.ts
+++ b/hooks/useScreenRecording.ts
@@ -17,6 +17,28 @@ function generateVideoId(): string {
return `vid_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
}
+// Delete the currentVideo record if it is older than 7 days.
+// openvidDB is only a recording→editor handoff cache; stale blobs waste storage.
+async function cleanupOldRecording(db: IDBDatabase): Promise<void> {
+ const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000;
+ const cutoff = Date.now() - SEVEN_DAYS_MS;
+ return new Promise((resolve) => {
+ try {
+ const transaction = db.transaction("videos", "readwrite");
+ const store = transaction.objectStore("videos");
+ const getReq = store.get("currentVideo");
+ getReq.onsuccess = () => {
+ const record = getReq.result as { timestamp?: number } | undefined;
+ if (record && record.timestamp && record.timestamp < cutoff) {
+ store.delete("currentVideo");
+ }
+ };
+ transaction.oncomplete = () => resolve();
+ transaction.onerror = () => resolve();
+ } catch { resolve(); }
+ });
+}
+
async function getDB(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const dbName = "openvidDB";
@@ -43,9 +65,13 @@ async function getDB(): Promise<IDBDatabase> {
retryDb.createObjectStore(storeName);
}
};
- retryRequest.onsuccess = () => resolve(retryRequest.result);
+ retryRequest.onsuccess = () => {
+ cleanupOldRecording(retryRequest.result).catch(() => {});
+ resolve(retryRequest.result);
+ };
retryRequest.onerror = () => reject(retryRequest.error);
} else {
+ cleanupOldRecording(db).catch(() => {});
resolve(db);
}
};
diff --git a/lib/thumbnail-cache.ts b/lib/thumbnail-cache.ts
index 62af305..9db8e76 100644
--- a/lib/thumbnail-cache.ts
+++ b/lib/thumbnail-cache.ts
@@ -12,6 +12,26 @@ export interface CachedThumbnailSet {
let dbInstance: IDBDatabase | null = null;
+async function cleanupOldThumbnails(db: IDBDatabase): Promise<void> {
+ const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000;
+ const cutoff = Date.now() - SEVEN_DAYS_MS;
+ return new Promise((resolve) => {
+ try {
+ const transaction = db.transaction(STORE_NAME, "readwrite");
+ const store = transaction.objectStore(STORE_NAME);
+ const index = store.index("createdAt");
+ const range = IDBKeyRange.upperBound(cutoff);
+ const request = index.openCursor(range);
+ request.onsuccess = (event) => {
+ const cursor = (event.target as IDBRequest<IDBCursorWithValue | null>).result;
+ if (cursor) { cursor.delete(); cursor.continue(); }
+ };
+ transaction.oncomplete = () => resolve();
+ transaction.onerror = () => resolve();
+ } catch { resolve(); }
+ });
+}
+
async function openDB(): Promise<IDBDatabase> {
if (dbInstance) return dbInstance;
@@ -21,6 +41,7 @@ async function openDB(): Promise<IDBDatabase> {
request.onerror = () => reject(request.error);
request.onsuccess = () => {
dbInstance = request.result;
+ cleanupOldThumbnails(dbInstance).catch(() => {});
resolve(request.result);
};
@@ -53,7 +74,6 @@ export async function getCachedThumbnails(
request.onsuccess = () => {
const result = request.result;
if (result) {
- // Check if cache is not too old (7 days)
const maxAge = 7 * 24 * 60 * 60 * 1000;
if (Date.now() - result.createdAt < maxAge) {
resolve(result);
diff --git a/lib/video-upload-cache.ts b/lib/video-upload-cache.ts
index bec5b8c..ed99ac6 100644
--- a/lib/video-upload-cache.ts
+++ b/lib/video-upload-cache.ts
@@ -17,6 +17,26 @@ export interface CachedUploadedVideo {
let dbInstance: IDBDatabase | null = null;
+async function cleanupOldUploadCache(db: IDBDatabase): Promise<void> {
+ const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000;
+ const cutoff = Date.now() - SEVEN_DAYS_MS;
+ return new Promise((resolve) => {
+ try {
+ const transaction = db.transaction(STORE_NAME, "readwrite");
+ const store = transaction.objectStore(STORE_NAME);
+ const getReq = store.get(SINGLE_VIDEO_KEY);
+ getReq.onsuccess = () => {
+ const record = getReq.result as CachedUploadedVideo | undefined;
+ if (record && record.uploadedAt < cutoff) {
+ store.delete(SINGLE_VIDEO_KEY);
+ }
+ };
+ transaction.oncomplete = () => resolve();
+ transaction.onerror = () => resolve();
+ } catch { resolve(); }
+ });
+}
+
async function openDB(): Promise<IDBDatabase> {
if (dbInstance) return dbInstance;
@@ -26,6 +46,7 @@ async function openDB(): Promise<IDBDatabase> {
request.onerror = () => reject(request.error);
request.onsuccess = () => {
dbInstance = request.result;
+ cleanupOldUploadCache(dbInstance).catch(() => {});
resolve(request.result);
};
diff --git a/lib/videos-library.ts b/lib/videos-library.ts
index 524f49a..efde2ce 100644
--- a/lib/videos-library.ts
+++ b/lib/videos-library.ts
@@ -38,6 +38,26 @@ interface ExtendedVideoElement extends HTMLVideoElement {
let dbInstance: IDBDatabase | null = null;
+async function cleanupOldLibraryEntries(db: IDBDatabase): Promise<void> {
+ const SIXTY_DAYS_MS = 60 * 24 * 60 * 60 * 1000;
+ const cutoff = Date.now() - SIXTY_DAYS_MS;
+ return new Promise((resolve) => {
+ try {
+ const transaction = db.transaction(STORE_NAME, "readwrite");
+ const store = transaction.objectStore(STORE_NAME);
+ const index = store.index("uploadedAt");
+ const range = IDBKeyRange.upperBound(cutoff);
+ const request = index.openCursor(range);
+ request.onsuccess = (event) => {
+ const cursor = (event.target as IDBRequest<IDBCursorWithValue | null>).result;
+ if (cursor) { cursor.delete(); cursor.continue(); }
+ };
+ transaction.oncomplete = () => resolve();
+ transaction.onerror = () => resolve();
+ } catch { resolve(); }
+ });
+}
+
function generateVideoId(): string {
return `uploaded_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
}
@@ -51,6 +71,7 @@ async function openDB(): Promise<IDBDatabase> {
request.onerror = () => reject(request.error);
request.onsuccess = () => {
dbInstance = request.result;
+ cleanupOldLibraryEntries(dbInstance).catch(() => {});
resolve(request.result);
};