mirror of
				https://gitee.com/kekingcn/file-online-preview.git
				synced 2025-10-31 08:26:47 +08:00 
			
		
		
		
	新增 tiff 、tif 图像文件格式预览支持
This commit is contained in:
		| @@ -1,7 +1,10 @@ | ||||
| #######################################不可动态配置,需要重启生效####################################### | ||||
| server.port = ${KK_SERVER_PORT:8012} | ||||
| server.context-path = ${KK_CONTEXT_PATH:/} | ||||
| spring.http.encoding.charset = utf8 | ||||
| server.servlet.context-path= ${KK_CONTEXT_PATH:/} | ||||
| server.servlet.encoding.charset = utf-8 | ||||
| #文件上传限制 | ||||
| spring.servlet.multipart.max-file-size=500MB | ||||
| spring.servlet.multipart.max-request-size=500MB | ||||
| ## Freemarker 配置 | ||||
| spring.freemarker.template-loader-path = classpath:/web/ | ||||
| spring.freemarker.cache = false | ||||
| @@ -13,11 +16,6 @@ spring.freemarker.expose-session-attributes = true | ||||
| spring.freemarker.request-context-attribute = request | ||||
| spring.freemarker.suffix = .ftl | ||||
|  | ||||
| server.tomcat.uri-encoding = UTF-8 | ||||
| #文件上传限制 | ||||
| spring.http.multipart.max-request-size=500MB | ||||
| spring.http.multipart.max-file-size=500MB | ||||
|  | ||||
| #文件资源路径(默认为打包根路径下的file目录下) | ||||
| #file.dir = D:\\kkFileview\\ | ||||
| file.dir = ${KK_FILE_DIR:default} | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| [#ftl] | ||||
| [#-- @implicitly included --] | ||||
| [#-- @ftlvariable name="currentUrl" type="java.lang.String" --] | ||||
| [#-- @ftlvariable name="file" type="cn.keking.model.FileAttribute" --] | ||||
| [#-- @ftlvariable name="fileName" type="java.lang.String" --] | ||||
| [#-- @ftlvariable name="fileTree" type="java.lang.String" --] | ||||
|   | ||||
| @@ -11,52 +11,57 @@ import java.util.Map; | ||||
|  */ | ||||
| public enum FileType { | ||||
|  | ||||
|     picture("pictureFilePreviewImpl"), | ||||
|     compress("compressFilePreviewImpl"), | ||||
|     office("officeFilePreviewImpl"), | ||||
|     simText("simTextFilePreviewImpl"), | ||||
|     pdf("pdfFilePreviewImpl"), | ||||
|     other("otherFilePreviewImpl"), | ||||
|     media("mediaFilePreviewImpl"), | ||||
|     markdown("markdownFilePreviewImpl"), | ||||
|     xml("xmlFilePreviewImpl"), | ||||
|     flv("flvFilePreviewImpl"), | ||||
|     cad("cadFilePreviewImpl"); | ||||
|     PICTURE("pictureFilePreviewImpl"), | ||||
|     COMPRESS("compressFilePreviewImpl"), | ||||
|     OFFICE("officeFilePreviewImpl"), | ||||
|     SIMTEXT("simTextFilePreviewImpl"), | ||||
|     PDF("pdfFilePreviewImpl"), | ||||
|     OTHER("otherFilePreviewImpl"), | ||||
|     MEDIA("mediaFilePreviewImpl"), | ||||
|     MARKDOWN("markdownFilePreviewImpl"), | ||||
|     XML("xmlFilePreviewImpl"), | ||||
|     FLV("flvFilePreviewImpl"), | ||||
|     CAD("cadFilePreviewImpl"), | ||||
|     TIFF("tiffFilePreviewImpl"); | ||||
|  | ||||
|     private static final String[] OFFICE_TYPES = {"docx", "doc", "xls", "xlsx", "ppt", "pptx"}; | ||||
|     private static final String[] PICTURE_TYPES = {"jpg", "jpeg", "png", "gif", "bmp", "ico", "raw"}; | ||||
|     private static final String[] ARCHIVE_TYPES = {"rar", "zip", "jar", "7-zip", "tar", "gzip", "7z"}; | ||||
|     private static final String[] TIFF_TYPES = {"tif", "tiff"}; | ||||
|     private static final String[] SSIM_TEXT_TYPES = ConfigConstants.getSimText(); | ||||
|     private static final String[] MEDIA_TYPES = ConfigConstants.getMedia(); | ||||
|     private static final Map<String, FileType> FILE_TYPE_MAPPER = new HashMap<>(); | ||||
|  | ||||
|     static { | ||||
|         for (String office : OFFICE_TYPES) { | ||||
|             FILE_TYPE_MAPPER.put(office, FileType.office); | ||||
|             FILE_TYPE_MAPPER.put(office, FileType.OFFICE); | ||||
|         } | ||||
|         for (String picture : PICTURE_TYPES) { | ||||
|             FILE_TYPE_MAPPER.put(picture, FileType.picture); | ||||
|             FILE_TYPE_MAPPER.put(picture, FileType.PICTURE); | ||||
|         } | ||||
|         for (String archive : ARCHIVE_TYPES) { | ||||
|             FILE_TYPE_MAPPER.put(archive, FileType.compress); | ||||
|             FILE_TYPE_MAPPER.put(archive, FileType.COMPRESS); | ||||
|         } | ||||
|         for (String text : SSIM_TEXT_TYPES) { | ||||
|             FILE_TYPE_MAPPER.put(text, FileType.simText); | ||||
|             FILE_TYPE_MAPPER.put(text, FileType.SIMTEXT); | ||||
|         } | ||||
|         for (String media : MEDIA_TYPES) { | ||||
|             FILE_TYPE_MAPPER.put(media, FileType.media); | ||||
|             FILE_TYPE_MAPPER.put(media, FileType.MEDIA); | ||||
|         } | ||||
|         FILE_TYPE_MAPPER.put("md", FileType.markdown); | ||||
|         FILE_TYPE_MAPPER.put("xml", FileType.xml); | ||||
|         FILE_TYPE_MAPPER.put("pdf", FileType.pdf); | ||||
|         FILE_TYPE_MAPPER.put("dwg", FileType.cad); | ||||
|         FILE_TYPE_MAPPER.put("flv", FileType.flv); | ||||
|  | ||||
|         for (String tif : TIFF_TYPES) { | ||||
|             FILE_TYPE_MAPPER.put(tif, FileType.TIFF); | ||||
|         } | ||||
|         FILE_TYPE_MAPPER.put("md", FileType.MARKDOWN); | ||||
|         FILE_TYPE_MAPPER.put("xml", FileType.XML); | ||||
|         FILE_TYPE_MAPPER.put("pdf", FileType.PDF); | ||||
|         FILE_TYPE_MAPPER.put("dwg", FileType.CAD); | ||||
|         FILE_TYPE_MAPPER.put("flv", FileType.FLV); | ||||
|     } | ||||
|  | ||||
|     private static FileType to(String fileType){ | ||||
|         return FILE_TYPE_MAPPER.getOrDefault(fileType,other); | ||||
|     private static FileType to(String fileType) { | ||||
|         return FILE_TYPE_MAPPER.getOrDefault(fileType, OTHER); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 查看文件类型(防止参数中存在.点号或者其他特殊字符,所以先抽取文件名,然后再获取文件类型) | ||||
|      * | ||||
|   | ||||
| @@ -68,7 +68,7 @@ public class CompressFileReader { | ||||
|                 String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); | ||||
|                 parentName = (level - 1) + "_" + parentName; | ||||
|                 FileType type = FileType.typeFromUrl(childName); | ||||
|                 if (type.equals(FileType.picture)) {//添加图片文件到图片列表 | ||||
|                 if (type.equals(FileType.PICTURE)) {//添加图片文件到图片列表 | ||||
|                     imgUrls.add(baseUrl + childName); | ||||
|                 } | ||||
|                 FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); | ||||
| @@ -121,7 +121,7 @@ public class CompressFileReader { | ||||
|                 } | ||||
|                 String parentName = getLast2FileName(fullName, "\\", archiveFileName); | ||||
|                 FileType type = FileType.typeFromUrl(childName); | ||||
|                 if (type.equals(FileType.picture)) {//添加图片文件到图片列表 | ||||
|                 if (type.equals(FileType.PICTURE)) {//添加图片文件到图片列表 | ||||
|                     imgUrls.add(baseUrl + childName); | ||||
|                 } | ||||
|                 FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); | ||||
| @@ -164,7 +164,7 @@ public class CompressFileReader { | ||||
|                 String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); | ||||
|                 parentName = (level - 1) + "_" + parentName; | ||||
|                 FileType type = FileType.typeFromUrl(childName); | ||||
|                 if (type.equals(FileType.picture)) {//添加图片文件到图片列表 | ||||
|                 if (type.equals(FileType.PICTURE)) {//添加图片文件到图片列表 | ||||
|                     imgUrls.add(baseUrl + childName); | ||||
|                 } | ||||
|                 FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); | ||||
|   | ||||
| @@ -62,7 +62,7 @@ public class FileConvertQueueTask { | ||||
|                         FileAttribute fileAttribute = fileHandlerService.getFileAttribute(url,null); | ||||
|                         FileType fileType = fileAttribute.getType(); | ||||
|                         logger.info("正在处理预览转换任务,url:{},预览类型:{}", url, fileType); | ||||
|                         if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) { | ||||
|                         if(fileType.equals(FileType.COMPRESS) || fileType.equals(FileType.OFFICE) || fileType.equals(FileType.CAD)) { | ||||
|                             FilePreview filePreview = previewFactory.get(fileAttribute); | ||||
|                             filePreview.filePreviewHandle(url, new ExtendedModelMap(), fileAttribute); | ||||
|                         } else { | ||||
|   | ||||
| @@ -14,6 +14,7 @@ public interface FilePreview { | ||||
|     String COMPRESS_FILE_PREVIEW_PAGE = "compress"; | ||||
|     String MEDIA_FILE_PREVIEW_PAGE = "media"; | ||||
|     String PICTURE_FILE_PREVIEW_PAGE = "picture"; | ||||
|     String TIFF_FILE_PREVIEW_PAGE = "tiff"; | ||||
|     String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture"; | ||||
|     String TXT_FILE_PREVIEW_PAGE = "txt"; | ||||
|     String EXEL_FILE_PREVIEW_PAGE = "html"; | ||||
|   | ||||
| @@ -0,0 +1,27 @@ | ||||
| package cn.keking.service.impl; | ||||
|  | ||||
| import cn.keking.model.FileAttribute; | ||||
| import cn.keking.service.FilePreview; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.ui.Model; | ||||
|  | ||||
| /** | ||||
|  * tiff 图片文件处理 | ||||
|  * @author kl (http://kailing.pub) | ||||
|  * @since 2021/2/8 | ||||
|  */ | ||||
| @Service | ||||
| public class TiffFilePreviewImpl implements FilePreview { | ||||
|  | ||||
|     private final PictureFilePreviewImpl pictureFilePreview; | ||||
|  | ||||
|     public TiffFilePreviewImpl(PictureFilePreviewImpl pictureFilePreview) { | ||||
|         this.pictureFilePreview = pictureFilePreview; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { | ||||
|         pictureFilePreview.filePreviewHandle(url,model,fileAttribute); | ||||
|         return TIFF_FILE_PREVIEW_PAGE; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3189
									
								
								server/src/main/resources/static/js/tiff.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3189
									
								
								server/src/main/resources/static/js/tiff.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										47
									
								
								server/src/main/resources/web/tiff.ftl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								server/src/main/resources/web/tiff.ftl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| <!DOCTYPE html> | ||||
| <html lang="en"> | ||||
| <head> | ||||
|     <meta charset="utf-8"/> | ||||
|     <title>Tiff 图片预览</title> | ||||
|     <link rel="stylesheet" href="css/viewer.min.css"> | ||||
|     <script src="js/tiff.min.js"></script> | ||||
|     <#include "*/commonHeader.ftl"> | ||||
|     <style> | ||||
|         body { | ||||
|             background-color: #404040; | ||||
|         } | ||||
|  | ||||
|         #tiff { | ||||
|             position: fixed; | ||||
|             top:50%; | ||||
|             left:50%; | ||||
|             transform: translate(-50%,-50%); | ||||
|         } | ||||
|  | ||||
|         /*#dowebok li img { width: 200%;}*/ | ||||
|     </style> | ||||
| </head> | ||||
| <body> | ||||
| <input hidden id="currentUrl" value="${currentUrl}"/> | ||||
| <div id="tiff"> | ||||
| </div> | ||||
|  | ||||
| <script> | ||||
|     var xhr = new XMLHttpRequest(); | ||||
|     xhr.responseType = 'arraybuffer'; | ||||
|     xhr.open('GET', $("#currentUrl").val()); | ||||
|     xhr.onload = function (e) { | ||||
|         var tiff = new Tiff({buffer: xhr.response}); | ||||
|         var canvas = tiff.toCanvas(); | ||||
|         $("#tiff").append(canvas) | ||||
|     }; | ||||
|     xhr.send(); | ||||
|  | ||||
|     /*初始化水印*/ | ||||
|     window.onload = function () { | ||||
|         initWaterMark(); | ||||
|     } | ||||
| </script> | ||||
| </body> | ||||
|  | ||||
| </html> | ||||
		Reference in New Issue
	
	Block a user
	 kl
					kl