Merge pull request !64 from MAC/develop
This commit is contained in:
MAC 2021-07-11 11:44:57 +00:00 committed by Gitee
commit 51cd6cf6b6
6 changed files with 120 additions and 57 deletions

View File

@ -4,22 +4,25 @@ import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiwenshare.common.constant.FileConstant;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.file.api.IElasticSearchService;
import com.qiwenshare.file.api.*;
import com.qiwenshare.file.config.es.FileSearch;
import com.qiwenshare.file.domain.TreeNode;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.domain.*;
import com.qiwenshare.file.mapper.UserFileMapper;
import com.qiwenshare.file.service.UserService;
import com.qiwenshare.file.vo.file.FileListVo;
import com.qiwenshare.ufo.factory.UFOFactory;
import com.qiwenshare.ufo.operation.read.Reader;
import com.qiwenshare.ufo.operation.read.domain.ReadFile;
import com.qiwenshare.ufo.util.PathUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@ -32,6 +35,14 @@ import java.util.concurrent.Executors;
public class FileDealComp {
@Resource
UserFileMapper userFileMapper;
@Resource
IUserService userService;
@Resource
IShareService shareService;
@Resource
IShareFileService shareFileService;
@Resource
IUserFileService userFileService;
@Autowired
private IElasticSearchService elasticSearchService;
public static Executor exec = Executors.newFixedThreadPool(10);
@ -88,7 +99,9 @@ public class FileDealComp {
/**
* 还原父文件路径
*
* 回收站文件还原操作会将文件恢复到原来的路径下,当还原文件的时候如果父目录已经不存在了则需要把父母录给还原
* 1回收站文件还原操作会将文件恢复到原来的路径下,当还原文件的时候如果父目录已经不存在了则需要把父母录给还原
* 2上传目录
*
* @param filePath
* @param sessionUserId
*/
@ -267,4 +280,47 @@ public class FileDealComp {
}
/**
* 根据用户传入的参数判断是否有下载或者预览权限
* @return
*/
public boolean checkAuthDownloadAndPreview(String shareBatchNum,
String extractionCode,
String token,
long userFileId) {
UserFile userFile = userFileService.getById(userFileId);
if ("undefined".equals(shareBatchNum) || StringUtils.isEmpty(shareBatchNum)) {
UserBean sessionUserBean = userService.getUserBeanByToken(token);
if (sessionUserBean == null) {
return false;
}
if (userFile.getUserId() != sessionUserBean.getUserId()) {
return false;
}
} else {
Map<String, Object> param = new HashMap<>();
param.put("shareBatchNum", shareBatchNum);
List<Share> shareList = shareService.listByMap(param);
//判断批次号
if (shareList.size() <= 0) {
return false;
}
Integer shareType = shareList.get(0).getShareType();
if (1 == shareType) {
//判断提取码
if (!shareList.get(0).getExtractionCode().equals(extractionCode)) {
return false;
}
}
param.put("userFileId", userFileId);
List<ShareFile> shareFileList = shareFileService.listByMap(param);
if (shareFileList.size() <= 0) {
return false;
}
}
return true;
}
}

View File

@ -20,6 +20,7 @@ import com.qiwenshare.file.service.ShareService;
import com.qiwenshare.file.service.StorageService;
import com.qiwenshare.file.vo.file.FileListVo;
import com.qiwenshare.file.vo.file.UploadFileVo;
import com.qiwenshare.ufo.util.PathUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@ -84,7 +85,14 @@ public class FiletransferController {
UserFile userFile = new UserFile();
userFile.setUserId(sessionUserBean.getUserId());
String relativePath = uploadFileDto.getRelativePath();
if (StringUtils.isNotEmpty(relativePath)) {
userFile.setFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/");
fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/", sessionUserBean.getUserId());
} else {
userFile.setFilePath(uploadFileDto.getFilePath());
}
String fileName = uploadFileDto.getFilename();
userFile.setFileName(FileUtil.getFileNameNotExtend(fileName));
userFile.setExtendName(FileUtil.getFileExtendName(fileName));
@ -136,6 +144,14 @@ public class FiletransferController {
@MyLog(operation = "下载文件", module = CURRENT_MODULE)
@RequestMapping(value = "/downloadfile", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
boolean authResult = fileDealComp.checkAuthDownloadAndPreview(downloadFileDTO.getShareBatchNum(),
downloadFileDTO.getExtractionCode(),
downloadFileDTO.getToken(),
downloadFileDTO.getUserFileId());
if (!authResult) {
log.error("没有权限下载!!!");
return;
}
httpServletResponse.setContentType("application/force-download");// 设置强制下载不打开
UserFile userFile = userFileService.getById(downloadFileDTO.getUserFileId());
String fileName = "";
@ -160,40 +176,14 @@ public class FiletransferController {
@GetMapping("/preview")
public void preview(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PreviewDTO previewDTO){
UserFile userFile = userFileService.getById(previewDTO.getUserFileId());
if ("undefined".equals(previewDTO.getShareBatchNum()) || StringUtils.isEmpty(previewDTO.getShareBatchNum())) {
String token = previewDTO.getToken();
UserBean sessionUserBean = userService.getUserBeanByToken(token);
if (sessionUserBean == null) {
boolean authResult = fileDealComp.checkAuthDownloadAndPreview(previewDTO.getShareBatchNum(),
previewDTO.getExtractionCode(),
previewDTO.getToken(),
previewDTO.getUserFileId());
if (!authResult) {
log.error("没有权限预览!!!");
return;
}
if (userFile.getUserId() != sessionUserBean.getUserId()) {
return;
}
} else {
Map<String, Object> param = new HashMap<>();
param.put("shareBatchNum", previewDTO.getShareBatchNum());
List<Share> shareList = shareService.listByMap(param);
//判断批次号
if (shareList.size() <= 0) {
return;
}
Integer shareType = shareList.get(0).getShareType();
if (1 == shareType) {
//判断提取码
String extractionCode = shareList.get(0).getExtractionCode();
if (!extractionCode.equals(previewDTO.getExtractionCode())) {
return;
}
}
param.put("userFileId", previewDTO.getUserFileId());
List<ShareFile> shareFileList = shareFileService.listByMap(param);
if (shareFileList.size() <= 0) {
return;
}
}
FileBean fileBean = fileService.getById(userFile.getFileId());
String mime= MimeUtils.getMime(userFile.getExtendName());

View File

@ -8,7 +8,6 @@ import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.file.anno.MyLog;
import com.qiwenshare.file.api.IFileService;
import com.qiwenshare.file.api.IFiletransferService;
import com.qiwenshare.file.api.IUserFileService;
import com.qiwenshare.file.api.IUserService;
import com.qiwenshare.file.domain.*;
@ -17,6 +16,7 @@ import com.qiwenshare.file.dto.file.EditOfficeFileDTO;
import com.qiwenshare.file.dto.file.PreviewOfficeFileDTO;
import com.qiwenshare.file.helper.ConfigManager;
import com.qiwenshare.ufo.factory.UFOFactory;
import com.qiwenshare.ufo.operation.download.domain.DownloadFile;
import com.qiwenshare.ufo.operation.write.Writer;
import com.qiwenshare.ufo.operation.write.domain.WriteFile;
import com.qiwenshare.ufo.util.PathUtil;
@ -185,7 +185,7 @@ public class OfficeController {
FileModel file = new FileModel(userFile.getFileName() + "." + userFile.getExtendName(),
previewOfficeFileDTO.getPreviewUrl(),
String.valueOf(new Date().getTime()),
userFile.getUploadTime(),
String.valueOf(loginUser.getUserId()),
loginUser.getUsername(),
"view");
@ -226,7 +226,7 @@ public class OfficeController {
FileModel file = new FileModel(userFile.getFileName() + "." + userFile.getExtendName(),
editOfficeFileDTO.getPreviewUrl(),
String.valueOf(new Date().getTime()),
userFile.getUploadTime(),
String.valueOf(loginUser.getUserId()),
loginUser.getUsername(),
"edit");
@ -259,22 +259,19 @@ public class OfficeController {
if (loginUser == null) {
throw new NotLoginException();
}
PrintWriter writer = null;
JSONObject jsonObj=null;
writer = response.getWriter();
PrintWriter writer = response.getWriter();
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
String body = scanner.hasNext() ? scanner.next() : "";
jsonObj = JSON.parseObject(body);
JSONObject jsonObj = JSON.parseObject(body);
log.info("===saveeditedfile:" + jsonObj.get("status")); ;
String status = jsonObj!=null?jsonObj.get("status").toString():"";
String status = jsonObj != null ? jsonObj.get("status").toString() : "";
if ("2".equals(status)) {//新建报告不强制手动操作时状态为2
String type = request.getParameter("type");
String downloadUri = (String) jsonObj.get("url");
if("edit".equals(type)){//修改报告
log.debug("====文档编辑完成,现在开始保存编辑后的文档,其下载地址为:" + downloadUri);
String fileId = request.getParameter("fileId");
String userFileId = request.getParameter("userFileId");
FileBean fileBean = fileService.getById(fileId);
@ -286,7 +283,7 @@ public class OfficeController {
URL url = new URL(downloadUri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// String md5Str = "";
int fileLength = 0;
try {
InputStream stream = connection.getInputStream();
@ -295,8 +292,6 @@ public class OfficeController {
WriteFile writeFile = new WriteFile();
writeFile.setFileUrl(fileBean.getFileUrl());
log.info("当前修改文件大小为:" + fileLength);
// log.info("当前修改文件md5为" + md5Str);
writeFile.setFileSize(connection.getContentLength());
writer1.write(stream, writeFile);
} catch (Exception e) {
@ -310,10 +305,14 @@ public class OfficeController {
userFileService.update(userFileUpdateWrapper);
LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
fileLength = connection.getContentLength();
log.info("当前修改文件大小为2222" + Long.valueOf(fileLength));
log.info("当前修改文件大小为" + Long.valueOf(fileLength));
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
InputStream inputStream = ufoFactory.getDownloader(fileBean.getStorageType()).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream);
lambdaUpdateWrapper
// .set(FileBean::getIdentifier, md5Str)
.set(FileBean::getIdentifier, md5Str)
.set(FileBean::getFileSize, Long.valueOf(fileLength))
.eq(FileBean::getFileId, fileId);
fileService.update(lambdaUpdateWrapper);

View File

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

View File

@ -24,6 +24,8 @@ public class UploadFileDTO {
@Schema(description = "切片大小")
private long chunkSize;
@Schema(description = "相对路径")
private String relativePath;
@Schema(description = "所有切片")
private int totalChunks;

View File

@ -15,6 +15,8 @@ import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qiwenshare.common.constant.FileConstant;
import com.qiwenshare.common.operation.FileOperation;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.FileUtil;
@ -45,6 +47,7 @@ import com.qiwenshare.ufo.operation.upload.Uploader;
import com.qiwenshare.ufo.operation.upload.domain.UploadFile;
import com.qiwenshare.ufo.util.PathUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -97,7 +100,13 @@ public class FiletransferService implements IFiletransferService {
fileBean.setPointCount(1);
fileMapper.insert(fileBean);
UserFile userFile = new UserFile();
String relativePath = uploadFileDto.getRelativePath();
if (StringUtils.isNotEmpty(relativePath)) {
userFile.setFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/");
fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/", userId);
} else {
userFile.setFilePath(uploadFileDto.getFilePath());
}
userFile.setUserId(userId);
userFile.setFileName(uploadFile.getFileName());
userFile.setExtendName(uploadFile.getFileType());
@ -135,7 +144,7 @@ public class FiletransferService implements IFiletransferService {
downloadFile.setFileUrl(fileBean.getFileUrl());
downloadFile.setFileSize(fileBean.getFileSize());
httpServletResponse.setContentLengthLong(fileBean.getFileSize());
downloader.download(httpServletResponse, downloadFile);
} else {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
@ -147,9 +156,9 @@ public class FiletransferService implements IFiletransferService {
String staticPath = PathUtil.getStaticPath();
String tempPath = staticPath + "temp" + File.separator;
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
File tempDirFile = new File(tempPath);
if (!tempDirFile.exists()) {
tempDirFile.mkdirs();
}
FileOutputStream f = null;
@ -213,6 +222,8 @@ public class FiletransferService implements IFiletransferService {
Downloader downloader = ufoFactory.getDownloader(StorageTypeEnum.LOCAL.getStorageType());
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl("temp" + File.separator+userFile.getFileName() + ".zip");
File tempFile = FileOperation.newFile(PathUtil.getStaticPath() + downloadFile.getFileUrl());
httpServletResponse.setContentLengthLong(tempFile.length());
downloader.download(httpServletResponse, downloadFile);
String zipPath = PathUtil.getStaticPath() + "temp" + File.separator+userFile.getFileName() + ".zip";
File file = new File(zipPath);