feat: 预览接口支持token校验用户是否有企标权限
This commit is contained in:
+8
-11
@@ -1,6 +1,7 @@
|
|||||||
package com.jero.modules.system.controller;
|
package com.jero.modules.system.controller;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
@@ -12,6 +13,7 @@ import com.jero.common.util.*;
|
|||||||
import com.jero.modules.oss.entity.OSSFile;
|
import com.jero.modules.oss.entity.OSSFile;
|
||||||
import com.jero.modules.oss.service.IOSSFileService;
|
import com.jero.modules.oss.service.IOSSFileService;
|
||||||
import com.jero.modules.system.util.SysWaterMarkUtil;
|
import com.jero.modules.system.util.SysWaterMarkUtil;
|
||||||
|
import com.jero.modules.system.util.UserUtils;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -155,17 +157,7 @@ public class CommonController {
|
|||||||
downloadAndViewWithWaterMark(id, response);
|
downloadAndViewWithWaterMark(id, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 预览图片
|
|
||||||
*
|
|
||||||
* @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 downloadAndView(@PathVariable String id, HttpServletResponse response) {
|
private void downloadAndView(@PathVariable String id, HttpServletResponse response) {
|
||||||
// 查询数据表数据是否存在
|
// 查询数据表数据是否存在
|
||||||
@@ -251,6 +243,11 @@ public class CommonController {
|
|||||||
if (null == ossFile) {
|
if (null == ossFile) {
|
||||||
throw new JeroBootException("文件不存在..");
|
throw new JeroBootException("文件不存在..");
|
||||||
}
|
}
|
||||||
|
// 如果是企标的文件则需要判断当前登录人是否有查看权限,如果没有则需要返回没有权限的文件
|
||||||
|
if (StrUtil.isBlank(ossFile.getStandardId())) {
|
||||||
|
String userId = UserUtils.getUserId();
|
||||||
|
|
||||||
|
}
|
||||||
String fileUrl = ossFile.getUrl();
|
String fileUrl = ossFile.getUrl();
|
||||||
// minio 下载
|
// minio 下载
|
||||||
// 通过MinioUtil查询时 只需要桶后面的路径
|
// 通过MinioUtil查询时 只需要桶后面的路径
|
||||||
|
|||||||
+3
-4
@@ -7,14 +7,13 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
|
|
||||||
import com.jero.common.api.vo.Result;
|
import com.jero.common.api.vo.Result;
|
||||||
import com.jero.common.exception.JeroBootException;
|
import com.jero.common.exception.JeroBootException;
|
||||||
import com.jero.common.util.MessageUtils;
|
|
||||||
import com.jero.modules.laws.common.constant.FieldCommon;
|
import com.jero.modules.laws.common.constant.FieldCommon;
|
||||||
import com.jero.common.api.vo.ResultCommon;
|
import com.jero.common.api.vo.ResultCommon;
|
||||||
import com.jero.modules.laws.common.util.CurrentUserUtil;
|
import com.jero.modules.laws.common.util.CurrentUserUtil;
|
||||||
import com.jero.modules.laws.dataCenter.entity.LawsDataCenterFile;
|
import com.jero.modules.laws.dataCenter.entity.LawsDataCenterFile;
|
||||||
import com.jero.modules.laws.dataCenter.service.ILawsDataCenterFileService;
|
import com.jero.modules.laws.dataCenter.service.ILawsDataCenterFileService;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.jero.modules.system.controller.CommonController;
|
import com.jero.modules.sys.controller.SysCommonController;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.jero.common.system.base.controller.JeroController;
|
import com.jero.common.system.base.controller.JeroController;
|
||||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||||
@@ -42,7 +41,7 @@ public class LawsDataCenterFileController extends JeroController<LawsDataCenterF
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ILawsDataCenterFileService lawsDataCenterFileService;
|
private ILawsDataCenterFileService lawsDataCenterFileService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private CommonController commonController;
|
private SysCommonController sysCommonController;
|
||||||
|
|
||||||
private final static String permissionFlag = "profileCenter:table"; // 权限标识
|
private final static String permissionFlag = "profileCenter:table"; // 权限标识
|
||||||
|
|
||||||
@@ -165,7 +164,7 @@ public class LawsDataCenterFileController extends JeroController<LawsDataCenterF
|
|||||||
throw new JeroBootException(ResultCommon.NO_PERMISSION);
|
throw new JeroBootException(ResultCommon.NO_PERMISSION);
|
||||||
}
|
}
|
||||||
// 下载
|
// 下载
|
||||||
commonController.view(file.getFileId(), request, response);
|
sysCommonController.view(file.getFileId(), request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -42,7 +42,7 @@ public interface EnterpriseStandardAuthUserService extends IService<EnterpriseSt
|
|||||||
*/
|
*/
|
||||||
void delFlagSet(List<Map<String, Object>> records);
|
void delFlagSet(List<Map<String, Object>> records);
|
||||||
|
|
||||||
boolean detailPermissionByUser(String standardNumber, String userId, String user);
|
boolean detailPermissionByUser(String standardId, String standardNumber, String userId);
|
||||||
|
|
||||||
boolean verifyESDeleteAuth(String ids, String userId);
|
boolean verifyESDeleteAuth(String ids, String userId);
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user