This commit is contained in:
+25
-2
@@ -415,9 +415,32 @@ def preview(resource_id):
|
||||
abort(404)
|
||||
|
||||
ext = (res.original_name or res.filename or '').rsplit('.', 1)[-1].lower()
|
||||
mime = (res.mime_type or '').lower()
|
||||
|
||||
DOCX_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
||||
DOC_MIME = 'application/msword'
|
||||
|
||||
# 部分来源(如 URL 下载)文件名没有扩展名,需用 MIME 与文件头兜底判断
|
||||
is_docx = ext == 'docx' or mime == DOCX_MIME
|
||||
is_doc = ext == 'doc' or mime == DOC_MIME
|
||||
if not (is_docx or is_doc):
|
||||
try:
|
||||
with open(abs_path, 'rb') as f:
|
||||
head = f.read(8)
|
||||
# docx/xlsx/pptx 实际上是 zip:PK\x03\x04
|
||||
if head.startswith(b'PK\x03\x04') and (
|
||||
'wordprocessingml' in mime or 'officedocument' in mime
|
||||
):
|
||||
is_docx = True
|
||||
# 旧版 .doc 二进制头:D0 CF 11 E0 A1 B1 1A E1
|
||||
elif head.startswith(b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1') and \
|
||||
('msword' in mime or mime == ''):
|
||||
is_doc = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Word 文档(.docx)使用 mammoth 转换为 HTML
|
||||
if ext == 'docx':
|
||||
if is_docx:
|
||||
try:
|
||||
import mammoth
|
||||
with open(abs_path, 'rb') as f:
|
||||
@@ -437,7 +460,7 @@ def preview(resource_id):
|
||||
)
|
||||
|
||||
# 旧版 .doc 二进制格式不支持在线预览
|
||||
if ext == 'doc':
|
||||
if is_doc:
|
||||
return Response(
|
||||
'<div style="padding:1rem;color:#6c757d">'
|
||||
'不支持在线预览旧版 .doc 文件,请下载后查看,或将文件转换为 .docx 格式。</div>',
|
||||
|
||||
@@ -157,7 +157,11 @@
|
||||
{% elif resource.resource_type == 'text' %}
|
||||
{%- set _name = (resource.original_name or resource.filename or '') -%}
|
||||
{%- set _ext = _name.rsplit('.', 1)[-1].lower() if '.' in _name else '' -%}
|
||||
{% if _ext in ('doc', 'docx') %}
|
||||
{%- set _mime = (resource.mime_type or '')|lower -%}
|
||||
{%- set _is_word = _ext in ('doc', 'docx')
|
||||
or 'wordprocessingml' in _mime
|
||||
or _mime == 'application/msword' -%}
|
||||
{% if _is_word %}
|
||||
<div class="text-preview-toolbar p-2 border-bottom d-flex gap-2 align-items-center">
|
||||
<i class="bi bi-file-earmark-word text-primary"></i>
|
||||
<span class="small text-muted">Word 文档预览</span>
|
||||
@@ -212,7 +216,11 @@
|
||||
{% if resource.resource_type == 'text' and resource.file_path and resource.download_status in ('na','done') %}
|
||||
{%- set _name = (resource.original_name or resource.filename or '') -%}
|
||||
{%- set _ext = _name.rsplit('.', 1)[-1].lower() if '.' in _name else '' -%}
|
||||
{% if _ext in ('doc', 'docx') %}
|
||||
{%- set _mime = (resource.mime_type or '')|lower -%}
|
||||
{%- set _is_word = _ext in ('doc', 'docx')
|
||||
or 'wordprocessingml' in _mime
|
||||
or _mime == 'application/msword' -%}
|
||||
{% if _is_word %}
|
||||
// Word 文档:服务端已转换为 HTML,直接渲染
|
||||
fetch("{{ url_for('resources.preview', resource_id=resource.id) }}")
|
||||
.then(r => r.text())
|
||||
|
||||
Reference in New Issue
Block a user