feat: 在线编辑文件改为文件服务器存储
This commit is contained in:
-4
@@ -84,16 +84,12 @@ public class OSSFileController {
|
||||
OSSFile file = ossFileService.getById(id);
|
||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(OSSFile::getBindId, id);
|
||||
OSSFile pdfFile = ossFileService.getOne(queryWrapper);
|
||||
if (file == null) {
|
||||
return Result.error("未找到对应实体");
|
||||
}
|
||||
else {
|
||||
result.setResult(file);
|
||||
result.setSuccess(true);
|
||||
if (pdfFile != null) {
|
||||
file.setPdfId(pdfFile.getId());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -62,6 +62,9 @@ public class OSSFile extends JeroEntity {
|
||||
@ApiModelProperty(value = "docx文件对应的pdf文件 使用此字段绑定docx的版本")
|
||||
private String bindId;
|
||||
|
||||
@ApiModelProperty(value = "onlyOffice关联Id 每次在线编辑生成一个")
|
||||
private String onlId;
|
||||
|
||||
@TableField(exist = false)
|
||||
@ApiModelProperty(value = "PDF版本对应的文件Id")
|
||||
private String pdfId;
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.jero.modules.oss.entity.OSSFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@@ -33,4 +34,5 @@ public interface IOSSFileService extends IService<OSSFile> {
|
||||
|
||||
Result<OSSFile> commonUpload(MultipartFile file, String bizPath);
|
||||
|
||||
void downloadAndViewWithWaterMark(String id, HttpServletResponse response);
|
||||
}
|
||||
|
||||
+106
@@ -1,5 +1,6 @@
|
||||
package com.jero.modules.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.jero.common.api.vo.Result;
|
||||
@@ -8,6 +9,7 @@ import com.jero.common.constant.enums.LanguageEnum;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.system.vo.LoginUser;
|
||||
import com.jero.common.util.CommonUtils;
|
||||
import com.jero.common.util.IntekeyUtils;
|
||||
import com.jero.common.util.MinioUtil;
|
||||
import com.jero.common.util.oConvertUtils;
|
||||
import com.jero.common.util.obs.ObsBootUtil;
|
||||
@@ -16,16 +18,25 @@ import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.mapper.OSSFileMapper;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.util.MyStringUtils;
|
||||
import com.jero.modules.system.util.SysWaterMarkUtil;
|
||||
import me.zhyd.oauth.utils.UuidUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
@Service("ossFileService")
|
||||
@@ -49,6 +60,11 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
|
||||
@Value(value = "${jero.splitUrl}")
|
||||
private String splitUrl;
|
||||
|
||||
@Resource
|
||||
private SysWaterMarkUtil sysWaterMarkUtil;
|
||||
|
||||
private static final String FILE_VIEW_ERROR = "预览文件失败";
|
||||
|
||||
@Override
|
||||
public void upload(MultipartFile multipartFile) throws IOException {
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
@@ -405,4 +421,94 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadAndViewWithWaterMark(@PathVariable String id, HttpServletResponse response) {
|
||||
// 查询数据表数据是否存在
|
||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(OSSFile::getId, id);
|
||||
OSSFile ossFile = this.getOne(queryWrapper);
|
||||
if (null == ossFile) {
|
||||
throw new JeroBootException("文件不存在..");
|
||||
}
|
||||
String fileUrl = ossFile.getUrl();
|
||||
// minio 下载
|
||||
// 通过MinioUtil查询时 只需要桶后面的路径
|
||||
String minioUrl = MinioUtil.getMinioUrl();
|
||||
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
|
||||
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
|
||||
String url = fileUrl.replace(minioUrl, "");
|
||||
// 文件名称
|
||||
String fileName = ossFile.getFileName();
|
||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||
// 设置强制下载不打开
|
||||
response.setContentType("application/force-download");
|
||||
// 然后在您的controller方法中:
|
||||
InputStream watermarkedInputStream = null;
|
||||
if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
|
||||
try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
if (inputStream == null) {
|
||||
return;
|
||||
}
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
response.setStatus(404);
|
||||
}
|
||||
}else if(CommonConstant.UPLOAD_TYPE_OBS.equals(uploadType)) {
|
||||
// 华为云 下载
|
||||
if ("pdf".equalsIgnoreCase(FileUtil.extName(fileName))) {
|
||||
try (InputStream originalInputStream = ObsBootUtil.getOssFile(fileUrl);
|
||||
OutputStream outputStream = response.getOutputStream()) {
|
||||
if (originalInputStream == null) {
|
||||
return;
|
||||
}
|
||||
MultipartFile mFile = new MockMultipartFile(fileName, fileName,
|
||||
ContentType.APPLICATION_OCTET_STREAM.toString(), originalInputStream);
|
||||
InputStream inputStream = IntekeyUtils.getInputStreamByDecryptFile(mFile);
|
||||
watermarkedInputStream = sysWaterMarkUtil.addWatermarkToPdf(inputStream);
|
||||
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = watermarkedInputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("error:",e);
|
||||
response.setStatus(404);
|
||||
}finally {
|
||||
try {
|
||||
if (watermarkedInputStream != null){
|
||||
watermarkedInputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try (InputStream inputStream = ObsBootUtil.getOssFile(fileUrl);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
} catch (Exception e) {
|
||||
log.error(FILE_VIEW_ERROR + e.getMessage());
|
||||
response.setStatus(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user