|
| 1 | +/** |
| 2 | + * 文档预览决策服务 |
| 3 | + * 负责基于文件元信息与 Link JSON 生成 DocumentPreviewResult |
| 4 | + */ |
| 5 | + |
| 6 | +import { FILE_TYPES } from "../constants/index.js"; |
| 7 | +import previewSettingsCache from "../cache/PreviewSettingsCache.js"; |
| 8 | +import { getFileExtension } from "../utils/fileTypeDetector.js"; |
| 9 | + |
| 10 | +/** |
| 11 | + * 解析文件类型是否属于 Office/文档范畴 |
| 12 | + * @param {any} fileMeta - 文件元信息(至少包含 type/typeName/mimetype/filename) |
| 13 | + * @returns {boolean} |
| 14 | + */ |
| 15 | +function isOfficeLike(fileMeta) { |
| 16 | + if (!fileMeta) return false; |
| 17 | + |
| 18 | + const typeCode = fileMeta.type; |
| 19 | + if (typeCode === FILE_TYPES.OFFICE || typeCode === FILE_TYPES.DOCUMENT) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + const typeName = (fileMeta.typeName || "").toString().toLowerCase(); |
| 24 | + if (typeName === "office" || typeName === "document") { |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + const mimetype = (fileMeta.mimetype || "").toLowerCase(); |
| 29 | + if ( |
| 30 | + mimetype.includes("officedocument") || |
| 31 | + mimetype.includes("ms-excel") || |
| 32 | + mimetype.includes("ms-powerpoint") || |
| 33 | + mimetype.includes("msword") || |
| 34 | + mimetype === "application/rtf" || |
| 35 | + mimetype === "application/pdf" |
| 36 | + ) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + return false; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * 统一的 DocumentPreview 决策入口 |
| 45 | + * @param {Object} fileMeta - 文件元信息(type/typeName/mimetype/filename/size 等) |
| 46 | + * @param {Object} linkJson - Link JSON 视角下的链接信息(rawUrl/linkType/use_proxy 等) |
| 47 | + * @returns {Promise<{providers?: Record<string,string>}>} |
| 48 | + */ |
| 49 | +export async function resolveDocumentPreview(fileMeta, linkJson) { |
| 50 | + const baseResult = {}; |
| 51 | + |
| 52 | + // 非 Office/文档类型直接拒绝 |
| 53 | + if (!isOfficeLike(fileMeta)) { |
| 54 | + return baseResult; |
| 55 | + } |
| 56 | + |
| 57 | + const link = linkJson || {}; |
| 58 | + const rawUrl = link.rawUrl || null; |
| 59 | + |
| 60 | + // 基于扩展名与 preview_document_apps 配置选择 DocumentApp 模板 |
| 61 | + const filename = fileMeta.filename || ""; |
| 62 | + const extension = getFileExtension(filename); |
| 63 | + |
| 64 | + const appsConfig = previewSettingsCache.getDocumentAppsConfig(); |
| 65 | + if (!appsConfig || !extension) { |
| 66 | + console.warn( |
| 67 | + "[DocumentPreview] 无有效 DocumentApp 配置或扩展名为空", |
| 68 | + JSON.stringify({ |
| 69 | + filename, |
| 70 | + extension, |
| 71 | + hasConfig: !!appsConfig, |
| 72 | + configKeys: appsConfig ? Object.keys(appsConfig) : [], |
| 73 | + }), |
| 74 | + ); |
| 75 | + return baseResult; |
| 76 | + } |
| 77 | + |
| 78 | + const matchedEntry = findMatchedDocumentAppEntry(appsConfig, extension, filename); |
| 79 | + if (!matchedEntry) { |
| 80 | + console.warn( |
| 81 | + "[DocumentPreview] 未找到匹配的 DocumentApp 条目", |
| 82 | + JSON.stringify({ |
| 83 | + filename, |
| 84 | + extension, |
| 85 | + configKeys: Object.keys(appsConfig || {}), |
| 86 | + }), |
| 87 | + ); |
| 88 | + return baseResult; |
| 89 | + } |
| 90 | + |
| 91 | + const providers = buildProvidersFromTemplate(matchedEntry.providers, { |
| 92 | + url: rawUrl, |
| 93 | + name: filename, |
| 94 | + }); |
| 95 | + |
| 96 | + console.log( |
| 97 | + "[DocumentPreview] 已生成 providers", |
| 98 | + JSON.stringify({ |
| 99 | + filename, |
| 100 | + extension, |
| 101 | + providerKeys: Object.keys(providers), |
| 102 | + }), |
| 103 | + ); |
| 104 | + |
| 105 | + const providerKeys = Object.keys(providers); |
| 106 | + if (!providerKeys.length) { |
| 107 | + return baseResult; |
| 108 | + } |
| 109 | + |
| 110 | + return { |
| 111 | + providers, |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * 在 DocumentApp 配置中根据扩展名/文件名查找匹配条目 |
| 117 | + * @param {Object} appsConfig |
| 118 | + * @param {string} extension |
| 119 | + * @param {string} filename |
| 120 | + * @returns {{providers: Object}|null} |
| 121 | + */ |
| 122 | +function findMatchedDocumentAppEntry(appsConfig, extension, filename) { |
| 123 | + const ext = extension.toLowerCase(); |
| 124 | + |
| 125 | + for (const [pattern, providersConfig] of Object.entries(appsConfig)) { |
| 126 | + if (!providersConfig || typeof providersConfig !== "object") continue; |
| 127 | + |
| 128 | + let matched = false; |
| 129 | + |
| 130 | + if (pattern.startsWith("/") && pattern.endsWith("/")) { |
| 131 | + // 正则匹配:用于高级场景(例如按文件名匹配) |
| 132 | + const body = pattern.slice(1, -1); |
| 133 | + try { |
| 134 | + const regex = new RegExp(body); |
| 135 | + matched = regex.test(filename); |
| 136 | + } catch (e) { |
| 137 | + console.warn("preview_document_apps 中正则模式无效,已跳过:", pattern, e); |
| 138 | + } |
| 139 | + } else { |
| 140 | + // 扩展名列表匹配 |
| 141 | + const exts = pattern |
| 142 | + .split(",") |
| 143 | + .map((p) => p.trim().toLowerCase()) |
| 144 | + .filter((p) => p.length > 0); |
| 145 | + matched = exts.includes(ext); |
| 146 | + } |
| 147 | + |
| 148 | + if (!matched) continue; |
| 149 | + |
| 150 | + // 归一化 providersConfig,支持 string 或 object 形式 |
| 151 | + const normalizedProviders = {}; |
| 152 | + |
| 153 | + for (const [providerKey, cfg] of Object.entries(providersConfig)) { |
| 154 | + if (!cfg) continue; |
| 155 | + |
| 156 | + if (typeof cfg === "string") { |
| 157 | + normalizedProviders[providerKey] = { |
| 158 | + urlTemplate: cfg, |
| 159 | + }; |
| 160 | + } else if (typeof cfg === "object") { |
| 161 | + normalizedProviders[providerKey] = { |
| 162 | + urlTemplate: cfg.urlTemplate || "", |
| 163 | + }; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return { providers: normalizedProviders }; |
| 168 | + } |
| 169 | + |
| 170 | + return null; |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * 根据模板与变量构造 providers 映射 |
| 175 | + * @param {Object} providersConfig |
| 176 | + * @param {{url: string|null, name: string}} vars |
| 177 | + * @returns {Record<string,string>} |
| 178 | + */ |
| 179 | +function buildProvidersFromTemplate(providersConfig, vars) { |
| 180 | + const result = {}; |
| 181 | + |
| 182 | + const url = vars.url || ""; |
| 183 | + const name = vars.name || ""; |
| 184 | + |
| 185 | + const valueMap = { |
| 186 | + $url: url, |
| 187 | + $name: name, |
| 188 | + $e_url: url ? encodeURIComponent(url) : "", |
| 189 | + }; |
| 190 | + |
| 191 | + for (const [providerKey, cfg] of Object.entries(providersConfig)) { |
| 192 | + if (!cfg || !cfg.urlTemplate) continue; |
| 193 | + let rendered = cfg.urlTemplate; |
| 194 | + |
| 195 | + rendered = rendered.replace(/\$e_url|\$url|\$name/g, (token) => valueMap[token] ?? ""); |
| 196 | + |
| 197 | + if (rendered) { |
| 198 | + result[providerKey] = rendered; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return result; |
| 203 | +} |
0 commit comments