add 新增需要token的下载接口

This commit is contained in:
lijiarao
2021-10-28 20:25:23 +08:00
parent ca00bf1995
commit 5310c6dbe7
9 changed files with 229 additions and 3 deletions
@@ -0,0 +1,59 @@
package com.jero.modules.oss.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
/**
* @Description: pay_file_download_record
* @Author: jeecg-boot
* @Date: 2021-08-30
* @Version: V1.0
*/
@Data
@TableName("pay_file_download_record")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="pay_file_download_record对象", description="pay_file_download_record")
public class FileDownloadRecord implements Serializable {
private static final long serialVersionUID = 1L;
/**主键*/
@TableId(type = IdType.ASSIGN_ID)
@ApiModelProperty(value = "主键")
private String id;
@ApiModelProperty(value = "文件id")
private String fileId;
/**用户id*/
@Excel(name = "用户id", width = 15)
@ApiModelProperty(value = "用户id")
private String userId;
/**创建人*/
@ApiModelProperty(value = "创建人")
private String createBy;
/**创建时间*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
@Excel(name = "下载时间", width = 15,exportFormat = "yyyy-MM-dd")
@ApiModelProperty(value = "创建时间")
private java.util.Date createTime;
/**更新人*/
@ApiModelProperty(value = "更新人")
private String updateBy;
/**更新时间*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
@ApiModelProperty(value = "更新时间")
private java.util.Date updateTime;
}
@@ -0,0 +1,16 @@
package com.jero.modules.oss.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jero.modules.oss.entity.FileDownloadRecord;
/**
* @Description: pay_file_download_record
* @Author: jeecg-boot
* @Date: 2021-08-30
* @Version: V1.0
*/
public interface FileDownloadRecordMapper extends BaseMapper<FileDownloadRecord> {
}
@@ -4,9 +4,13 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jero.modules.oss.entity.OSSFile;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
public interface OSSFileMapper extends BaseMapper<OSSFile> {
/**
* 根据文件id获取文件名称
*/
String findFileNameByFileId(@Param("fileId")String fileId);
String findFileNameByFileId(@Param("fileId") String fileId);
void saveDownloadTimes(@Param("userId") String userId, @Param("fileId") String fileId, @Param("date") Date date);
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jero.modules.oss.mapper.FileDownloadRecordMapper">
</mapper>
@@ -12,4 +12,7 @@
a.id = #{fileId}
</select>
<insert id="saveDownloadTimes">
insert into pay_file_download_record(id, file_id, file_name, file_path, company_name, download_name, meeting_name, create_by, create_time, update_by, update_time)
</insert>
</mapper>
@@ -17,4 +17,8 @@ public interface IOSSFileService extends IService<OSSFile> {
* @param ossFile
*/
void updateAndPush(OSSFile ossFile);
void saveDownloadTimes(String fileId,String userId);
Integer searchDownloadTimes(String fileId, String userId);
}
@@ -1,20 +1,25 @@
package com.jero.modules.oss.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.util.AuthenticationUtils;
import com.jero.common.util.CommonUtils;
import com.jero.common.util.oss.OssBootUtil;
import com.jero.modules.oss.entity.FileDownloadRecord;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.mapper.OSSFileMapper;
import com.jero.modules.oss.mapper.FileDownloadRecordMapper;
import com.jero.modules.oss.service.IOSSFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
@Service("ossFileService")
public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> implements IOSSFileService {
@Resource
private FileDownloadRecordMapper fileDownloadRecordMapper;
@Override
public void upload(MultipartFile multipartFile) throws IOException {
String fileName = multipartFile.getOriginalFilename();
@@ -47,4 +52,20 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
AuthenticationUtils.post("/oss/file/updateAndPush",null,ossFile);
}
@Override
public void saveDownloadTimes(String fileId,String userId) {
FileDownloadRecord fileDownloadRecord = new FileDownloadRecord();
fileDownloadRecord.setFileId(fileId);
fileDownloadRecord.setUserId(userId);
fileDownloadRecordMapper.insert(fileDownloadRecord);
}
@Override
public Integer searchDownloadTimes(String fileId, String userId) {
QueryWrapper<FileDownloadRecord> wrapper = new QueryWrapper<>();
wrapper.eq("user_id",userId);
wrapper.eq("file_id",fileId);
return fileDownloadRecordMapper.selectCount(wrapper);
}
}
@@ -169,6 +169,11 @@ public class CommonController {
}
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
response.setContentType("application/force-download");// 设置强制下载不打开
if (type == null) {
if (fileName.contains(".jpg") || fileName.contains(".png") || fileName.contains(".gif") || fileName.contains(".jpeg")) {
response.setContentType("image/png;image/jpg;image/gif;image/jpeg");
}
}
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
@@ -217,6 +222,115 @@ public class CommonController {
}
/**
* 预览图片&下载文件
*
* @param id 传入文件id
* @param request
* @param response
*/
@GetMapping(value = "/userDownload/{id}")
public void download(@PathVariable String id,HttpServletRequest request, HttpServletResponse response,String type) {
// 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId,id);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){
throw new JeroBootException("文件不存在..");
}
String fileUrl = ossFile.getUrl();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String fileName = "";
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
//本地下载
String filePath = uploadpath + File.separator + fileUrl;
File file = new File(filePath);
if(!file.exists()){
response.setStatus(404);
throw new RuntimeException("文件不存在..");
}
// 文件名称
fileName = file.getName();
inputStream = new BufferedInputStream(new FileInputStream(filePath));
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
// minio 下载
// 通过MinioUtil查询时 只需要桶后面的路径
String minioUrl = MinioUtil.getMinioUrl();
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = fileUrl.replace(minioUrl, "");
// 文件名称
fileName = ossFile.getFileName();
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
}
ossFileService.updateById(ossFile);
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
if (loginUser.getIsCompanyUser()) {
Integer integer = ossFileService.searchDownloadTimes(ossFile.getId(), loginUser.getId());
if (integer >= 5) {
throw new JeroBootException("超出下载次数限制");
}
}
ossFileService.saveDownloadTimes(ossFile.getId(), loginUser.getId());
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
response.setContentType("application/force-download");// 设置强制下载不打开
if (type == null) {
if (fileName.contains(".jpg") || fileName.contains(".png") || fileName.contains(".gif") || fileName.contains(".jpeg")) {
response.setContentType("image/png;image/jpg;image/gif;image/jpeg");
}
}
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
//统计下载和上传次数
//if ("download".equals(type)) {
Integer downloadNumber = ossFile.getDownloadNumber();
if (downloadNumber == null) {
ossFile.setDownloadNumber(0);
} else {
ossFile.setDownloadNumber(downloadNumber + 1);
}
//} else if ("view".equals(type)) {
// Integer viewNumber = ossFile.getViewNumber();
// if (viewNumber == null) {
// ossFile.setViewNumber(0);
// } else {
// ossFile.setViewNumber(viewNumber + 1);
// }
// ossFileService.updateAndPush(ossFile);
//}
} catch (IOException e) {
log.error("预览文件失败" + e.getMessage());
response.setStatus(404);
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
/**
* @功能:pdf预览Iframe
* @param modelAndView