| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts"; |
| 5 | import * as ElectronWindow from "../../electron/ElectronWindow.ts"; |
| 6 | import { getDesktopUrl } from "../../electron/ElectronProtocol.ts"; |
| 7 | import * as DesktopVideo from "../../media/DesktopVideo.ts"; |
| 8 | import * as DesktopIpc from "../DesktopIpc.ts"; |
| 9 | import * as IpcChannels from "../channels.ts"; |
| 10 | |
| 11 | export const openVideo = DesktopIpc.makeIpcMethod({ |
| 12 | channel: IpcChannels.OPEN_VIDEO_CHANNEL, |
| 13 | payload: Schema.Struct({ |
| 14 | url: Schema.String.check(Schema.isMaxLength(16_384)), |
| 15 | name: Schema.String.check(Schema.isMaxLength(255)), |
| 16 | }), |
| 17 | result: Schema.Void, |
| 18 | handler: Effect.fn("desktop.ipc.media.openVideo")(function* (input, event) { |
| 19 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 20 | const main = yield* (yield* ElectronWindow.ElectronWindow).main; |
| 21 | const expected = new URL(getDesktopUrl(environment.isDevelopment)); |
| 22 | const frame = event?.senderFrame; |
| 23 | const source = frame && URL.canParse(frame.url) ? new URL(frame.url) : null; |
| 24 | if ( |
| 25 | event === undefined || |
| 26 | Option.isNone(main) || |
| 27 | main.value.isDestroyed() || |
| 28 | main.value.webContents.id !== event.sender.id || |
| 29 | frame?.parent !== null || |
| 30 | source?.protocol !== expected.protocol || |
| 31 | source.host !== expected.host |
| 32 | ) |
| 33 | return yield* new DesktopVideo.DesktopVideoError({ stage: "source" }); |
| 34 | |
| 35 | return yield* (yield* DesktopVideo.DesktopVideo).open(input); |
| 36 | }), |
| 37 | }); |