perf:源文件预览去除【下载文件】功能
This commit is contained in:
1 parent
8206630823
commit
445763e144
1 file changed
-76
@@ -48,7 +48,6 @@ const emit = defineEmits<{
|
||||
const { t } = useI18n();
|
||||
|
||||
const isLoading = ref(false);
|
||||
const isDownloading = ref(false);
|
||||
const error = ref('');
|
||||
const markdownHtml = ref('');
|
||||
const excelPreview = ref<ExcelPreviewData | null>(null);
|
||||
@@ -150,21 +149,6 @@ const readErrorMessage = async (res: Response) => {
|
||||
return extractErrorMessage(text, fallback);
|
||||
};
|
||||
|
||||
const extractDownloadFilename = (res: Response) => {
|
||||
const disposition = res.headers.get('content-disposition') || '';
|
||||
const utf8Match = disposition.match(/filename\*=UTF-8''([^;]+)/i);
|
||||
if (utf8Match?.[1]) {
|
||||
try {
|
||||
return decodeURIComponent(utf8Match[1]);
|
||||
} catch {
|
||||
return utf8Match[1];
|
||||
}
|
||||
}
|
||||
const asciiMatch = disposition.match(/filename="([^"]+)"/i);
|
||||
if (asciiMatch?.[1]) return asciiMatch[1];
|
||||
return '';
|
||||
};
|
||||
|
||||
const applyExcelPreview = (buffer: ArrayBuffer) => {
|
||||
const workbook = XLSX.read(buffer, { type: 'array' });
|
||||
if (!workbook.SheetNames.length) {
|
||||
@@ -331,42 +315,6 @@ const loadBinaryContent = async () => {
|
||||
officeObjectUrl.value = URL.createObjectURL(blob);
|
||||
};
|
||||
|
||||
const downloadFile = async () => {
|
||||
if (!props.file?.url || !props.agentId || isDownloading.value) return;
|
||||
|
||||
isDownloading.value = true;
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
download: '1',
|
||||
});
|
||||
const res = await fetch(`/api/chat/preview-file-raw?${params.toString()}`, {
|
||||
headers: props.authHeaders || {},
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(await readErrorMessage(res));
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
const downloadUrl = URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = downloadUrl;
|
||||
link.download = extractDownloadFilename(res) || props.file.name || 'download';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.setTimeout(() => URL.revokeObjectURL(downloadUrl), 1000);
|
||||
} catch (e: any) {
|
||||
error.value = t('filePreview.msg.downloadFailed', {
|
||||
reason: e?.message || t('filePreview.msg.unknownError'),
|
||||
});
|
||||
} finally {
|
||||
isDownloading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 在弹窗/文件变化时加载预览内容。
|
||||
* 流程:
|
||||
@@ -507,16 +455,6 @@ watch(
|
||||
<div class="min-w-0">
|
||||
<div class="text-sm font-medium text-slate-500 dark:text-slate-400 tracking-wide">{{ t('filePreview.title') }}</div>
|
||||
<div class="mt-1 font-semibold text-slate-900 dark:text-slate-100 break-words">{{ file.name }}</div>
|
||||
<button type="button" @click="downloadFile" :disabled="isDownloading" class="mt-2 inline-flex items-center justify-center px-3 py-1.5 text-xs font-medium rounded-lg bg-brand-50 text-brand-700 hover:bg-brand-100 border border-brand-200 dark:bg-brand-900/30 dark:text-brand-300 dark:border-brand-800 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
|
||||
<svg v-if="!isDownloading" xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
|
||||
</svg>
|
||||
<svg v-else class="animate-spin -ml-1 mr-2 h-4 w-4 text-brand-700 dark:text-brand-300" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
{{ isDownloading ? t('filePreview.downloading') : t('filePreview.download') }}
|
||||
</button>
|
||||
</div>
|
||||
<button type="button" class="btn-secondary shrink-0" @click="close">{{ t('common.close') }}</button>
|
||||
</div>
|
||||
@@ -611,20 +549,6 @@ watch(
|
||||
<div class="mt-2 text-sm text-muted">
|
||||
This Office file uses a private or non-HTTPS link, so online preview cannot render it.
|
||||
</div>
|
||||
<div class="mt-4 flex gap-3">
|
||||
<button
|
||||
type="button"
|
||||
@click="downloadFile"
|
||||
:disabled="isDownloading"
|
||||
class="px-4 py-2 rounded-lg bg-brand-600 text-white text-sm hover:bg-brand-700 disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center"
|
||||
>
|
||||
<svg v-if="isDownloading" class="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
{{ isDownloading ? t('filePreview.downloading') : t('filePreview.download') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<iframe
|
||||
v-else
|
||||
|
||||
Reference in new issue
Block a user