feat: 所有文档文件转换pdf接口
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package com.jero.modules.docking.file.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||||
|
import com.jero.common.api.vo.Result;
|
||||||
|
import com.jero.common.aspect.annotation.AutoLog;
|
||||||
|
import com.jero.modules.docking.file.serviec.FileService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@Api(tags = "文件")
|
||||||
|
@RequestMapping("/file")
|
||||||
|
public class FileController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FileService fileService;
|
||||||
|
|
||||||
|
@GetMapping("/allDocFileTransferPdf")
|
||||||
|
@AutoLog(value = "文件-所有历史doc和docx文件生成pdf版本")
|
||||||
|
@ApiOperation(value="文件-所有历史doc和docx文件生成pdf版本", notes="文件-所有历史doc和docx文件生成pdf版本")
|
||||||
|
@ApiOperationSupport(order = 100)
|
||||||
|
public Result<?> allDocFileTransferPdf(String fileId) {
|
||||||
|
return Result.OK(fileService.allDocFileTransferPdf(fileId));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package com.jero.modules.docking.file.serviec;
|
||||||
|
|
||||||
|
public interface FileService {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 所有历史doc和docx文件生成pdf版本
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String allDocFileTransferPdf(String fileId);
|
||||||
|
|
||||||
|
}
|
||||||
+108
@@ -0,0 +1,108 @@
|
|||||||
|
package com.jero.modules.docking.file.serviec.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.jero.common.util.IntekeyUtils;
|
||||||
|
import com.jero.common.util.MinioUtil;
|
||||||
|
import com.jero.modules.docking.file.serviec.FileService;
|
||||||
|
import com.jero.modules.onlyoffice.utils.OnlyOfficePdfUtil;
|
||||||
|
import com.jero.modules.oss.entity.OSSFile;
|
||||||
|
import com.jero.modules.oss.service.IOSSFileService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class FileServiceImpl implements FileService {
|
||||||
|
|
||||||
|
@Value("#{'${download.enable}'}")
|
||||||
|
private boolean enable;
|
||||||
|
|
||||||
|
@Value("${file.path}")
|
||||||
|
private String filePath;//文件存储路径
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IOSSFileService ossFileService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OnlyOfficePdfUtil onlyOfficePdfUtil;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String allDocFileTransferPdf(String fileIds) {
|
||||||
|
List<OSSFile> resultList = new ArrayList<>();
|
||||||
|
// 获取所有需要生成pdf版本的文件
|
||||||
|
// 先获取所有有url的文件记录
|
||||||
|
LambdaQueryWrapper<OSSFile> ossWrapper = new LambdaQueryWrapper<>();
|
||||||
|
ossWrapper.in(StrUtil.isNotBlank(fileIds), OSSFile::getId, Arrays.asList(fileIds.split(",")));
|
||||||
|
ossWrapper.isNotNull(OSSFile::getUrl);
|
||||||
|
List<OSSFile> ossFileList = ossFileService.list(ossWrapper);
|
||||||
|
if (CollUtil.isEmpty(ossFileList)) {
|
||||||
|
return "没有需要处理的文件";
|
||||||
|
}
|
||||||
|
// 筛选出后缀为doc或docx的文件
|
||||||
|
List<OSSFile> docFileList = ossFileList.stream()
|
||||||
|
.filter(o -> o.getUrl().endsWith("doc") || o.getUrl().endsWith("docx"))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
// 筛选出已经转换过的pdf文件
|
||||||
|
Map<String, List<OSSFile>> alreadyTransferFileMap = ossFileList.stream()
|
||||||
|
.filter(o -> StrUtil.isNotBlank(o.getBindId()))
|
||||||
|
.collect(Collectors.groupingBy(OSSFile::getBindId));
|
||||||
|
for (OSSFile ossFile : docFileList){
|
||||||
|
List<OSSFile> existPdfFileList = alreadyTransferFileMap.get(ossFile.getId());
|
||||||
|
// 已经转换过的不再重复转换
|
||||||
|
if (CollUtil.isNotEmpty(existPdfFileList)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String docFileName = ossFile.getFileName();
|
||||||
|
log.info("开始处理文件名为 {} 的文件转换工作", docFileName);
|
||||||
|
// 根据文件url获取文件
|
||||||
|
InputStream isDecrypt;
|
||||||
|
try (InputStream is = MinioUtil.download(ossFile.getUrl())) {
|
||||||
|
isDecrypt = is;
|
||||||
|
// 文件解密
|
||||||
|
if (enable) {
|
||||||
|
isDecrypt = IntekeyUtils.autoDecryptInputStreamFile(isDecrypt, ossFile.getFileName());
|
||||||
|
}
|
||||||
|
// 转换为pdf
|
||||||
|
String pdfFileName = docFileName.substring(0, docFileName.lastIndexOf("."));
|
||||||
|
String targetPath = "convert/" + ossFile.getFileName();
|
||||||
|
File targetFile = new File(filePath + targetPath);
|
||||||
|
try (OutputStream outputStream = Files.newOutputStream(targetFile.toPath())) {
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = isDecrypt.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("文件写入失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
onlyOfficePdfUtil.convertAndSavePdfFile(pdfFileName, ossFile.getId(), targetPath);
|
||||||
|
// 将下载到本地的文件删除
|
||||||
|
FileUtil.del(targetFile);
|
||||||
|
log.info("文件名为 {} 的文件转换成功", docFileName);
|
||||||
|
resultList.add(ossFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("文件名为 {} 的文件转换失败,失败原因{}", docFileName, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "文件转换完成,需要转换的文件数量为: " + ossFileList.size() + "成功转换数量为: " + resultList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -427,7 +427,7 @@ public class BusProcessModelHisServiceImpl extends ServiceImpl<BusProcessModelHi
|
|||||||
// 获取上传文件对象
|
// 获取上传文件对象
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
MultipartFile multipartFile = BusProcessModelHisServiceImpl.convert(file);
|
MultipartFile multipartFile = BusProcessModelHisServiceImpl.convert(file);
|
||||||
String savePath = CommonUtils.upload(multipartFile, "", uploadType);
|
String savePath = CommonUtils.upload(multipartFile, "convert", uploadType);
|
||||||
if (oConvertUtils.isNotEmpty(savePath)) {
|
if (oConvertUtils.isNotEmpty(savePath)) {
|
||||||
// 文件名
|
// 文件名
|
||||||
String fileName = his.getFileName();
|
String fileName = his.getFileName();
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ public class OnlyOfficePdfUtil {
|
|||||||
try (InputStream content = entity.getContent()) {
|
try (InputStream content = entity.getContent()) {
|
||||||
MultipartFile file = new MockMultipartFile(pdfFileName, pdfFileName, ContentType.APPLICATION_OCTET_STREAM.toString(), content);
|
MultipartFile file = new MockMultipartFile(pdfFileName, pdfFileName, ContentType.APPLICATION_OCTET_STREAM.toString(), content);
|
||||||
// 保存文件逻辑
|
// 保存文件逻辑
|
||||||
String savePath = CommonUtils.upload(file, "", uploadType);
|
String savePath = CommonUtils.upload(file, "convert", uploadType);
|
||||||
if (oConvertUtils.isNotEmpty(savePath)) {
|
if (oConvertUtils.isNotEmpty(savePath)) {
|
||||||
// 如果有的话,删除原来的pdf版本
|
// 如果有的话,删除原来的pdf版本
|
||||||
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
|||||||
@@ -476,7 +476,7 @@ download:
|
|||||||
# 文件下载、预览拦截路径,多个以逗号分隔
|
# 文件下载、预览拦截路径,多个以逗号分隔
|
||||||
download_url: /sys/common/download/
|
download_url: /sys/common/download/
|
||||||
view_url: /sys/common/view/
|
view_url: /sys/common/view/
|
||||||
white_url: /lawsSpecialProjectLibrary/exportXls,/noEncryptDownload,/overseas/exportData,/overseas/exportDataWithFile,/overseas/templateDownload,/domestic/exportData,/domestic/exportDataWithFile,/domestic/templateDownload,/onlyOffice,/enterprise/templateDownload,/exportDataWithFile,/exportSplitInfoZip,/lawsCustomCompareInfo/exportExcel,/home/search/download,/documentSplit/downloadImportTemplate,/split/file/getImage
|
white_url: /lawsSpecialProjectLibrary/exportXls,/noEncryptDownload,/overseas/exportData,/overseas/exportDataWithFile,/overseas/templateDownload,/domestic/exportData,/domestic/exportDataWithFile,/domestic/templateDownload,/onlyOffice,/enterprise/templateDownload,/exportDataWithFile,/exportSplitInfoZip,/lawsCustomCompareInfo/exportExcel,/home/search/download,/documentSplit/downloadImportTemplate,/split/file/getImage,/allDocFileTransferPdf,/file/convert
|
||||||
dev_encrypt_url: /devEncryptDownload,/enterprise/exportData
|
dev_encrypt_url: /devEncryptDownload,/enterprise/exportData
|
||||||
# 管理员角色
|
# 管理员角色
|
||||||
admin_role_code: admin,BZGLY
|
admin_role_code: admin,BZGLY
|
||||||
|
|||||||
Reference in New Issue
Block a user