浏览代码

移除文档预览

徐玉祥 4 年之前
父节点
当前提交
462002c5a8

+ 0 - 138
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/util/LibreOfficeUtil.java

@ -1,138 +0,0 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.util;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.Log;
import cn.stylefeng.guns.core.consts.MediaTypeConstant;
import cn.stylefeng.guns.core.enums.DocumentFormatEnum;
import cn.stylefeng.guns.core.exception.LibreOfficeException;
import org.jodconverter.DocumentConverter;
import org.jodconverter.document.DocumentFormat;
import org.jodconverter.office.OfficeException;
import org.springframework.http.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * LibreOffice工具类,用于将word,excel,ppt等格式文件转为pdf预览
 *
 * @author xuyuxiang
 * @date 2020/7/6 14:55
 */
public class LibreOfficeUtil {
    private static final Log log = Log.get();
    private static DocumentConverter documentConverter;
    private static void init() {
        try {
            documentConverter = SpringUtil.getBean(DocumentConverter.class);
        } catch (Exception e) {
            throw new LibreOfficeException();
        }
    }
    /**
     * 将文件流转换为PDF流
     *
     * inputStream:输入流
     * outputStream:输入pdf流
     * fileSuffix:源文件后缀
     * 返回目标类型的contentType
     *
     * @author xuyuxiang
     * @date 2020/7/6 15:02
     */
    public static void convertToPdf(InputStream inputStream, OutputStream outputStream, String fileSuffix) {
        init();
        final DocumentFormatEnum documentFormatEnum = DocumentFormatEnum.valueOf(fileSuffix.toUpperCase());
        final DocumentFormat format = documentFormatEnum.getFormFormat();
        log.info(">>> 待转换的文档类型:{}", format);
        final DocumentFormat targetFormat = documentFormatEnum.getTargetFormat();
        log.info(">>> 转换的目标文档类型:{}", targetFormat);
        try {
            final InputStream is = documentFormatEnum.getInputStream(inputStream);
            documentConverter.convert(is).as(format).to(outputStream).as(targetFormat).execute();
        } catch (IOException | OfficeException e) {
            e.printStackTrace();
        }
        log.info(">>> 文件转换结束");
    }
    /**
     * 根据文件后缀判断是否图片
     *
     * @author xuyuxiang
     * @date 2020/7/6 15:31
     */
    public static boolean isPic(String fileSuffix) {
        return MediaTypeConstant.IMG_JPG.equals(fileSuffix)
                || MediaTypeConstant.IMG_JPEG.equals(fileSuffix)
                || MediaTypeConstant.IMG_PNG.equals(fileSuffix)
                || MediaTypeConstant.IMG_GIF.equals(fileSuffix)
                || MediaTypeConstant.IMG_TIF.equals(fileSuffix)
                || MediaTypeConstant.IMG_BMP.equals(fileSuffix);
    }
    /**
     * 根据文件后缀判断是否文档
     *
     * @author xuyuxiang
     * @date 2020/7/6 15:31
     */
    public static boolean isDoc(String fileSuffix) {
        return MediaTypeConstant.DOC_TXT.equals(fileSuffix)
                || MediaTypeConstant.DOC_DOC.equals(fileSuffix)
                || MediaTypeConstant.DOC_DOCX.equals(fileSuffix)
                || MediaTypeConstant.DOC_XLS.equals(fileSuffix)
                || MediaTypeConstant.DOC_XLSX.equals(fileSuffix)
                || MediaTypeConstant.DOC_PPT.equals(fileSuffix)
                || MediaTypeConstant.DOC_PPTX.equals(fileSuffix);
    }
    /**
     * 根据文件后缀获取转换目标类型
     *
     * @author xuyuxiang
     * @date 2020/7/6 17:03
     */
    public static String getTargetContentTypeBySuffix(String fileSuffix) {
        //如果目标类型是pdf
        if(MediaTypeConstant.DOC_TXT.equals(fileSuffix)
                || MediaTypeConstant.DOC_DOC.equals(fileSuffix)
                || MediaTypeConstant.DOC_DOCX.equals(fileSuffix)
                || MediaTypeConstant.DOC_PPT.equals(fileSuffix)
                || MediaTypeConstant.DOC_PPTX.equals(fileSuffix)) {
            return MediaType.APPLICATION_PDF_VALUE;
        } else {
            //否则是html类型
            return MediaType.TEXT_HTML_VALUE;
        }
    }
}

+ 17 - 30
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/file/service/impl/SysFileInfoServiceImpl.java

@ -32,12 +32,12 @@ import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.stylefeng.guns.core.consts.MediaTypeConstant;
import cn.stylefeng.guns.core.consts.SymbolConstant;
import cn.stylefeng.guns.core.exception.LibreOfficeException;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.util.LibreOfficeUtil;
import cn.stylefeng.guns.sys.modular.file.entity.SysFileInfo;
import cn.stylefeng.guns.sys.modular.file.enums.FileLocationEnum;
import cn.stylefeng.guns.sys.modular.file.enums.SysFileInfoExceptionEnum;
@ -240,7 +240,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
        //获取文件字节码
        fileBytes = sysFileInfoResult.getFileBytes();
        //如果是图片类型,则直接输出
        if (LibreOfficeUtil.isPic(fileSuffix)) {
        if (this.isPic(fileSuffix)) {
            try {
                //设置contentType
                response.setContentType("image/jpeg");
@ -252,34 +252,6 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
                throw new ServiceException(SysFileInfoExceptionEnum.PREVIEW_ERROR_NOT_SUPPORT);
            }
        } else if (LibreOfficeUtil.isDoc(fileSuffix)) {
            try {
                //如果是文档类型,则使用libreoffice转换为pdf或html
                InputStream inputStream = IoUtil.toStream(fileBytes);
                //获取目标contentType(word和ppt和text转成pdf,excel转成html)
                String targetContentType = LibreOfficeUtil.getTargetContentTypeBySuffix(fileSuffix);
                //设置contentType
                response.setContentType(targetContentType);
                //获取outputStream
                ServletOutputStream outputStream = response.getOutputStream();
                //转换
                LibreOfficeUtil.convertToPdf(inputStream, outputStream, fileSuffix);
                //输出
                IoUtil.write(outputStream, true, fileBytes);
            } catch (IOException e) {
                log.error(">>> 预览文件异常:{}", e.getMessage());
                throw new ServiceException(SysFileInfoExceptionEnum.PREVIEW_ERROR_NOT_SUPPORT);
            } catch (LibreOfficeException e) {
                log.error(">>> 初始化LibreOffice失败:{}", e.getMessage());
                throw new ServiceException(SysFileInfoExceptionEnum.PREVIEW_ERROR_LIBREOFFICE);
            }
        } else {
            //否则不支持预览(暂时)
            throw new ServiceException(SysFileInfoExceptionEnum.PREVIEW_ERROR_NOT_SUPPORT);
@ -319,4 +291,19 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
        return sysFileInfo;
    }
    /**
     * 根据文件后缀判断是否图片
     *
     * @author xuyuxiang
     * @date 2020/7/6 15:31
     */
    public boolean isPic(String fileSuffix) {
        return MediaTypeConstant.IMG_JPG.equals(fileSuffix)
                || MediaTypeConstant.IMG_JPEG.equals(fileSuffix)
                || MediaTypeConstant.IMG_PNG.equals(fileSuffix)
                || MediaTypeConstant.IMG_GIF.equals(fileSuffix)
                || MediaTypeConstant.IMG_TIF.equals(fileSuffix)
                || MediaTypeConstant.IMG_BMP.equals(fileSuffix);
    }
}