Repository navigation
fix: 修复 Office 文件先 PDF 预览再切换图片预览时提示缓存异常 - #802
Open
yuhaibohotmail wants to merge 1 commit into
Open
yuhaibohotmail wants to merge 1 commit into
yuhaibohotmail wants to merge 1 commit into
Conversation
PDF 预览与图片预览共用同一个 cacheName(转换出的 PDF 文件名)。PDF 预览 转换完成后 cacheName 已记入已转换文件缓存,此时图片还没有生成;图片预览 只判断 cacheName 是否在缓存中,命中后直接读取图片缓存,读到空列表就返回 「Office转换缓存异常,请联系管理员」。 图片预览判断是否需要转换时,增加图片缓存是否存在的判断(包含加密文档的 图片缓存),不存在时按未转换处理,走原有的异步转换流程。 新增 OfficeImagePreviewAfterPdfTests:只有 PDF 缓存时应进入转换并返回等待页; 图片缓存或加密文档图片缓存已存在时直接渲染,不重新转换。 Fixes kekingcn#754 Signed-off-by: yuhaibo <yuhaibo@hotmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
Office 文件(docx、pptx 等)先用 PDF 方式预览,再点「使用图片预览」(或直接带
officePreviewType=image打开同一个文件),页面提示:同一个文件如果一开始就用图片方式预览则正常。在当前 master(即 5.0.3)上用下面的单元测试可以稳定复现,现象与 #754 一致。
原因
OfficeFilePreviewImpl#filePreviewHandle里,PDF 预览与图片预览共用同一个cacheName(转换出的 PDF 文件名):handleRegularOfficePreview)转换完成后调用fileHandlerService.addConvertedFile(cacheName, ...),此时只有 PDF,图片还没有生成;fileHandlerService.listConvertedFiles().containsKey(cacheName)。上一步已经把cacheName记进去了,于是走到「已有缓存,直接渲染」,进入getPreviewType;getPreviewType调fileHandlerService.loadPdf2jpgCache(outFilePath)取图片列表,取到空列表,返回「Office转换缓存异常,请联系管理员」。PdfFilePreviewImpl没有这个问题,因为它的cacheName是pdfName + officePreviewType,两种预览方式各用各的缓存名。修复
图片预览分支判断是否需要转换时,增加「图片缓存是否存在」这一条件:
hasImageCache与getPreviewType取图片的两个来源保持一致:加密文档的图片缓存(pdftojpgservice.hasEncryptedPdfCacheSimple)或普通图片缓存(fileHandlerService.loadPdf2jpgCache)有一个存在就算有。都没有时按未转换处理,走原有的异步转换流程并返回等待页;转换完成后再次进入就直接渲染图片。只改了这一个判断,没有改
cacheName的生成方式,已有的缓存不受影响;PDF 预览分支没有改动。验证
新增
OfficeImagePreviewAfterPdfTests(Mockito,不需要 LibreOffice),3 条:shouldConvertToImagesWhenOnlyThePdfIsCached:已转换文件缓存里有cacheName、图片缓存为空时,以图片方式预览应返回等待页,并触发 PDF 转图片;shouldRenderCachedImagesWithoutConvertingAgain:图片缓存已存在时直接返回图片预览页,不重新转换;shouldRenderEncryptedImageCacheWithoutConvertingAgain:加密文档的图片缓存存在时同样直接渲染,不重新转换。修改前先跑:第 1 条失败(
expected: <waiting> but was: <fileNotSupported>,msg 为「Office转换缓存异常,请联系管理员」),另外 2 条通过;修改后 3 条全部通过。把新增的条件去掉,第 1 条重新失败;只去掉加密缓存那一半,第 3 条失败。mvn -B -pl server test -Dtest='!ServerMainTests'(Windows,JDK 25):Tests run: 90, Failures: 0, Errors: 1。唯一的 1 条是ArchiveDirectorySecurityTests.cachedArchivePreviewRendersItsTreeWithoutAnEnumerationRequest,它要创建文件名带双引号的文件,Windows 不允许,在未修改的 master 上同样失败(master 为 87 条、同样 1 条 Error),与本次修改无关。Fixes #754