mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-27 16:33:49 +00:00
perf: url fetch
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
// pages/api/fetchContent.ts
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import axios from 'axios';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import { Readability } from '@mozilla/readability';
|
||||
import { jsonRes } from '@/service/response';
|
||||
|
||||
const fetchContent = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const { url } = req.body;
|
||||
if (!url) {
|
||||
return res.status(400).json({ error: 'URL is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get(url, {
|
||||
httpsAgent: new (require('https').Agent)({
|
||||
rejectUnauthorized: false,
|
||||
}),
|
||||
});
|
||||
|
||||
const dom = new JSDOM(response.data, {
|
||||
url,
|
||||
contentType: 'text/html',
|
||||
});
|
||||
|
||||
const reader = new Readability(dom.window.document);
|
||||
const article = reader.parse();
|
||||
|
||||
if (!article) {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: '页面获取失败或页面为空'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
jsonRes(res, {
|
||||
code: 200,
|
||||
data: article.content
|
||||
});
|
||||
|
||||
} catch (error:any) {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export default fetchContent;
|
64
client/src/pages/api/plugins/urlFetch.ts
Normal file
64
client/src/pages/api/plugins/urlFetch.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
// pages/api/fetchContent.ts
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import axios from 'axios';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import { Readability } from '@mozilla/readability';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
|
||||
type FetchResultItem = {
|
||||
url: string;
|
||||
title: string;
|
||||
content: string;
|
||||
};
|
||||
export type UrlFetchResponse = FetchResultItem[];
|
||||
|
||||
const fetchContent = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
try {
|
||||
const { urlList = [] } = req.body as { urlList: string[] };
|
||||
|
||||
if (!urlList || urlList.length === 0) {
|
||||
throw new Error('urlList is empty');
|
||||
}
|
||||
|
||||
await authUser({ req });
|
||||
|
||||
const response = (
|
||||
await Promise.allSettled(
|
||||
urlList.map(async (url) => {
|
||||
const fetchRes = await axios.get(url, {
|
||||
timeout: 30000
|
||||
});
|
||||
|
||||
const dom = new JSDOM(fetchRes.data, {
|
||||
url,
|
||||
contentType: 'text/html'
|
||||
});
|
||||
|
||||
const reader = new Readability(dom.window.document);
|
||||
const article = reader.parse();
|
||||
|
||||
return {
|
||||
url,
|
||||
title: article?.title || '',
|
||||
content: article?.textContent || ''
|
||||
};
|
||||
})
|
||||
)
|
||||
)
|
||||
.filter((item) => item.status === 'fulfilled')
|
||||
.map((item: any) => item.value)
|
||||
.filter((item) => item.content);
|
||||
|
||||
jsonRes<UrlFetchResponse>(res, {
|
||||
data: response
|
||||
});
|
||||
} catch (error: any) {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default fetchContent;
|
Reference in New Issue
Block a user