-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
6 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createWriteStream } from "node:fs" | ||
import { pipeline } from "node:stream" | ||
import { promisify } from "node:util" | ||
|
||
const streamPipeline = promisify(pipeline) | ||
|
||
export async function downloadFile(url: string, dest: string) { | ||
const res = await fetch(url) | ||
|
||
// 检查响应是否成功 | ||
if (!res.ok) { | ||
throw new Error(`Failed to fetch ${url}: ${res.statusText}`) | ||
} | ||
if (!res.body) { | ||
throw new Error(`Failed to get response body`) | ||
} | ||
await streamPipeline(res.body as any, createWriteStream(dest)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import type { BrowserWindow } from "electron" | ||
import type { toast } from "sonner" | ||
|
||
const PREFIX = "__follow" | ||
interface RenderGlobalContext { | ||
showSetting: () => void | ||
|
||
toast: typeof toast | ||
} | ||
|
||
export const registerGlobalContext = (context: RenderGlobalContext) => { | ||
globalThis[PREFIX] = context | ||
} | ||
export const callGlobalContextMethod = ( | ||
|
||
export function callGlobalContextMethod( | ||
window: BrowserWindow, | ||
method: string, | ||
args?: any[] | ||
): void | ||
|
||
export function callGlobalContextMethod<T extends keyof RenderGlobalContext>( | ||
window: BrowserWindow, | ||
method: keyof RenderGlobalContext, | ||
) => window.webContents.executeJavaScript(`globalThis.${PREFIX}.${method}()`) | ||
method: T, | ||
|
||
// @ts-expect-error | ||
args: Parameters<RenderGlobalContext[T]> = [] as any | ||
): void | ||
export function callGlobalContextMethod<T extends keyof RenderGlobalContext>( | ||
window: BrowserWindow, | ||
method: T, | ||
|
||
args: Parameters<RenderGlobalContext[T]> = [] as any, | ||
) { | ||
return window.webContents.executeJavaScript( | ||
`globalThis.${PREFIX}.${method}(${args | ||
.map((arg) => JSON.stringify(arg)) | ||
.join(",")})`, | ||
) | ||
} |