feat: finalize mindspace service extraction phase a
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import path from 'node:path';
|
||||
|
||||
export async function handleMindSpaceLongImageDownload({
|
||||
query,
|
||||
filePath,
|
||||
pageUrl = null,
|
||||
res,
|
||||
isLongImageDownloadRequest,
|
||||
longImagePathForHtml,
|
||||
renderLongImage,
|
||||
} = {}) {
|
||||
if (!isLongImageDownloadRequest?.(query)) return false;
|
||||
const longImagePath = longImagePathForHtml(filePath);
|
||||
try {
|
||||
await renderLongImage(pageUrl
|
||||
? { url: pageUrl, outputPath: longImagePath }
|
||||
: { htmlPath: filePath, outputPath: longImagePath });
|
||||
res.set('Cache-Control', 'no-store');
|
||||
res.download(longImagePath, path.basename(longImagePath), (err) => {
|
||||
if (err && !res.headersSent) res.status(404).json({ message: '长图文件不存在' });
|
||||
});
|
||||
} catch (error) {
|
||||
res
|
||||
.status(500)
|
||||
.type('text/plain; charset=utf-8')
|
||||
.send(`长图生成失败:${error?.message || '未知错误'}`);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function decorateMindSpacePublishedHtml({
|
||||
html,
|
||||
embed = false,
|
||||
context,
|
||||
userAgent = '',
|
||||
preparePublicationHtmlForEmbed,
|
||||
injectOgTags,
|
||||
injectWechatShareBridge,
|
||||
injectPublicFileShareButton,
|
||||
publishedPageCsp,
|
||||
isWechatUserAgent,
|
||||
} = {}) {
|
||||
let nextHtml = html;
|
||||
let allowEmbedFrame = false;
|
||||
|
||||
if (embed) {
|
||||
nextHtml = preparePublicationHtmlForEmbed(nextHtml);
|
||||
allowEmbedFrame = true;
|
||||
}
|
||||
|
||||
try {
|
||||
nextHtml = injectOgTags(nextHtml, {
|
||||
origin: context.origin,
|
||||
pageUrl: context.pageUrl,
|
||||
pageDirUrl: context.pageDirUrl,
|
||||
fallbackImageUrl: context.fallbackImageUrl,
|
||||
});
|
||||
const wechatShare = !embed && isWechatUserAgent(userAgent || '');
|
||||
if (wechatShare) {
|
||||
nextHtml = injectWechatShareBridge(nextHtml, { pageUrl: context.pageUrl });
|
||||
}
|
||||
const shareInjection = !embed
|
||||
? injectPublicFileShareButton(nextHtml)
|
||||
: { html: nextHtml, scriptHashes: [] };
|
||||
nextHtml = shareInjection.html;
|
||||
return {
|
||||
html: nextHtml,
|
||||
csp: publishedPageCsp(nextHtml, {
|
||||
embed,
|
||||
wechatShare,
|
||||
scriptHashes: shareInjection.scriptHashes,
|
||||
}),
|
||||
allowEmbedFrame,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
html: nextHtml,
|
||||
csp: publishedPageCsp(nextHtml, { embed }),
|
||||
allowEmbedFrame,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user