The VS Code of video editing, built for agents.
TypeScript
Enhance file handling and permissions management in desktop and web applications
konstantin-paulus848433024126
diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.tsindex dd975e05..688361ec 100644--- a/apps/desktop/src/preload.ts+++ b/apps/desktop/src/preload.ts@@ -2,11 +2,12 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-import { contextBridge, ipcRenderer } from "electron";-import type { IpcRendererEvent } from "electron";+import { contextBridge, ipcRenderer, webUtils } from "electron";import { MAIN_WIRE } from "./main-channels";import { CLI_WIRE } from "@diffusionstudio/cli/channels";+import type { IpcRendererEvent } from "electron";+const ALLOWED_RENDERER_TO_MAIN: ReadonlySet<string> = new Set([MAIN_WIRE.REQUEST]);const ALLOWED_MAIN_TO_RENDERER: ReadonlySet<string> = new Set([@@ -16,11 +17,16 @@ const ALLOWED_MAIN_TO_RENDERER: ReadonlySet<string> = new Set([]);contextBridge.exposeInMainWorld("desktop", {+ getPathForFile,platform: process.platform,send,on,});+function getPathForFile(file: File): string {+ return webUtils.getPathForFile(file);+}+function send(channel: string, payload: unknown): void {if (!ALLOWED_RENDERER_TO_MAIN.has(channel)) return;ipcRenderer.send(channel, payload);diff --git a/apps/web/src/components/engine/api/assets.ts b/apps/web/src/components/engine/api/assets.tsindex edcf43c7..ca3b4169 100644--- a/apps/web/src/components/engine/api/assets.ts+++ b/apps/web/src/components/engine/api/assets.ts@@ -18,7 +18,7 @@ import { createEntity } from "./entities";import { appendChild } from "./hierarchy";import { addComponent, setComponent } from "./events";import { resizeEntity } from "./resize";-import { mimeTypeToExtension } from "@/utils";+import { mimeTypeToExtension, showFileDialog } from "@/utils";import { trpc } from "@/lib/trpc";import {ElectronFileHandle,@@ -796,19 +796,25 @@ export async function insertAssetInTimeline(world: EngineWorld, asset: Asset, moexport async function replaceAssetHandle(world: EngineWorld, asset: Asset) {const { mimeType } = asset;- let handle: FileSystemFileHandle | undefined;- try {- [handle] = await window.showOpenFilePicker({- multiple: false,- types: [- {- description: `${mimeType} files`,- accept: { [mimeType]: [mimeTypeToExtension(mimeType)] },- },- ],- });- } catch {- return;+ let handle: FileSystemFileHandle | ElectronFileHandle | undefined;++ if (window.desktop) {+ const [picked] = await showFileDialog(mimeType, false);+ handle = picked as FileSystemFileHandle | ElectronFileHandle;+ } else {+ try {+ [handle] = await window.showOpenFilePicker({+ multiple: false,+ types: [+ {+ description: `${mimeType} files`,+ accept: { [mimeType]: [mimeTypeToExtension(mimeType)] },+ },+ ],+ });+ } catch {+ return;+ }}if (!handle) return;@@ -827,19 +833,25 @@ export async function replaceAssetHandle(world: EngineWorld, asset: Asset) {}};-type FileOrHandle = File | FileSystemFileHandle | FileSystemDirectoryHandle+type FileOrHandle = File | FileSystemFileHandle | FileSystemDirectoryHandle | ElectronFileHandleexport async function getFileFromDataTransferItem(item?: DataTransferItem): Promise<FileOrHandle | null> {if (!item) return null;- if (typeof item.getAsFileSystemHandle === 'function') {- const handle = await item.getAsFileSystemHandle();- if (handle && (handle.kind === 'file' || handle.kind === 'directory')) {- return handle as FileOrHandle;- }+ const file = item.getAsFile();+ if (typeof item.getAsFileSystemHandle !== 'function') return file;+ const handle = await item.getAsFileSystemHandle();++ if (!handle || (handle.kind !== 'file' && handle.kind !== 'directory')) {return null;}- return item.getAsFile();++ if (window.desktop && handle.kind === 'file' && file) {+ const path = window.desktop.getPathForFile(file);+ if (path) return ElectronFileHandle.fromFile(path, file);+ }++ return handle as FileOrHandle;}diff --git a/apps/web/src/index.tsx b/apps/web/src/index.tsxindex 74ae7815..1521b93b 100644--- a/apps/web/src/index.tsx+++ b/apps/web/src/index.tsx@@ -23,8 +23,12 @@ document.addEventListener('contextmenu', (e) => e.preventDefault())if (window.desktop) {const origRequest = FileSystemHandle.prototype.requestPermission;- FileSystemHandle.prototype.queryPermission = function (desc) {- return origRequest.call(this, desc);+ FileSystemHandle.prototype.queryPermission = async function (desc) {+ try {+ return await origRequest.call(this, desc);+ } catch {+ return 'prompt';+ }};restoreLastRoute();diff --git a/apps/web/src/lib/electron-file-handle.ts b/apps/web/src/lib/electron-file-handle.tsindex af73fc0e..b5fb5c25 100644--- a/apps/web/src/lib/electron-file-handle.ts+++ b/apps/web/src/lib/electron-file-handle.ts@@ -67,12 +67,18 @@ export class ElectronFileHandle {private filePromise: Promise<File> | null = null;- constructor(path: string, name?: string) {+ public constructor(path: string, name?: string) {this.path = path;this.name = name ?? basename(path);}- async getFile(): Promise<File> {+ public static fromFile(path: string, file: File): ElectronFileHandle {+ const handle = new ElectronFileHandle(path, file.name);+ handle.filePromise = Promise.resolve(file);+ return handle;+ }++ public async getFile(): Promise<File> {if (!this.filePromise) {// Serialize CDP round-trips: every call shares the same hidden input,// and overlapping setFiles() calls would race on `input.files[0]`.@@ -84,28 +90,26 @@ export class ElectronFileHandle {return this.filePromise;}- invalidate() {+ public invalidate() {this.filePromise = null;}// FileSystemFileHandle shape — ElectronFileHandle is always "granted" because// the main process has full disk access.- async queryPermission(_descriptor?: any): Promise<PermissionState> {+ public async queryPermission(_descriptor?: any): Promise<PermissionState> {return "granted";}- async requestPermission(_descriptor?: any): Promise<PermissionState> {+ public async requestPermission(_descriptor?: any): Promise<PermissionState> {return "granted";}- toJSON(): SerializedElectronFileHandle {+ public toJSON(): SerializedElectronFileHandle {return { kind: "electron-file", path: this.path, name: this.name };}}-export function isSerializedElectronFileHandle(- value: unknown-): value is SerializedElectronFileHandle {+export function isSerializedElectronFileHandle(value: unknown): value is SerializedElectronFileHandle {return (typeof value === "object" &&value !== null &&diff --git a/apps/web/src/utils/browser.ts b/apps/web/src/utils/browser.tsindex 133873e9..4429a312 100644--- a/apps/web/src/utils/browser.ts+++ b/apps/web/src/utils/browser.ts@@ -4,8 +4,7 @@import { mimeTypeToExtension } from "./media";import { colord } from "colord";--import type { ElectronFileHandle } from "@/lib/electron-file-handle";+import { ElectronFileHandle } from "@/lib/electron-file-handle";/*** Reads the computed value of a CSS custom property from the document root.@@ -87,16 +86,16 @@ export async function verifyHandlePermissions(export async function showFileDialog(accept = 'image/*,video/*,audio/*,text/*',multiple = true-): Promise<File[] | FileSystemFileHandle[]> {+): Promise<(File | FileSystemFileHandle | ElectronFileHandle)[]> {try {- if ('showOpenFilePicker' in window) {+ if (!window.desktop && 'showOpenFilePicker' in window) {return await window.showOpenFilePicker({multiple,startIn: 'downloads',});}- return new Promise<File[]>(resolve => {+ return new Promise<(File | ElectronFileHandle)[]>(resolve => {// setup inputconst input = document.createElement('input');input.type = 'file';@@ -105,8 +104,12 @@ export async function showFileDialog(// listen for changesinput.onchange = (fileEvent: Event) => {const files = Array.from((fileEvent.target as HTMLInputElement)?.files ?? []);- resolve(files);+ resolve(files.map((file) => {+ const path = window.desktop?.getPathForFile(file);+ return path ? ElectronFileHandle.fromFile(path, file) : file;+ }));};+ input.oncancel = () => resolve([]);input.click();});} catch {diff --git a/apps/web/src/vite-env.d.ts b/apps/web/src/vite-env.d.tsindex d8755d9e..71827199 100644--- a/apps/web/src/vite-env.d.ts+++ b/apps/web/src/vite-env.d.ts@@ -16,6 +16,7 @@ declare global {platform: string;send(channel: string, payload: unknown): void;on(channel: string, cb: (payload: unknown) => void): (() => void);+ getPathForFile(file: File): string;};showOpenFilePicker(options?: OpenFilePickerOptions): Promise<FileSystemFileHandle[]>;EyeDropper?: {