Skip to content

Let's use AI to Earn!

feat: 请求器添加图片流下载,获取文件方法添加下载

gaoSirc9e00d4b6d98
diff --git a/electron/plat/requestNet.ts b/electron/plat/requestNet.ts
index e482b66f..ea8c695c 100644
--- a/electron/plat/requestNet.ts
+++ b/electron/plat/requestNet.ts
@@ -19,6 +19,9 @@ export interface IRequestNetParams {
url?: string;
body?: any;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
+ // 是否请求文件
+ isReqFile?: boolean;
+ // body是否为文件
isFile?: boolean;
formData?: FormData;
}
@@ -30,6 +33,7 @@ const requestNet = <T = any>({
url,
isFile,
formData,
+ isReqFile,
}: IRequestNetParams): Promise<IRequestNetResult<T>> => {
return new Promise((resolve, reject) => {
const req = net.request({
@@ -54,18 +58,28 @@ const requestNet = <T = any>({
// 处理响应
req.on('response', (response) => {
+ const chunks: Buffer<ArrayBufferLike>[] = [];
let data = '';
response.on('data', (chunk) => {
- data += chunk;
+ if (isReqFile) {
+ chunks.push(chunk);
+ } else {
+ data += chunk;
+ }
});
response.on('end', () => {
let parsedData: T;
- try {
- parsedData = JSON.parse(data);
- } catch (e) {
- parsedData = undefined as any;
- console.log(e);
+ if (isReqFile) {
+ const buffer = Buffer.concat(chunks);
+ parsedData = buffer as any;
+ } else {
+ try {
+ parsedData = JSON.parse(data);
+ } catch (e) {
+ parsedData = undefined as any;
+ console.log(e);
+ }
}
resolve({
status: response.statusCode,
diff --git a/electron/plat/utils/index.ts b/electron/plat/utils/index.ts
index 5e450738..c394c803 100644
--- a/electron/plat/utils/index.ts
+++ b/electron/plat/utils/index.ts
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import sharp from 'sharp';
import * as path from 'path';
+import requestNet from '../requestNet';
/**
* 获取文件内容
@@ -8,13 +9,20 @@ import * as path from 'path';
*/
export async function getFileContent(filePath: string): Promise<Buffer> {
try {
- // 确保路径是绝对路径
- const absolutePath = path.resolve(filePath);
- console.log('Reading file:', absolutePath);
+ if (filePath.includes('https://') || filePath.includes('http://')) {
+ const res = await requestNet({
+ url: filePath,
+ isReqFile: true,
+ });
+ return res.data;
+ } else {
+ // 确保路径是绝对路径
+ const absolutePath = path.resolve(filePath);
+ console.log('Reading file:', absolutePath);
- // 读取文件内容
- const fileContent = await fs.promises.readFile(absolutePath);
- return fileContent;
+ // 读取文件内容
+ return await fs.promises.readFile(absolutePath);
+ }
} catch (error) {
console.error('Failed to read file:', error);
throw new Error(`读取文件失败: filePath`);
diff --git a/src/views/publish/components/CommonComponents/DouyinCommonComponents.tsx b/src/views/publish/components/CommonComponents/DouyinCommonComponents.tsx
index 36e829c7..2b7d989f 100644
--- a/src/views/publish/components/CommonComponents/DouyinCommonComponents.tsx
+++ b/src/views/publish/components/CommonComponents/DouyinCommonComponents.tsx
@@ -85,7 +85,7 @@ export const CommonHotspotSelect = ({
filterOption={false}
onSearch={debounceFetcher}
notFoundContent={fetching ? <Spin size="small" /> : null}
- options={(!keywords ? doytinHotAll : options).map((v) => {
+ options={(!keywords ? doytinHotAll : options)?.map((v) => {
return {
...v,
label: v.word,