From 6dce47e47f4cafdeb70db956eb08057ec99c38dc Mon Sep 17 00:00:00 2001 From: zhangzhen1979 Date: Sun, 5 Dec 2021 12:38:30 +0800 Subject: [PATCH] =?UTF-8?q?*=20tif=E5=9B=BE=E7=89=87=E9=A2=84=E8=A7=88?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8application.properties=E4=B8=AD=E5=8A=A0?= =?UTF-8?q?=E5=85=A5tif.preview.type=20=3D=20${KK=5FTIF=5FPREVIEW=5FTYPE:t?= =?UTF-8?q?if}=EF=BC=8C=E5=8F=AF=E4=BB=A5=E6=8E=A7=E5=88=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8tif=E3=80=81jpg=E3=80=81pdf=E6=96=B9=E5=BC=8F=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E3=80=82=20=E4=BF=AE=E6=94=B9=E7=9B=B8=E5=BA=94?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=8A=A0=E5=85=A5=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/pom.xml | 2 + server/src/main/config/application.properties | 3 + .../cn/keking/config/ConfigConstants.java | 17 +++- .../keking/config/ConfigRefreshComponent.java | 5 + .../java/cn/keking/service/FilePreview.java | 1 + .../service/impl/TiffFilePreviewImpl.java | 96 ++++++++++++------- 6 files changed, 86 insertions(+), 38 deletions(-) diff --git a/server/pom.xml b/server/pom.xml index d0288345..72a1941a 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -260,6 +260,7 @@ javax.media jai_core 1.1.2-beta + system ${basedir}/lib/jai_core-1.1.2-beta.jar @@ -267,6 +268,7 @@ javax.media jai_codec 1.1.3 + system ${basedir}/lib/jai_codec-1.1.3.jar diff --git a/server/src/main/config/application.properties b/server/src/main/config/application.properties index 122182e4..e17cadaf 100644 --- a/server/src/main/config/application.properties +++ b/server/src/main/config/application.properties @@ -106,3 +106,6 @@ watermark.height = ${WATERMARK_HEIGHT:80} #水印倾斜度数,要求设置在大于等于0,小于90 watermark.angle = ${WATERMARK_ANGLE:10} +#Tif类型图片浏览模式:tif(利用前端js插件浏览);jpg(转换为jpg后前端显示);pdf(转换为pdf后显示,便于打印) +tif.preview.type = ${KK_TIF_PREVIEW_TYPE:tif} + diff --git a/server/src/main/java/cn/keking/config/ConfigConstants.java b/server/src/main/java/cn/keking/config/ConfigConstants.java index cdef9a28..83993082 100644 --- a/server/src/main/java/cn/keking/config/ConfigConstants.java +++ b/server/src/main/java/cn/keking/config/ConfigConstants.java @@ -37,6 +37,7 @@ public class ConfigConstants { private static CopyOnWriteArraySet trustHostSet; private static String pdfDownloadDisable; private static Boolean fileUploadDisable; + private static String tifPreviewType; public static final String DEFAULT_CACHE_ENABLED = "true"; public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd"; @@ -52,7 +53,7 @@ public class ConfigConstants { public static final String DEFAULT_TRUST_HOST = "default"; public static final String DEFAULT_PDF_DOWNLOAD_DISABLE = "true"; public static final String DEFAULT_FILE_UPLOAD_DISABLE = "false"; - + public static final String DEFAULT_TIF_PREVIEW_TYPE = "jpg"; public static Boolean isCacheEnabled() { return cacheEnabled; @@ -284,4 +285,18 @@ public class ConfigConstants { public static void setFileUploadDisableValue(Boolean fileUploadDisable) { ConfigConstants.fileUploadDisable = fileUploadDisable; } + + + public static String getTifPreviewType() { + return tifPreviewType; + } + + @Value("${tif.preview.type:tif}") + public void setTifPreviewType(String tifPreviewType) { + setTifPreviewTypeValue(tifPreviewType); + } + + public static void setTifPreviewTypeValue(String tifPreviewType) { + ConfigConstants.tifPreviewType = tifPreviewType; + } } diff --git a/server/src/main/java/cn/keking/config/ConfigRefreshComponent.java b/server/src/main/java/cn/keking/config/ConfigRefreshComponent.java index 7e53a052..c448aeb8 100644 --- a/server/src/main/java/cn/keking/config/ConfigRefreshComponent.java +++ b/server/src/main/java/cn/keking/config/ConfigRefreshComponent.java @@ -49,6 +49,8 @@ public class ConfigRefreshComponent { String trustHost; String pdfDownloadDisable; boolean fileUploadDisable; + String tifPreviewType; + while (true) { FileReader fileReader = new FileReader(configFilePath); BufferedReader bufferedReader = new BufferedReader(fileReader); @@ -68,6 +70,8 @@ public class ConfigRefreshComponent { trustHost = properties.getProperty("trust.host", ConfigConstants.DEFAULT_TRUST_HOST); pdfDownloadDisable = properties.getProperty("pdf.download.disable", ConfigConstants.DEFAULT_PDF_DOWNLOAD_DISABLE); fileUploadDisable = Boolean.parseBoolean(properties.getProperty("file.upload.disable", ConfigConstants.DEFAULT_FILE_UPLOAD_DISABLE)); + tifPreviewType = properties.getProperty("tif.preview.type", ConfigConstants.DEFAULT_TIF_PREVIEW_TYPE); + ConfigConstants.setCacheEnabledValueValue(cacheEnabled); ConfigConstants.setSimTextValue(textArray); ConfigConstants.setMediaValue(mediaArray); @@ -80,6 +84,7 @@ public class ConfigRefreshComponent { ConfigConstants.setOfficePreviewSwitchDisabledValue(officePreviewSwitchDisabled); ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable); ConfigConstants.setFileUploadDisableValue(fileUploadDisable); + ConfigConstants.setTifPreviewTypeValue(tifPreviewType); setWatermarkConfig(properties); bufferedReader.close(); fileReader.close(); diff --git a/server/src/main/java/cn/keking/service/FilePreview.java b/server/src/main/java/cn/keking/service/FilePreview.java index eeb1c462..db1743d8 100644 --- a/server/src/main/java/cn/keking/service/FilePreview.java +++ b/server/src/main/java/cn/keking/service/FilePreview.java @@ -15,6 +15,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 OFD_FILE_PREVIEW_PAGE = "ofd"; String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture"; String TXT_FILE_PREVIEW_PAGE = "txt"; diff --git a/server/src/main/java/cn/keking/service/impl/TiffFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/TiffFilePreviewImpl.java index ce0799b2..018d675d 100644 --- a/server/src/main/java/cn/keking/service/impl/TiffFilePreviewImpl.java +++ b/server/src/main/java/cn/keking/service/impl/TiffFilePreviewImpl.java @@ -7,9 +7,11 @@ import cn.keking.model.ReturnResponse; import cn.keking.service.FilePreview; import cn.keking.utils.ConvertPicUtil; import cn.keking.utils.DownloadUtils; +import cn.keking.utils.WebUtils; import cn.keking.web.filter.BaseUrlFilter; import org.springframework.stereotype.Service; import org.springframework.ui.Model; +import org.springframework.util.StringUtils; import java.io.File; import java.util.ArrayList; @@ -25,6 +27,9 @@ import java.util.UUID; public class TiffFilePreviewImpl implements FilePreview { private final PictureFilePreviewImpl pictureFilePreview; + private static final String INITIALIZE_MEMORY_SIZE = "initializeMemorySize"; + //默认初始化 50MB 内存 + private static final long INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT = 1024L * 1024 * 50; private final String fileDir = ConfigConstants.getFileDir(); @@ -34,55 +39,72 @@ public class TiffFilePreviewImpl implements FilePreview { @Override public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { - String inputFileName = url.substring(url.lastIndexOf("/") + 1); - String inputFileExt = inputFileName.substring(inputFileName.lastIndexOf(".") + 1); - String uuid = UUID.randomUUID().toString().replaceAll("-",""); - String tiffFileName = uuid + "." + inputFileExt; - - ReturnResponse response = DownloadUtils.downLoad(fileAttribute, tiffFileName); - if (response.isFailure()) { - return NOT_SUPPORTED_FILE_PAGE; - } - String strTiffPath = response.getContent(); - File fileTiff = new File(strTiffPath); + String tifPreviewType = ConfigConstants.getTifPreviewType(); + + if("tif".equalsIgnoreCase(tifPreviewType)){ + + pictureFilePreview.filePreviewHandle(url,model,fileAttribute); + String fileSize = WebUtils.getUrlParameterReg(url,INITIALIZE_MEMORY_SIZE); + if(StringUtils.hasText(fileSize)){ + model.addAttribute(INITIALIZE_MEMORY_SIZE,fileSize); + }else { + model.addAttribute(INITIALIZE_MEMORY_SIZE,Long.toString(INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT)); + } + return TIFF_FILE_PREVIEW_PAGE; - File fileJpg = ConvertPicUtil.convertPic2Jpg(strTiffPath, fileDir + uuid + ".jpg"); + }else if("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)){ + String inputFileName = url.substring(url.lastIndexOf("/") + 1); + String inputFileExt = inputFileName.substring(inputFileName.lastIndexOf(".") + 1); + String uuid = UUID.randomUUID().toString().replaceAll("-",""); + String tiffFileName = uuid + "." + inputFileExt; - if(fileJpg.exists()){ - // 转换后的tif没用了,可以删掉了 - fileTiff.delete(); + ReturnResponse response = DownloadUtils.downLoad(fileAttribute, tiffFileName); + if (response.isFailure()) { + return NOT_SUPPORTED_FILE_PAGE; + } + String strTiffPath = response.getContent(); - String baseUrl = BaseUrlFilter.getBaseUrl(); - if("pdf".equalsIgnoreCase(fileAttribute.getOfficePreviewType())){ - File filePdf = ConvertPicUtil.convertJpg2Pdf(fileDir + uuid + ".jpg", fileDir + uuid + ".pdf"); - if(filePdf.exists()){ - // 转换后的jpg没用了,可以删掉了 - fileJpg.delete(); + File fileTiff = new File(strTiffPath); - String pdfUrl = baseUrl + uuid + ".pdf"; - model.addAttribute("pdfUrl", pdfUrl); + File fileJpg = ConvertPicUtil.convertPic2Jpg(strTiffPath, fileDir + uuid + ".jpg"); - return PDF_FILE_PREVIEW_PAGE; - } - }else{ - String jpgUrl = baseUrl + uuid + ".jpg"; + if(fileJpg.exists()){ + // 转换后的tif没用了,可以删掉了 + fileTiff.delete(); - fileAttribute.setName(uuid + ".jpg"); - fileAttribute.setType(FileType.PICTURE); - fileAttribute.setSuffix("jpg"); - fileAttribute.setUrl(jpgUrl); + String baseUrl = BaseUrlFilter.getBaseUrl(); + if("pdf".equalsIgnoreCase(tifPreviewType)){ + File filePdf = ConvertPicUtil.convertJpg2Pdf(fileDir + uuid + ".jpg", fileDir + uuid + ".pdf"); + if(filePdf.exists()){ + // 转换后的jpg没用了,可以删掉了 + fileJpg.delete(); - List imgUrls = new ArrayList<>(); - imgUrls.add(jpgUrl); + String pdfUrl = baseUrl + uuid + ".pdf"; + model.addAttribute("pdfUrl", pdfUrl); + + return PDF_FILE_PREVIEW_PAGE; + } + }else{ + String jpgUrl = baseUrl + uuid + ".jpg"; + + fileAttribute.setName(uuid + ".jpg"); + fileAttribute.setType(FileType.PICTURE); + fileAttribute.setSuffix("jpg"); + fileAttribute.setUrl(jpgUrl); + + List imgUrls = new ArrayList<>(); + imgUrls.add(jpgUrl); + + model.addAttribute("imgUrls", imgUrls); + model.addAttribute("currentUrl", jpgUrl); + } - model.addAttribute("imgUrls", imgUrls); - model.addAttribute("currentUrl", jpgUrl); } + return PICTURE_FILE_PREVIEW_PAGE; } - return PICTURE_FILE_PREVIEW_PAGE; - + return NOT_SUPPORTED_FILE_PAGE; } }