feat: enhance URL validation and timeout handling in knowledge base components

- Added isValidURL function to validate URLs before rendering links in the document content component, preventing invalid links from being clickable.
- Refactored timeout handling in KnowledgeBase.vue to use setTimeout instead of setInterval, improving performance and resource management.
- Ensured that timeouts are cleared appropriately to avoid memory leaks and unnecessary polling.

This update improves the robustness of URL handling and optimizes the component's performance.
This commit is contained in:
wizardchen
2026-04-16 12:24:57 +08:00
parent a78182cfc7
commit 7c40fb6ac9
3 changed files with 49 additions and 11 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import mermaid from "mermaid";
import { onMounted, ref, nextTick, onUnmounted, watch, computed } from "vue";
import { downKnowledgeDetails, deleteGeneratedQuestion, getChunkByIdOnly, previewKnowledgeFile } from "@/api/knowledge-base/index";
import { MessagePlugin, DialogPlugin } from "tdesign-vue-next";
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL, hydrateProtectedFileImages } from '@/utils/security';
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL, hydrateProtectedFileImages, isValidURL } from '@/utils/security';
import { openMermaidFullscreen } from '@/utils/mermaidViewer';
import { useI18n } from 'vue-i18n';
import DocumentPreview from '@/components/document-preview.vue';
@@ -778,7 +778,7 @@ const handleDetailsScroll = () => {
<div v-else-if="details.type === 'url'" class="url_box">
<span class="label">{{ $t('knowledgeBase.urlSource') }}</span>
<div class="url_link_box">
<a :href="details.source" target="_blank" class="url_link">
<a :href="isValidURL(details.source) ? details.source : 'javascript:void(0)'" :target="isValidURL(details.source) ? '_blank' : undefined" class="url_link">
<t-icon name="link" size="14px" />
<span class="url_text">{{ details.source }}</span>
<t-icon name="jump" size="14px" class="jump-icon" />
+45 -7
View File
@@ -166,7 +166,7 @@ const onVisibleChange = (visible: boolean) => {
}
};
let isCardDetails = ref(false);
let timeout: ReturnType<typeof setInterval> | null = null;
let timeout: ReturnType<typeof setTimeout> | null = null;
let delDialog = ref(false)
let rebuildDialog = ref(false)
let rebuildKnowledgeItem = ref<KnowledgeCard>({ id: '', parse_status: '' })
@@ -715,6 +715,10 @@ onUnmounted(() => {
window.removeEventListener('knowledgeFileUploaded', handleFileUploaded as EventListener);
window.removeEventListener('openURLImportDialog', handleOpenURLImportDialog as EventListener);
stopMovePoll();
if (timeout !== null) {
clearTimeout(timeout);
timeout = null;
}
});
watch(() => cardList.value, (newValue) => {
if (isFAQ.value) return;
@@ -734,7 +738,7 @@ watch(() => cardList.value, (newValue) => {
return isParsing || isSummaryPending;
})
if (timeout !== null) {
clearInterval(timeout);
clearTimeout(timeout);
timeout = null;
}
if (analyzeList.length) {
@@ -761,25 +765,59 @@ type KnowledgeCard = {
tag_id?: string;
};
const updateStatus = (analyzeList: KnowledgeCard[]) => {
if (timeout !== null) {
clearTimeout(timeout);
timeout = null;
}
if (!analyzeList.length) return;
let query = ``;
for (let i = 0; i < analyzeList.length; i++) {
query += `ids=${analyzeList[i].id}&`;
}
timeout = setInterval(() => {
timeout = setTimeout(() => {
batchQueryKnowledge(query).then((result: any) => {
let hasChanges = false;
if (result.success && result.data) {
(result.data as KnowledgeCard[]).forEach((item: KnowledgeCard) => {
const index = cardList.value.findIndex(card => card.id == item.id);
if (index == -1) return;
// Always update the card data
cardList.value[index].parse_status = item.parse_status;
cardList.value[index].summary_status = item.summary_status;
cardList.value[index].description = item.description;
if (cardList.value[index].parse_status !== item.parse_status ||
cardList.value[index].summary_status !== item.summary_status ||
cardList.value[index].description !== item.description) {
// Always update the card data
cardList.value[index].parse_status = item.parse_status;
cardList.value[index].summary_status = item.summary_status;
cardList.value[index].description = item.description;
hasChanges = true;
}
});
}
// If there are no changes, the watch won't trigger, so we must manually poll again
// Even if there are changes, we can manually poll again just to be safe.
// The watch will clear this timeout if it triggers.
const stillPending = cardList.value.filter(item => {
const isParsing = item.parse_status == 'pending' || item.parse_status == 'processing';
const isSummaryPending = item.parse_status == 'completed' &&
(item.summary_status == 'pending' || item.summary_status == 'processing');
return isParsing || isSummaryPending;
});
if (stillPending.length > 0) {
updateStatus(stillPending);
}
}).catch((_err) => {
// 错误处理
const stillPending = cardList.value.filter(item => {
const isParsing = item.parse_status == 'pending' || item.parse_status == 'processing';
const isSummaryPending = item.parse_status == 'completed' &&
(item.summary_status == 'pending' || item.summary_status == 'processing');
return isParsing || isSummaryPending;
});
if (stillPending.length > 0) {
updateStatus(stillPending);
}
});
}, 1500);
};
+2 -2
View File
@@ -162,8 +162,8 @@ func IsValidURL(url string) bool {
return false
}
// 检查协议, 只允许 http, https, local, minio, cos, tos 协议
allowedProtocols := []string{"http://", "https://", "local://", "minio://", "cos://", "tos://"}
// 检查协议, 只允许 http, https, local, minio, cos, tos, oss 协议
allowedProtocols := []string{"http://", "https://", "local://", "minio://", "cos://", "tos://", "oss://"}
isAllowed := false
for _, protocol := range allowedProtocols {
if strings.HasPrefix(strings.ToLower(url), protocol) {