fix(公告): 新增公告图片预览支持

This commit is contained in:
MAC 2022-01-01 21:12:12 +08:00
parent 2ffb05cae9
commit c0f1364ad8
10 changed files with 154 additions and 15 deletions

View File

@ -18,6 +18,7 @@ public interface IFiletransferService {
void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO);
void previewFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO);
void previewPictureFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO);
void deleteFile(FileBean fileBean);
Long selectStorageSizeByUserId(Long userId);

View File

@ -299,8 +299,12 @@ public class FileDealComp {
public boolean checkAuthDownloadAndPreview(String shareBatchNum,
String extractionCode,
String token,
long userFileId) {
Long userFileId,
Integer platform) {
log.debug("权限检查开始shareBatchNum:{}, extractionCode:{}, token:{}, userFileId{}" , shareBatchNum, extractionCode, token, userFileId);
if (platform != null && platform == 2) {
return true;
}
UserFile userFile = userFileService.getById(userFileId);
log.debug(JSON.toJSONString(userFile));
if ("undefined".equals(shareBatchNum) || StringUtils.isEmpty(shareBatchNum)) {

View File

@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
@ -107,11 +108,18 @@ public class FiletransferController {
@Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"})
@MyLog(operation = "下载文件", module = CURRENT_MODULE)
@RequestMapping(value = "/downloadfile", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
public void downloadFile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
Cookie[] cookieArr = httpServletRequest.getCookies();
String token = "";
for (Cookie cookie : cookieArr) {
if ("token".equals(cookie.getName())) {
token = cookie.getValue();
}
}
boolean authResult = fileDealComp.checkAuthDownloadAndPreview(downloadFileDTO.getShareBatchNum(),
downloadFileDTO.getExtractionCode(),
downloadFileDTO.getToken(),
downloadFileDTO.getUserFileId());
token,
downloadFileDTO.getUserFileId(), null);
if (!authResult) {
log.error("没有权限下载!!!");
return;
@ -139,11 +147,26 @@ public class FiletransferController {
@Operation(summary="预览文件", description="用于文件预览", tags = {"filetransfer"})
@GetMapping("/preview")
public void preview(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PreviewDTO previewDTO){
if (previewDTO.getPlatform() != null && previewDTO.getPlatform() == 2) {
filetransferService.previewPictureFile(httpServletResponse, previewDTO);
return ;
}
Cookie[] cookieArr = httpServletRequest.getCookies();
String token = "";
for (Cookie cookie : cookieArr) {
if ("token".equals(cookie.getName())) {
token = cookie.getValue();
}
}
UserFile userFile = userFileService.getById(previewDTO.getUserFileId());
boolean authResult = fileDealComp.checkAuthDownloadAndPreview(previewDTO.getShareBatchNum(),
previewDTO.getExtractionCode(),
previewDTO.getToken(),
previewDTO.getUserFileId());
token,
previewDTO.getUserFileId(),
previewDTO.getPlatform());
if (!authResult) {
log.error("没有权限预览!!!");
@ -198,7 +221,6 @@ public class FiletransferController {
filetransferService.previewFile(httpServletResponse, previewDTO);
}
@Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"})

View File

@ -0,0 +1,60 @@
package com.qiwenshare.file.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import javax.persistence.*;
/**
* @author MAC
* @version 1.0
* @description: TODO
* @date 2022/1/1 19:06
*/
@Data
@Table(name = "picturefile")
@Entity
@TableName("picturefile")
public class PictureFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@TableId(type = IdType.AUTO)
@Column(columnDefinition="bigint(20)")
private Long pictureFileId;
@Column(columnDefinition="varchar(500) comment '文件url'")
private String fileUrl;
@Column(columnDefinition="bigint(10) comment '文件大小'")
private Long fileSize;
@Column(columnDefinition="int(1) comment '存储类型'")
private Integer storageType;
@Column(columnDefinition = "bigint(20) comment '用户id'")
private Long userId;
@Column(columnDefinition="varchar(100) comment '文件名'")
private String fileName;
@Column(columnDefinition="varchar(100) comment '扩展名'")
private String extendName;
@Column(columnDefinition="varchar(25) comment '创建时间'")
private String createTime;
@Column(columnDefinition="bigint(20) comment '创建用户id'")
private Long createUserId;
@Column(columnDefinition="varchar(25) comment '修改时间'")
private String modifyTime;
@Column(columnDefinition="bigint(20) comment '修改用户id'")
private Long modifyUserId;
}

View File

@ -7,7 +7,6 @@ import lombok.Data;
@Schema(name = "下载文件DTO",required = true)
public class DownloadFileDTO {
private long userFileId;
private String token;
@Schema(description="批次号")
private String shareBatchNum;
@Schema(description="提取码")

View File

@ -7,10 +7,11 @@ import lombok.Data;
@Schema(name = "预览文件DTO",required = true)
public class PreviewDTO {
private Long userFileId;
private String token;
@Schema(description="批次号")
private String shareBatchNum;
@Schema(description="提取码")
private String extractionCode;
private String isMin;
private Integer platform;
private String url;
}

View File

@ -0,0 +1,7 @@
package com.qiwenshare.file.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiwenshare.file.domain.PictureFile;
public interface PictureFileMapper extends BaseMapper<PictureFile> {
}

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.MimeUtils;
import com.qiwenshare.file.api.IFiletransferService;
import com.qiwenshare.file.component.FileDealComp;
import com.qiwenshare.file.config.security.user.JwtUser;
@ -40,6 +41,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -70,6 +72,8 @@ public class FiletransferService implements IFiletransferService {
@Resource
ImageMapper imageMapper;
@Resource
PictureFileMapper pictureFileMapper;
@Override
@ -387,15 +391,55 @@ public class FiletransferService implements IFiletransferService {
previewer.imageOriginalPreview(httpServletResponse, previewFile);
}
} catch (Exception e){
//org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接
if (e.getMessage().contains("ClientAbortException")) {
//该异常忽略不做处理
} else {
log.error("预览文件出现异常:{}", e.getMessage());
//org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接
if (e.getMessage().contains("ClientAbortException")) {
//该异常忽略不做处理
} else {
log.error("预览文件出现异常:{}", e.getMessage());
}
}
}
@Override
public void previewPictureFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO) {
byte[] bytesUrl = Base64.getDecoder().decode(previewDTO.getUrl());
PictureFile pictureFile = new PictureFile();
pictureFile.setFileUrl(new String(bytesUrl));
pictureFile = pictureFileMapper.selectOne(new QueryWrapper<>(pictureFile));
Previewer previewer = ufopFactory.getPreviewer(pictureFile.getStorageType());
if (previewer == null) {
log.error("预览失败文件存储类型不支持预览storageType:{}", pictureFile.getStorageType());
throw new UploadException("预览失败");
}
PreviewFile previewFile = new PreviewFile();
previewFile.setFileUrl(pictureFile.getFileUrl());
previewFile.setFileSize(pictureFile.getFileSize());
try {
String mime= MimeUtils.getMime(pictureFile.getExtendName());
httpServletResponse.setHeader("Content-Type", mime);
String fileName = pictureFile.getFileName() + "." + pictureFile.getExtendName();
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpServletResponse.addHeader("Content-Disposition", "fileName=" + fileName);// 设置文件名
previewer.imageOriginalPreview(httpServletResponse, previewFile);
} catch (Exception e){
//org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接
if (e.getMessage().contains("ClientAbortException")) {
//该异常忽略不做处理
} else {
log.error("预览文件出现异常:{}", e.getMessage());
}
}
}
@Override

View File

@ -48,6 +48,7 @@ public class UserService extends ServiceImpl<UserMapper, UserBean> implements IU
return null;
}
token = token.replace("Bearer ", "");
token = token.replace("Bearer%20", "");
try {
c = jwtComp.parseJWT(token);
} catch (Exception e) {

View File

@ -10,7 +10,7 @@ insert into sysparam (sysParamId, sysParamKey, sysParamValue, sysParamDesc) valu
insert into sysparam (sysParamId, sysParamKey, sysParamValue, sysParamDesc) values (2, 'initDataFlag', '1', '系统初始化数据标识');
insert into sysparam (sysParamId, sysParamKey, sysParamValue, sysParamDesc) values (3, 'version', '1.1.2', '当前脚本的版本号');
delete from filetype where fileTypeId in (0, 1, 2, 3, 4, 5)
delete from filetype where fileTypeId in (0, 1, 2, 3, 4, 5);
INSERT INTO `filetype` (`fileTypeId`, `fileTypeName`) VALUES (0, '全部');
INSERT INTO `filetype` (`fileTypeId`, `fileTypeName`) VALUES (1, '图片');
INSERT INTO `filetype` (`fileTypeId`, `fileTypeName`) VALUES (2, '文档');