feat: 预览接口支持token校验用户是否有企标权限
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
package com.jero.modules.sys.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.util.MinioUtil;
|
||||
import com.jero.modules.laws.enterprise.service.EnterpriseStandardAuthUserService;
|
||||
import com.jero.modules.laws.enterprise.util.WaterMarkUtil;
|
||||
import com.jero.modules.laws.standard.entity.LawsEnterpriseStandard;
|
||||
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.util.UserUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author: Mzaxd
|
||||
* @Date: 2024/1/10 15:35
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sys/common")
|
||||
public class SysCommonController {
|
||||
|
||||
@Resource
|
||||
private WaterMarkUtil waterMarkUtil;
|
||||
|
||||
@Resource
|
||||
private IOSSFileService ossFileService;
|
||||
|
||||
@Resource
|
||||
private ILawsEnterpriseStandardService esService;
|
||||
|
||||
@Resource
|
||||
private EnterpriseStandardAuthUserService authUserService;
|
||||
|
||||
/**
|
||||
* 预览图片
|
||||
*
|
||||
* @param id 传入文件id
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
@GetMapping(value = "/view/{id}")
|
||||
public void view(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
|
||||
downloadAndViewWithWaterMark(id, response);
|
||||
}
|
||||
|
||||
private void downloadAndViewWithWaterMark(@PathVariable String id, HttpServletResponse response) {
|
||||
// 查询数据表数据是否存在
|
||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(OSSFile::getId, id);
|
||||
OSSFile ossFile = ossFileService.getOne(queryWrapper);
|
||||
if (null == ossFile) {
|
||||
throw new JeroBootException("文件不存在..");
|
||||
}
|
||||
// 如果是企标的文件则需要判断当前登录人是否有查看权限,如果没有则需要返回没有权限的文件
|
||||
String standardId = ossFile.getStandardId();
|
||||
if (StrUtil.isNotBlank(standardId)) {
|
||||
LawsEnterpriseStandard es = esService.getById(standardId);
|
||||
if (es != null) {
|
||||
String userId = UserUtils.getUserId();
|
||||
boolean auth = authUserService.detailPermissionByUser(standardId, null, userId);
|
||||
if (!auth) {
|
||||
// 返回固定无权限pdf模板
|
||||
try (InputStream inputStream = new ClassPathResource("pdfTemplate/暂无该文件查看权限.pdf").getInputStream();
|
||||
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(e.getMessage());
|
||||
response.setStatus(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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方法中:
|
||||
if ("pdf".equalsIgnoreCase(FileUtil.extName(fileName))) {
|
||||
try (InputStream originalInputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
|
||||
InputStream watermarkedInputStream = waterMarkUtil.addWatermarkToPdf(originalInputStream);
|
||||
OutputStream outputStream = response.getOutputStream()) {
|
||||
|
||||
if (originalInputStream == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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(e.getMessage());
|
||||
response.setStatus(404);
|
||||
}
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user