mirror of
https://github.com/labring/FastGPT.git
synced 2026-01-28 01:10:33 +08:00
76 lines
2.9 KiB
TypeScript
76 lines
2.9 KiB
TypeScript
import { authDatasetByTmbId } from '../../support/permission/dataset/auth';
|
|
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
|
|
import { S3Sources } from '../../common/s3/type';
|
|
import { getS3DatasetSource, S3DatasetSource } from '../../common/s3/sources/dataset';
|
|
import { getS3ChatSource } from '../../common/s3/sources/chat';
|
|
import { jwtSignS3ObjectKey, isS3ObjectKey } from '../../common/s3/utils';
|
|
|
|
// TODO: 需要优化成批量获取权限
|
|
export const filterDatasetsByTmbId = async ({
|
|
datasetIds,
|
|
tmbId
|
|
}: {
|
|
datasetIds: string[];
|
|
tmbId: string;
|
|
}) => {
|
|
const permissions = await Promise.all(
|
|
datasetIds.map(async (datasetId) => {
|
|
try {
|
|
await authDatasetByTmbId({
|
|
tmbId,
|
|
datasetId,
|
|
per: ReadPermissionVal
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
console.log(`Dataset ${datasetId} access denied:`, error);
|
|
return false;
|
|
}
|
|
})
|
|
);
|
|
|
|
// Then filter datasetIds based on permissions
|
|
return datasetIds.filter((_, index) => permissions[index]);
|
|
};
|
|
|
|
/**
|
|
* 替换数据集引用 markdown 文本中的图片链接格式的 S3 对象键为 JWT 签名后的 URL
|
|
*
|
|
* @param documentQuoteText 数据集引用文本
|
|
* @param expiredTime 过期时间
|
|
* @returns 替换后的文本
|
|
*
|
|
* @example
|
|
*
|
|
* ```typescript
|
|
* const datasetQuoteText = '';
|
|
* const replacedText = await replaceS3KeyToPreviewUrl(datasetQuoteText, addDays(new Date(), 90))
|
|
* console.log(replacedText)
|
|
* // ''
|
|
* ```
|
|
*/
|
|
export function replaceS3KeyToPreviewUrl(documentQuoteText: string, expiredTime: Date) {
|
|
if (!documentQuoteText || typeof documentQuoteText !== 'string')
|
|
return documentQuoteText as string;
|
|
|
|
const prefixes = Object.values(S3Sources);
|
|
const pattern = prefixes.map((p) => `${p}\\/[^)]+`).join('|');
|
|
const regex = new RegExp(String.raw`(!?)\[([^\]]*)\]\(\s*(?!https?:\/\/)(${pattern})\s*\)`, 'g');
|
|
|
|
const matches = Array.from(documentQuoteText.matchAll(regex));
|
|
let content = documentQuoteText;
|
|
|
|
for (const match of matches.slice().reverse()) {
|
|
const [full, bang, alt, objectKey] = match;
|
|
|
|
if (isS3ObjectKey(objectKey, 'dataset') || isS3ObjectKey(objectKey, 'chat')) {
|
|
const url = jwtSignS3ObjectKey(objectKey, expiredTime);
|
|
const replacement = `${bang}[${alt}](${url})`;
|
|
content =
|
|
content.slice(0, match.index) + replacement + content.slice(match.index + full.length);
|
|
}
|
|
}
|
|
|
|
return content;
|
|
}
|