feat: 所有文档文件转换pdf接口

This commit is contained in:
2024-06-03 15:24:50 +08:00
parent abb8f2db1c
commit 340b766d5e
6 changed files with 159 additions and 3 deletions
@@ -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));
}
}
@@ -0,0 +1,12 @@
package com.jero.modules.docking.file.serviec;
public interface FileService {
/**
* 所有历史doc和docx文件生成pdf版本
* @return
*/
String allDocFileTransferPdf(String fileId);
}
@@ -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();
}
}