Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web api): wire desktop uploads to Storage and quota invalidation

Richie McIlroy276b432528ad
diff --git a/apps/web/app/api/desktop/[...route]/route.ts b/apps/web/app/api/desktop/[...route]/route.ts
index 9255bc7eb..368572000 100644
--- a/apps/web/app/api/desktop/[...route]/route.ts
+++ b/apps/web/app/api/desktop/[...route]/route.ts
@@ -6,6 +6,7 @@ import { corsMiddleware } from "../../utils";
import * as root from "./root";
import * as s3Config from "./s3Config";
import * as session from "./session";
+import * as storage from "./storage";
import * as video from "./video";
const app = new Hono()
@@ -13,6 +14,7 @@ const app = new Hono()
.use(corsMiddleware)
.route("/s3/config", s3Config.app)
.route("/session", session.app)
+ .route("/storage", storage.app)
.route("/video", video.app)
.route("/", root.app);
diff --git a/apps/web/app/api/desktop/[...route]/video.ts b/apps/web/app/api/desktop/[...route]/video.ts
index 5a159aa09..b5dc93773 100644
--- a/apps/web/app/api/desktop/[...route]/video.ts
+++ b/apps/web/app/api/desktop/[...route]/video.ts
@@ -6,21 +6,22 @@ import {
importedVideos,
organizationMembers,
organizations,
- s3Buckets,
users,
videos,
videoUploads,
} from "@cap/database/schema";
import { buildEnv, NODE_ENV, serverEnv } from "@cap/env";
import { dub, userIsPro } from "@cap/utils";
-import { S3Buckets } from "@cap/web-backend";
+import { Storage } from "@cap/web-backend";
import { Organisation, Video } from "@cap/web-domain";
import { zValidator } from "@hono/zod-validator";
import { and, count, eq, lte, or } from "drizzle-orm";
import { Effect, Option } from "effect";
import { Hono } from "hono";
import { z } from "zod";
+import { invalidateGoogleDriveStorageQuotaCache } from "@/lib/google-drive-storage-quota";
import { runPromise } from "@/lib/server";
+import { decodeStorageVideo } from "@/lib/video-storage";
import { isFromDesktopSemver, UPLOAD_PROGRESS_VERSION } from "@/utils/desktop";
import { stringOrNumberOptional } from "@/utils/zod";
import { withAuth } from "../../utils";
@@ -85,11 +86,6 @@ app.get(
fps,
});
- const [customBucket] = await db()
- .select()
- .from(s3Buckets)
- .where(eq(s3Buckets.ownerId, user.id));
-
const date = new Date();
const formattedDate = `${date.getDate()} ${date.toLocaleString(
"default",
@@ -169,6 +165,9 @@ app.get(
const videoName =
name ??
`Cap ${isScreenshot ? "Screenshot" : "Recording"} - ${formattedDate}`;
+ const writable = await Storage.getWritableAccessForUser(user.id).pipe(
+ runPromise,
+ );
await db()
.insert(videos)
@@ -186,7 +185,8 @@ app.get(
? { type: "desktopSegments" as const }
: undefined,
isScreenshot,
- bucket: customBucket?.id,
+ bucket: Option.getOrNull(writable.bucketId),
+ storageIntegrationId: Option.getOrNull(writable.storageIntegrationId),
public: serverEnv().CAP_VIDEOS_DEFAULT_PUBLIC,
duration: durationInSecs,
width,
@@ -273,9 +273,8 @@ app.delete(
try {
const [result] = await db()
- .select({ video: videos, bucket: s3Buckets })
+ .select({ video: videos })
.from(videos)
- .leftJoin(s3Buckets, eq(videos.bucket, s3Buckets.id))
.where(and(eq(videos.id, videoId), eq(videos.ownerId, user.id)));
if (!result)
@@ -292,9 +291,8 @@ app.delete(
.where(and(eq(videos.id, videoId), eq(videos.ownerId, user.id)));
await Effect.gen(function* () {
- const [bucket] = yield* S3Buckets.getBucketAccess(
- Option.fromNullable(result.bucket?.id),
- );
+ const video = decodeStorageVideo(result.video);
+ const [bucket] = yield* Storage.getAccessForVideo(video);
const listedObjects = yield* bucket.listObjects({
prefix: `${user.id}/${videoId}/`,
@@ -302,11 +300,14 @@ app.delete(
if (listedObjects.Contents)
yield* bucket.deleteObjects(
- listedObjects.Contents.map((content: any) => ({
+ listedObjects.Contents.map((content) => ({
Key: content.Key,
})),
);
}).pipe(runPromise);
+ await invalidateGoogleDriveStorageQuotaCache(
+ result.video.storageIntegrationId,
+ );
return c.json(true);
} catch (error) {
@@ -344,7 +345,11 @@ app.post(
try {
const [video] = await db()
- .select({ id: videos.id, upload: videoUploads })
+ .select({
+ id: videos.id,
+ storageIntegrationId: videos.storageIntegrationId,
+ upload: videoUploads,
+ })
.from(videos)
.where(and(eq(videos.id, videoId), eq(videos.ownerId, user.id)))
.leftJoin(videoUploads, eq(videos.id, videoUploads.videoId));
@@ -382,6 +387,11 @@ app.post(
updatedAt,
});
}
+ if (uploaded === total) {
+ await invalidateGoogleDriveStorageQuotaCache(
+ video.storageIntegrationId,
+ );
+ }
return c.json(true);
} catch (error) {