Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ interface Window {
saveExportedVideo: (
videoData: ArrayBuffer,
fileName: string,
) => Promise<{ success: boolean; path?: string; message?: string; canceled?: boolean }>;
exportFolder?: string,
) => Promise<{
success: boolean;
path?: string;
dir?: string;
message?: string;
canceled?: boolean;
}>;
openVideoFilePicker: () => Promise<{ success: boolean; path?: string; canceled?: boolean }>;
setCurrentVideoPath: (path: string) => Promise<{ success: boolean }>;
setCurrentRecordingSession: (
Expand Down
92 changes: 49 additions & 43 deletions electron/ipc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,54 +822,60 @@ export function registerIpcHandlers(
* @returns Object with success status, optional file path, and error details.
*/

ipcMain.handle("save-exported-video", async (_, videoData: ArrayBuffer, fileName: string) => {
try {
// Determine file type from extension
const isGif = fileName.toLowerCase().endsWith(".gif");
const filters = isGif
? [{ name: mainT("dialogs", "fileDialogs.gifImage"), extensions: ["gif"] }]
: [{ name: mainT("dialogs", "fileDialogs.mp4Video"), extensions: ["mp4"] }];

const result = await dialog.showSaveDialog({
title: isGif
? mainT("dialogs", "fileDialogs.saveGif")
: mainT("dialogs", "fileDialogs.saveVideo"),
defaultPath: path.join(app.getPath("downloads"), fileName),
filters,
properties: ["createDirectory", "showOverwriteConfirmation"],
});
ipcMain.handle(
"save-exported-video",
async (_, videoData: ArrayBuffer, fileName: string, exportFolder?: string) => {
try {
// Determine file type from extension
const isGif = fileName.toLowerCase().endsWith(".gif");
const filters = isGif
? [{ name: mainT("dialogs", "fileDialogs.gifImage"), extensions: ["gif"] }]
: [{ name: mainT("dialogs", "fileDialogs.mp4Video"), extensions: ["mp4"] }];

if (result.canceled || !result.filePath) {
return {
success: false,
canceled: true,
message: "Export canceled",
};
}
const baseFolder = exportFolder ?? app.getPath("downloads");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

?? won't protect against empty string exportFolder

nullish coalescing only catches null/undefined — an empty "" sneaks right through. path.join("", fileName) resolves to a relative path, so the save dialog would open at the process CWD instead of Downloads on that edge case.

The userPreferences.ts validation already guards against "" in storage, but the IPC boundary itself has no such check. Swapping to || closes the gap for zero cost.

🛡️ Proposed fix
- const baseFolder = exportFolder ?? app.getPath("downloads");
+ const baseFolder = exportFolder || app.getPath("downloads");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const baseFolder = exportFolder ?? app.getPath("downloads");
const baseFolder = exportFolder || app.getPath("downloads");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@electron/ipc/handlers.ts` at line 835, The assignment to baseFolder uses
nullish coalescing (const baseFolder = exportFolder ?? app.getPath("downloads"))
which doesn't guard against an empty string and can make path.join("", fileName)
resolve relative; change the check to treat empty string as falsy (e.g., use ||
or an explicit empty-string check) so that when exportFolder is "" the fallback
app.getPath("downloads") is used; update the line referencing exportFolder and
baseFolder (and any subsequent path.join usage) to rely on the corrected
baseFolder value.


// --- FIX: Normalize the path for Windows compatibility ---
const normalizedPath = path.normalize(result.filePath);
const result = await dialog.showSaveDialog({
title: isGif
? mainT("dialogs", "fileDialogs.saveGif")
: mainT("dialogs", "fileDialogs.saveVideo"),
defaultPath: path.join(baseFolder, fileName),
filters,
properties: ["createDirectory", "showOverwriteConfirmation"],
});

// Ensure the parent directory exists (Windows may fail if the folder is missing)
await fs.mkdir(path.dirname(normalizedPath), { recursive: true });
// --- END FIX ---
if (result.canceled || !result.filePath) {
return {
success: false,
canceled: true,
message: "Export canceled",
};
}

await fs.writeFile(normalizedPath, Buffer.from(videoData));
// --- FIX: Normalize the path for Windows compatibility ---
const normalizedPath = path.normalize(result.filePath);

return {
success: true,
path: normalizedPath,
message: "Video exported successfully",
};
} catch (error) {
console.error("Failed to save exported video:", error);
return {
success: false,
message: "Failed to save exported video",
error: String(error),
};
}
});
// Ensure the parent directory exists (Windows may fail if the folder is missing)
await fs.mkdir(path.dirname(normalizedPath), { recursive: true });
// --- END FIX ---

await fs.writeFile(normalizedPath, Buffer.from(videoData));

return {
success: true,
path: normalizedPath,
dir: path.dirname(normalizedPath),
message: "Video exported successfully",
};
} catch (error) {
console.error("Failed to save exported video:", error);
return {
success: false,
message: "Failed to save exported video",
error: String(error),
};
}
},
);
ipcMain.handle("open-video-file-picker", async () => {
try {
const result = await dialog.showOpenDialog({
Expand Down
4 changes: 2 additions & 2 deletions electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ contextBridge.exposeInMainWorld("electronAPI", {
openExternalUrl: (url: string) => {
return ipcRenderer.invoke("open-external-url", url);
},
saveExportedVideo: (videoData: ArrayBuffer, fileName: string) => {
return ipcRenderer.invoke("save-exported-video", videoData, fileName);
saveExportedVideo: (videoData: ArrayBuffer, fileName: string, exportFolder?: string) => {
return ipcRenderer.invoke("save-exported-video", videoData, fileName, exportFolder);
},
openVideoFilePicker: () => {
return ipcRenderer.invoke("open-video-file-picker");
Expand Down
25 changes: 20 additions & 5 deletions src/components/video-editor/VideoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export default function VideoEditor() {
const [showNewRecordingDialog, setShowNewRecordingDialog] = useState(false);
const [exportQuality, setExportQuality] = useState<ExportQuality>("good");
const [exportFormat, setExportFormat] = useState<ExportFormat>("mp4");
const [exportFolder, setExportFolder] = useState<string | undefined>(undefined);
const [gifFrameRate, setGifFrameRate] = useState<GifFrameRate>(15);
const [gifLoop, setGifLoop] = useState(true);
const [gifSizePreset, setGifSizePreset] = useState<GifSizePreset>("medium");
Expand Down Expand Up @@ -409,14 +410,15 @@ export default function VideoEditor() {
});
setExportQuality(prefs.exportQuality);
setExportFormat(prefs.exportFormat);
setExportFolder(prefs.exportFolder);
setPrefsHydrated(true);
}, [updateState]);

// Auto-save user preferences when settings change
useEffect(() => {
if (!prefsHydrated) return;
saveUserPreferences({ padding, aspectRatio, exportQuality, exportFormat });
}, [prefsHydrated, padding, aspectRatio, exportQuality, exportFormat]);
saveUserPreferences({ padding, aspectRatio, exportQuality, exportFormat, exportFolder });
}, [prefsHydrated, padding, aspectRatio, exportQuality, exportFormat, exportFolder]);

const saveProject = useCallback(
async (forceSaveAs: boolean) => {
Expand Down Expand Up @@ -1309,10 +1311,12 @@ export default function VideoEditor() {
const saveResult = await window.electronAPI.saveExportedVideo(
unsavedExport.arrayBuffer,
unsavedExport.fileName,
exportFolder,
);
if (saveResult.canceled) {
toast.info("Export canceled");
} else if (saveResult.success && saveResult.path) {
if (saveResult.dir) setExportFolder(saveResult.dir);
setUnsavedExport(null);
handleExportSaved(unsavedExport.format === "gif" ? "GIF" : "Video", saveResult.path);
} else {
Expand All @@ -1322,7 +1326,7 @@ export default function VideoEditor() {
console.error("Error saving unsaved export:", error);
toast.error("Failed to save exported video");
}
}, [unsavedExport, handleExportSaved]);
}, [unsavedExport, exportFolder, handleExportSaved]);

const handleExport = useCallback(
async (settings: ExportSettings) => {
Expand Down Expand Up @@ -1410,12 +1414,17 @@ export default function VideoEditor() {
}
}

const saveResult = await window.electronAPI.saveExportedVideo(arrayBuffer, fileName);
const saveResult = await window.electronAPI.saveExportedVideo(
arrayBuffer,
fileName,
exportFolder,
);

if (saveResult.canceled) {
setUnsavedExport({ arrayBuffer, fileName, format: "gif" });
toast.info("Export canceled");
} else if (saveResult.success && saveResult.path) {
if (saveResult.dir) setExportFolder(saveResult.dir);
setUnsavedExport(null);
handleExportSaved("GIF", saveResult.path);
} else {
Expand Down Expand Up @@ -1550,12 +1559,17 @@ export default function VideoEditor() {
}
}

const saveResult = await window.electronAPI.saveExportedVideo(arrayBuffer, fileName);
const saveResult = await window.electronAPI.saveExportedVideo(
arrayBuffer,
fileName,
exportFolder,
);

if (saveResult.canceled) {
setUnsavedExport({ arrayBuffer, fileName, format: "mp4" });
toast.info("Export canceled");
} else if (saveResult.success && saveResult.path) {
if (saveResult.dir) setExportFolder(saveResult.dir);
setUnsavedExport(null);
handleExportSaved("Video", saveResult.path);
} else {
Expand Down Expand Up @@ -1612,6 +1626,7 @@ export default function VideoEditor() {
webcamSizePreset,
webcamPosition,
exportQuality,
exportFolder,
handleExportSaved,
cursorTelemetry,
t,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/userPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface UserPreferences {
exportQuality: ExportQuality;
/** Default export format */
exportFormat: ExportFormat;
/** Last folder used when saving an export */
exportFolder?: string;
}

const DEFAULT_PREFS: UserPreferences = {
Expand Down Expand Up @@ -76,6 +78,10 @@ export function loadUserPreferences(): UserPreferences {
raw.exportFormat === "gif" || raw.exportFormat === "mp4"
? (raw.exportFormat as ExportFormat)
: DEFAULT_PREFS.exportFormat,
exportFolder:
typeof raw.exportFolder === "string" && raw.exportFolder.length > 0
? raw.exportFolder
: undefined,
};
}

Expand Down
Loading