Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: new CORS for desktop app

Richie McIlroy518b353737a4
diff --git a/apps/desktop/src/app/page.tsx b/apps/desktop/src/app/page.tsx
index 405c073e3..81f4bd190 100644
--- a/apps/desktop/src/app/page.tsx
+++ b/apps/desktop/src/app/page.tsx
@@ -1,7 +1,6 @@
"use client";
import React, { useState, useEffect } from "react";
-import { getCookie } from "cookies-next";
import { SignIn } from "@/components/windows/inner/SignIn";
import { Recorder } from "@/components/windows/inner/Recorder";
import { WindowActions } from "@/components/WindowActions";
@@ -13,23 +12,20 @@ export default function CameraPage() {
const [loading, setLoading] = useState(true);
useEffect(() => {
- const checkSignInStatus = async () => {
- const cookie = getCookie("next-auth.session-token");
- const signedIn = !!cookie;
-
- if (signedIn !== isSignedIn) {
- setIsSignedIn(signedIn);
- }
- if (loading) {
- setLoading(false);
+ const checkSession = setInterval(() => {
+ const session = localStorage.getItem("session");
+ if (session) {
+ const { token, expires } = JSON.parse(session);
+ if (token && new Date(expires * 1000) > new Date()) {
+ setIsSignedIn(true);
+ clearInterval(checkSession);
+ }
}
- };
-
- checkSignInStatus();
- const interval = setInterval(checkSignInStatus, 1000);
+ setLoading(false);
+ }, 1000);
- return () => clearInterval(interval);
- }, [isSignedIn, loading]);
+ return () => clearInterval(checkSession);
+ }, []);
useEffect(() => {
if (isSignedIn && !cameraWindowOpen) {
@@ -85,8 +81,14 @@ export default function CameraPage() {
return (
<div id="app" data-tauri-drag-region style={{ borderRadius: "16px" }}>
- <WindowActions />
- {isSignedIn ? <Recorder /> : <SignIn />}
+ {isSignedIn ? (
+ <>
+ <WindowActions />
+ <Recorder />
+ </>
+ ) : (
+ <SignIn />
+ )}
</div>
);
}
diff --git a/apps/desktop/src/components/windows/Camera.tsx b/apps/desktop/src/components/windows/Camera.tsx
index 439e22b25..0a278d85e 100644
--- a/apps/desktop/src/components/windows/Camera.tsx
+++ b/apps/desktop/src/components/windows/Camera.tsx
@@ -214,7 +214,7 @@ export const Camera = () => {
onClick={async () => {
await setWindowSize("sm");
}}
- className="w-2 h-2 bg-gray-200 rounded-full"
+ className="w-2 h-2 m-0 p-0 bg-gray-200 rounded-full"
></button>
</div>
<div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
diff --git a/apps/desktop/src/utils/auth/index.ts b/apps/desktop/src/utils/auth/index.ts
index a7d93b777..4754645e9 100644
--- a/apps/desktop/src/utils/auth/index.ts
+++ b/apps/desktop/src/utils/auth/index.ts
@@ -1,6 +1,7 @@
"use client";
import callbackTemplate from "./callback.template";
+import { setCookie } from "cookies-next";
const dynamicImports = {
invoke: () => import("@tauri-apps/api").then(({ invoke }) => invoke),
@@ -37,16 +38,19 @@ export const login = () => {
const token = urlObject.searchParams.get("token");
const expires = urlObject.searchParams.get("expires");
- console.log("token", token);
-
if (!token || !expires) {
+ console.error("Missing token or expires");
return;
}
- const expiresDate = new Date(parseInt(expires) * 1000);
-
- if (typeof document !== "undefined") {
- document.cookie = `next-auth.session-token=${token}; expires=${expiresDate.toUTCString()}; path=/`;
+ try {
+ localStorage.setItem(
+ "session",
+ JSON.stringify({ token: token, expires: expires })
+ );
+ console.log("Setting localstorage");
+ } catch (error) {
+ console.error("Error setting item in localStorage", error);
}
});
})
diff --git a/apps/desktop/src/utils/recording/useMediaRecorder.ts b/apps/desktop/src/utils/recording/useMediaRecorder.ts
deleted file mode 100644
index 4fc18e990..000000000
--- a/apps/desktop/src/utils/recording/useMediaRecorder.ts
+++ /dev/null
@@ -1,308 +0,0 @@
-"use client";
-
-import { useState, useRef, useCallback, useEffect } from "react";
-import { FFmpeg as FfmpegType } from "@ffmpeg/ffmpeg";
-import { fetchFile } from "@ffmpeg/util";
-import {
- writeBinaryFile,
- writeTextFile,
- readTextFile,
- createDir,
-} from "@tauri-apps/api/fs";
-import { appDataDir, join } from "@tauri-apps/api/path";
-import { getLatestVideoId } from "../database/utils";
-
-export const useMediaRecorder = () => {
- const isRecording = useRef(false);
- const mediaRecorder = useRef<MediaRecorder | null>(null);
- const [ffmpegLoaded, setFfmpegLoaded] = useState(false);
- const ffmpegRef = useRef<FfmpegType | null>(null);
- const queue = useRef<Blob[]>([]);
- const processingQueue = useRef(false);
- const segmentCounter = useRef(0);
- const [metadataRecorded, setMetadataRecorded] = useState(false);
-
- useEffect(() => {
- const loadFfmpeg = async () => {
- const { FFmpeg } = await import("@ffmpeg/ffmpeg");
- ffmpegRef.current = new FFmpeg();
- setFfmpegLoaded(true);
- };
-
- if (!ffmpegLoaded && !ffmpegRef.current) {
- loadFfmpeg();
- }
- }, [ffmpegLoaded]);
-
- const getVideoChunksDir = async () => {
- const dir = await appDataDir();
- return join(dir, "chunks", "video");
- };
-
- const getAudioChunksDir = async () => {
- const dir = await appDataDir();
- return join(dir, "chunks", "audio");
- };
-
- const appendToSegmentListFile = async (fileName: string) => {
- const dir = await getAudioChunksDir();
- const segmentListPath = await join(dir, "segment_list.txt");
-
- let fileContent;
- try {
- fileContent = await readTextFile(segmentListPath);
- } catch (error) {
- fileContent = "";
- }
-
- const newContent = `${fileContent}${fileName}\n`;
- await writeTextFile(segmentListPath, newContent);
- };
-
- const saveFile = async (
- data: Uint8Array,
- fileName: string,
- screenshot: boolean
- ) => {
- try {
- const dir =
- screenshot === false ? await getVideoChunksDir() : await appDataDir();
- await createDir(dir, { recursive: true });
- const filePath = await join(dir, fileName);
- await writeBinaryFile(filePath, data);
- if (screenshot === false) {
- await appendToSegmentListFile(fileName);
- }
- } catch (error) {
- console.error("Failed to save file:", error);
- }
- };
-
- const processQueue = useCallback(async () => {
- if (
- ffmpegRef.current &&
- !processingQueue.current &&
- queue.current.length > 0
- ) {
- processingQueue.current = true;
-
- if (ffmpegRef.current) {
- try {
- await ffmpegRef.current.load();
- } catch (error) {
- console.error("Failed to load FFmpeg:", error);
- return;
- }
- }
-
- while (queue.current.length > 0) {
- try {
- const chunk = queue.current.shift();
-
- if (!chunk) {
- continue;
- }
-
- console.log("log-1");
-
- const chunkNumber = segmentCounter.current++;
-
- const ffmpeg = ffmpegRef.current;
-
- if (!ffmpeg) {
- console.error("FFmpeg not loaded");
- return;
- }
-
- const inputFileName = `input-${String(chunkNumber).padStart(
- 3,
- "0"
- )}.mp4`;
- const audioOutputFileName = `output-${String(chunkNumber).padStart(
- 3,
- "0"
- )}.aac`;
-
- const buffer = new Uint8Array(await chunk.arrayBuffer());
- const videoBuffer = await fetchFile(new Blob([buffer]));
-
- console.log("video buffer:");
- console.log(videoBuffer);
-
- console.log("log-2");
-
- await ffmpeg.writeFile(inputFileName, videoBuffer);
-
- console.log("log-3");
-
- await ffmpeg.exec([
- "-i",
- inputFileName,
- "-vn",
- "-acodec",
- "aac",
- "-b:a",
- "128k",
- audioOutputFileName,
- ]);
-
- console.log("log-4");
-
- try {
- const savedAudio = (await ffmpeg.readFile(
- audioOutputFileName
- )) as Uint8Array;
- const audioDir = await getAudioChunksDir();
- await createDir(audioDir, { recursive: true });
- const audioPath = await join(audioDir, audioOutputFileName);
- await writeBinaryFile(audioPath, savedAudio);
- await appendToSegmentListFile(audioOutputFileName);
- console.log("Audio saved: ", audioOutputFileName);
- } catch (error) {
- console.error("Failed to save audio:", error);
- }
-
- await ffmpeg.deleteFile(inputFileName);
- } catch (error) {
- console.error("Failed to process chunk:", error);
- }
- }
-
- processingQueue.current = false;
- }
- }, [queue]);
-
- const sendMetadataAPI = async () => {
- try {
- const videoId = await getLatestVideoId();
- const audioStartTime = Date.now();
- const queryParams = new URLSearchParams({
- videoId,
- audioStartTime: audioStartTime.toString(),
- }).toString();
- const res = await fetch(
- `${process.env.NEXT_PUBLIC_URL}/api/desktop/video/metadata/create?${queryParams}`,
- {
- method: "GET",
- credentials: "include",
- cache: "no-store",
- }
- );
- if (res.status === 401) {
- console.error("Unauthorized");
- return;
- }
- const data = await res.json();
- console.log("Metadata fetched successfully:", data);
- } catch (error) {
- console.error("Error fetching video and audio start time:", error);
- }
- };
-
- const startMediaRecording = useCallback(
- async (stream: MediaStream) => {
- if (!stream.active || !stream.getTracks().length) {
- console.error("The provided stream is not active or has no tracks.");
- return;
- }
-
- console.log("stream:");
- console.log(stream);
-
- console.log("stream.getTracks():");
- console.log(stream.getTracks());
-
- const initRecorder = async () => {
- if (
- mediaRecorder.current &&
- mediaRecorder.current.state !== "inactive"
- ) {
- mediaRecorder.current.stop();
- }
-
- if (isRecording.current === false) {
- return;
- }
-
- const options = {
- mimeType: "video/mp4",
- audioBitsPerSecond: 128000,
- videoBitsPerSecond: 0,
- };
- mediaRecorder.current = new MediaRecorder(stream, options);
-
- mediaRecorder.current.ondataavailable = async (event) => {
- console.log(`Blob size: ${event.data.size}`);
- if (event.data.size > 0) {
- queue.current.push(event.data);
- processQueue();
- }
- };
-
- mediaRecorder.current.onerror = (event: any) => {
- console.error("MediaRecorder error:", event.error);
- };
-
- mediaRecorder.current.start();
- };
-
- isRecording.current = true;
- initRecorder();
-
- const requestDataInterval = setInterval(() => {
- if (mediaRecorder.current && isRecording.current === true) {
- console.log("Stopping and restarting the recorder to gather chunks.");
- mediaRecorder.current.stop();
- mediaRecorder.current.onstop = () => {
- initRecorder();
- };
- } else {
- clearInterval(requestDataInterval);
- }
- }, 3000);
-
- mediaRecorder.current.onstart = async () => {
- if (metadataRecorded === false) {
- setMetadataRecorded(true);
- await sendMetadataAPI();
- }
- console.log("MediaRecorder started:", mediaRecorder.current);
- };
-
- return () => {
- clearInterval(requestDataInterval);
- if (
- mediaRecorder.current &&
- mediaRecorder.current.state !== "inactive"
- ) {
- mediaRecorder.current.stop();
- isRecording.current = false;
- }
- console.log("Recording stopped");
- };
- },
- [processQueue]
- );
-
- const stopMediaRecording = useCallback(async () => {
- if (mediaRecorder.current) {
- mediaRecorder.current.stop();
- console.log("Media recording stopped.");
- }
-
- isRecording.current = false;
- setMetadataRecorded(false);
-
- while (processingQueue.current || queue.current.length > 0) {
- await new Promise((resolve) => setTimeout(resolve, 100));
- }
-
- console.log("Queue processed and recording fully stopped.");
- }, []);
-
- return {
- isRecording: isRecording.current,
- startMediaRecording,
- stopMediaRecording,
- };
-};
diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs
index 1df7462e6..c5625a80c 100644
--- a/apps/web/next.config.mjs
+++ b/apps/web/next.config.mjs
@@ -76,6 +76,27 @@ const nextConfig = {
},
],
},
+ {
+ source: "/api/desktop/(.*)",
+ headers: [
+ {
+ key: "Access-Control-Allow-Origin",
+ value: "tauri://localhost",
+ },
+ {
+ key: "Access-Control-Allow-Methods",
+ value: "GET, POST, PUT, DELETE, OPTIONS",
+ },
+ {
+ key: "Access-Control-Allow-Headers",
+ value: "*",
+ },
+ {
+ key: "Access-Control-Allow-Credentials",
+ value: "true",
+ },
+ ],
+ },
{
source: "/api/desktop/(.*)",
headers: [