1、修复文件解压失败问题

2、修复新建PPT文件打开失败问题
3、新增复制文件接口
4、底层代码优化
This commit is contained in:
马超 2021-08-16 17:37:02 +08:00
parent 497407a2a8
commit f74ff38c94
28 changed files with 412 additions and 269 deletions

View File

@ -6,12 +6,12 @@
<parent>
<groupId>com.qiwenshare</groupId>
<artifactId>qiwenshare</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
</parent>
<groupId>com.qiwenshare</groupId>
<artifactId>qiwen-file</artifactId>
<version>1.0.7-SNAPSHOT</version>
<version>1.0.8-SNAPSHOT</version>
<name>qiwen-file</name>
<description>fileos.qiwenshare.com</description>
<packaging>jar</packaging>

View File

@ -71,10 +71,10 @@ public class GlobalExceptionHandlerAdvice {
/**-------- 自定义定异常处理方法 --------**/
@ExceptionHandler(CMSException.class)
@ExceptionHandler(QiwenException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public RestResult error(CMSException e) {
public RestResult error(QiwenException e) {
e.printStackTrace();
log.error("全局异常捕获:" + e);
return RestResult.fail().message(e.getMessage()).code(e.getCode());

View File

@ -7,21 +7,21 @@ import lombok.Data;
* 自定义全局异常类
*/
@Data
public class CMSException extends RuntimeException {
public class QiwenException extends RuntimeException {
private Integer code;
public CMSException(Integer code, String message) {
public QiwenException(Integer code, String message) {
super(message);
this.code = code;
}
public CMSException(ResultCodeEnum resultCodeEnum) {
public QiwenException(ResultCodeEnum resultCodeEnum) {
super(resultCodeEnum.getMessage());
this.code = resultCodeEnum.getCode();
}
@Override
public String toString() {
return "CMSException{" + "code=" + code + ", message=" + this.getMessage() + '}';
return "QiwenException{" + "code=" + code + ", message=" + this.getMessage() + '}';
}
}

View File

@ -12,7 +12,7 @@ public interface IFileService extends IService<FileBean> {
void increaseFilePointCount(Long fileId);
void decreaseFilePointCount(Long fileId);
void unzipFile(long userFileId, int unzipMode, String filePath);

View File

@ -11,10 +11,12 @@ import java.util.Map;
public interface IUserFileService extends IService<UserFile> {
List<UserFile> selectUserFileByNameAndPath(String fileName, String filePath, Long userId);
boolean isDirExist(String fileName, String filePath, long userId);
List<UserFile> selectSameUserFile(String fileName, String filePath, String extendName, Long userId);
void replaceUserFilePath(String filePath, String oldFilePath, Long userId);
List<FileListVo> userFileList(UserFile userFile, Long beginCount, Long pageCount);
void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName, long userId);
void userFileCopy(String oldfilePath, String newfilePath, String fileName, String extendName, long userId);
List<FileListVo> selectFileByExtendName(List<String> fileNameList, Long beginCount, Long pageCount, long userId);
Long selectCountByExtendName(List<String> fileNameList, Long beginCount, Long pageCount, long userId);

View File

@ -13,7 +13,7 @@ import com.qiwenshare.file.vo.file.FileListVo;
import com.qiwenshare.ufop.factory.UFOPFactory;
import com.qiwenshare.ufop.operation.read.Reader;
import com.qiwenshare.ufop.operation.read.domain.ReadFile;
import com.qiwenshare.ufop.util.PathUtil;
import com.qiwenshare.ufop.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -45,6 +45,7 @@ public class FileDealComp {
IShareFileService shareFileService;
@Resource
IUserFileService userFileService;
@Autowired
private IElasticSearchService elasticSearchService;
public static Executor exec = Executors.newFixedThreadPool(10);
@ -64,11 +65,13 @@ public class FileDealComp {
String extendName = userFile.getExtendName();
Integer deleteFlag = userFile.getDeleteFlag();
Long userId = userFile.getUserId();
int isDir = userFile.getIsDir();
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getFilePath, savefilePath)
.eq(UserFile::getDeleteFlag, deleteFlag)
.eq(UserFile::getUserId, userId)
.eq(UserFile::getFileName, fileName);
.eq(UserFile::getFileName, fileName)
.eq(UserFile::getIsDir, isDir);
if (userFile.getIsDir() == 0) {
lambdaQueryWrapper.eq(UserFile::getExtendName, extendName);
}
@ -87,7 +90,8 @@ public class FileDealComp {
lambdaQueryWrapper1.eq(UserFile::getFilePath, savefilePath)
.eq(UserFile::getDeleteFlag, deleteFlag)
.eq(UserFile::getUserId, userId)
.eq(UserFile::getFileName, fileName + "(" + i + ")");
.eq(UserFile::getFileName, fileName + "(" + i + ")")
.eq(UserFile::getIsDir, isDir);
if (userFile.getIsDir() == 0) {
lambdaQueryWrapper1.eq(UserFile::getExtendName, extendName);
}
@ -111,10 +115,10 @@ public class FileDealComp {
// 加锁防止并发情况下有重复创建目录情况
Lock lock = new ReentrantLock();
lock.lock();
String parentFilePath = PathUtil.getParentPath(filePath);
String parentFilePath = UFOPUtils.getParentPath(filePath);
while(parentFilePath.contains("/")) {
String fileName = parentFilePath.substring(parentFilePath.lastIndexOf("/") + 1);
parentFilePath = PathUtil.getParentPath(parentFilePath);
parentFilePath = UFOPUtils.getParentPath(parentFilePath);
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getFilePath, parentFilePath + FileConstant.pathSeparator)

View File

@ -1,34 +1,33 @@
package com.qiwenshare.file.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.exception.NotLoginException;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.operation.FileOperation;
import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.file.api.*;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.file.advice.QiwenException;
import com.qiwenshare.file.api.IFileService;
import com.qiwenshare.file.api.IUserFileService;
import com.qiwenshare.file.api.IUserService;
import com.qiwenshare.file.component.FileDealComp;
import com.qiwenshare.file.config.es.FileSearch;
import com.qiwenshare.file.domain.*;
import com.qiwenshare.file.dto.*;
import com.qiwenshare.file.domain.TreeNode;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.dto.BatchMoveFileDTO;
import com.qiwenshare.file.dto.CopyFileDTO;
import com.qiwenshare.file.dto.MoveFileDTO;
import com.qiwenshare.file.dto.file.*;
import com.qiwenshare.file.vo.file.FileListVo;
import com.qiwenshare.ufop.factory.UFOPFactory;
import com.qiwenshare.ufop.operation.copy.domain.CopyFile;
import com.qiwenshare.ufop.operation.download.Downloader;
import com.qiwenshare.ufop.operation.download.domain.DownloadFile;
import com.qiwenshare.ufop.operation.rename.domain.RenameFile;
import com.qiwenshare.ufop.util.PathUtil;
import com.qiwenshare.ufop.util.UFOPUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.util.StringUtil;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
@ -42,13 +41,10 @@ import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilde
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.*;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import static com.qiwenshare.common.util.FileUtil.getFileExtendsByType;
@Tag(name = "file", description = "该接口为文件接口,主要用来做一些文件的基本操作,如创建目录,删除,移动,复制等。")
@RestController
@Slf4j
@ -84,8 +80,9 @@ public class FileController {
UserBean sessionUserBean = userService.getUserBeanByToken(token);
List<UserFile> userFiles = userFileService.selectUserFileByNameAndPath(createFileDto.getFileName(), createFileDto.getFilePath(), sessionUserBean.getUserId());
if (userFiles != null && !userFiles.isEmpty()) {
boolean isDirExist = userFileService.isDirExist(createFileDto.getFileName(), createFileDto.getFilePath(), sessionUserBean.getUserId());
if (isDirExist) {
return RestResult.fail().message("同名文件已存在");
}
@ -201,7 +198,7 @@ public class FileController {
List<FileListVo> fileList = null;
userFile.setFilePath(PathUtil.urlDecode(filePath));
userFile.setFilePath(UFOPUtils.urlDecode(filePath));
if (currentPage == 0 || pageCount == 0) {
fileList = userFileService.userFileList(userFile, 0L, 10L);
} else {
@ -274,116 +271,41 @@ public class FileController {
if (sessionUserBean == null) {
throw new NotLoginException();
}
UserFile userFile = userFileService.getById(unzipFileDto.getUserFileId());
FileBean fileBean = fileService.getById(userFile.getFileId());
File destFile = new File(PathUtil.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl());
Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
downloadFile.setFileSize(fileBean.getFileSize());
InputStream inputStream = downloader.getInputStream(downloadFile);
try {
FileUtils.copyInputStreamToFile(inputStream, destFile);
} catch (IOException e) {
e.printStackTrace();
fileService.unzipFile(unzipFileDto.getUserFileId(), unzipFileDto.getUnzipMode(), unzipFileDto.getFilePath());
} catch (QiwenException e) {
return RestResult.fail().message(e.getMessage());
}
String extendName = userFile.getExtendName();
String unzipUrl = (PathUtil.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl()).replace("." + extendName, "");
List<String> fileEntryNameList = new ArrayList<>();
if ("zip".equals(extendName)) {
fileEntryNameList = FileOperation.unzip(destFile, unzipUrl);
} else if ("rar".equals(extendName)) {
try {
fileEntryNameList = FileOperation.unrar(destFile, unzipUrl);
} catch (Exception e) {
e.printStackTrace();
log.error("rar解压失败" + e);
return RestResult.fail().message("rar解压失败");
}
} else {
return RestResult.fail().message("不支持的文件格式!");
}
if (destFile.exists()) {
destFile.delete();
}
for (int i = 0; i < fileEntryNameList.size(); i++){
String entryName = fileEntryNameList.get(i);
log.info("文件名:"+ entryName);
executor.execute(() -> {
String totalFileUrl = unzipUrl + entryName;
File currentFile = FileOperation.newFile(totalFileUrl);
FileBean tempFileBean = new FileBean();
UserFile saveUserFile = new UserFile();
saveUserFile.setUploadTime(DateUtil.getCurrentTime());
saveUserFile.setUserId(sessionUserBean.getUserId());
saveUserFile.setFilePath(FileUtil.pathSplitFormat(userFile.getFilePath() + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
if (currentFile.isDirectory()){
saveUserFile.setIsDir(1);
saveUserFile.setFileName(currentFile.getName());
}else{
String saveFileUrl = "";
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(currentFile);
CopyFile createFile = new CopyFile();
createFile.setExtendName(FileUtil.getFileExtendName(totalFileUrl));
saveFileUrl = ufopFactory.getCopier().copy(fileInputStream, createFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
log.info("关闭流");
fileInputStream.close();
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
saveUserFile.setIsDir(0);
saveUserFile.setExtendName(FileUtil.getFileExtendName(totalFileUrl));
saveUserFile.setFileName(FileUtil.getFileNameNotExtend(currentFile.getName()));
tempFileBean.setFileSize(currentFile.length());
tempFileBean.setFileUrl(saveFileUrl);
tempFileBean.setPointCount(1);
tempFileBean.setStorageType(storageType);
boolean saveResult = fileService.save(tempFileBean);
if (saveResult) {
boolean result = currentFile.delete();
log.info("删除{}结果:{}",saveFileUrl, result);
}
}
saveUserFile.setFileId(tempFileBean.getFileId());
saveUserFile.setDeleteFlag(0);
userFileService.save(saveUserFile);
});
}
return RestResult.success();
}
@Operation(summary = "文件复制", description = "可以复制文件或者目录", tags = {"file"})
@RequestMapping(value = "/copyfile", method = RequestMethod.POST)
@MyLog(operation = "文件复制", module = CURRENT_MODULE)
@ResponseBody
public RestResult<String> copyFile(@RequestBody CopyFileDTO copyFileDTO, @RequestHeader("token") String token) {
UserBean sessionUserBean = userService.getUserBeanByToken(token);
if (sessionUserBean == null) {
throw new NotLoginException();
}
String oldfilePath = copyFileDTO.getOldFilePath();
String newfilePath = copyFileDTO.getFilePath();
String fileName = copyFileDTO.getFileName();
String extendName = copyFileDTO.getExtendName();
if (StringUtil.isEmpty(extendName)) {
String testFilePath = oldfilePath + fileName + "/";
if (newfilePath.startsWith(testFilePath)) {
return RestResult.fail().message("原路径与目标路径冲突,不能移动");
}
}
userFileService.updateFilepathByFilepath(oldfilePath, newfilePath, fileName, extendName, sessionUserBean.getUserId());
return RestResult.success();
}
@Operation(summary = "文件移动", description = "可以移动文件或者目录", tags = {"file"})
@RequestMapping(value = "/movefile", method = RequestMethod.POST)
@ -468,19 +390,19 @@ public class FileController {
}
Long total = 0L;
if (fileType == FileUtil.OTHER_TYPE) {
if (fileType == UFOPUtils.OTHER_TYPE) {
List<String> arrList = new ArrayList<>();
arrList.addAll(Arrays.asList(FileUtil.DOC_FILE));
arrList.addAll(Arrays.asList(FileUtil.IMG_FILE));
arrList.addAll(Arrays.asList(FileUtil.VIDEO_FILE));
arrList.addAll(Arrays.asList(FileUtil.MUSIC_FILE));
arrList.addAll(Arrays.asList(UFOPUtils.DOC_FILE));
arrList.addAll(Arrays.asList(UFOPUtils.IMG_FILE));
arrList.addAll(Arrays.asList(UFOPUtils.VIDEO_FILE));
arrList.addAll(Arrays.asList(UFOPUtils.MUSIC_FILE));
fileList = userFileService.selectFileNotInExtendNames(arrList,beginCount, pageCount, userId);
total = userFileService.selectCountNotInExtendNames(arrList,beginCount, pageCount, userId);
} else {
fileList = userFileService.selectFileByExtendName(getFileExtendsByType(fileType), beginCount, pageCount,userId);
total = userFileService.selectCountByExtendName(getFileExtendsByType(fileType), beginCount, pageCount,userId);
fileList = userFileService.selectFileByExtendName(UFOPUtils.getFileExtendsByType(fileType), beginCount, pageCount,userId);
total = userFileService.selectCountByExtendName(UFOPUtils.getFileExtendsByType(fileType), beginCount, pageCount,userId);
}
Map<String, Object> map = new HashMap<>();

View File

@ -4,21 +4,23 @@ import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.exception.NotLoginException;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.MimeUtils;
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.component.FileDealComp;
import com.qiwenshare.file.domain.*;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.StorageBean;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.dto.DownloadFileDTO;
import com.qiwenshare.file.dto.UploadFileDTO;
import com.qiwenshare.file.dto.file.PreviewDTO;
import com.qiwenshare.file.service.StorageService;
import com.qiwenshare.file.vo.file.FileListVo;
import com.qiwenshare.file.vo.file.UploadFileVo;
import com.qiwenshare.ufop.util.PathUtil;
import com.qiwenshare.ufop.util.UFOPUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@ -29,7 +31,6 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -82,16 +83,16 @@ public class FiletransferController {
userFile.setUserId(sessionUserBean.getUserId());
String relativePath = uploadFileDto.getRelativePath();
if (relativePath.contains("/")) {
userFile.setFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/");
fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/", sessionUserBean.getUserId());
userFile.setFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/");
fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/", sessionUserBean.getUserId());
fileDealComp.deleteRepeatSubDirFile(uploadFileDto.getFilePath(), sessionUserBean.getUserId());
} else {
userFile.setFilePath(uploadFileDto.getFilePath());
}
String fileName = uploadFileDto.getFilename();
userFile.setFileName(FileUtil.getFileNameNotExtend(fileName));
userFile.setExtendName(FileUtil.getFileExtendName(fileName));
userFile.setFileName(UFOPUtils.getFileNameNotExtend(fileName));
userFile.setExtendName(UFOPUtils.getFileExtendName(fileName));
userFile.setDeleteFlag(0);
List<FileListVo> userFileList = userFileService.userFileList(userFile, null, null);
if (userFileList.size() <= 0) {

View File

@ -20,7 +20,7 @@ import com.qiwenshare.ufop.operation.copy.domain.CopyFile;
import com.qiwenshare.ufop.operation.download.domain.DownloadFile;
import com.qiwenshare.ufop.operation.write.Writer;
import com.qiwenshare.ufop.operation.write.domain.WriteFile;
import com.qiwenshare.ufop.util.PathUtil;
import com.qiwenshare.ufop.util.UFOPUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@ -89,7 +89,7 @@ public class OfficeController {
} else if ("pptx".equals(extendName)) {
templateFilePath = "template/PowerPoint.pptx";
}
String templateFileUrl = PathUtil.getStaticPath() + templateFilePath;
String templateFileUrl = UFOPUtils.getStaticPath() + templateFilePath;
FileInputStream fileInputStream = new FileInputStream(templateFileUrl);
Copier copier = ufopFactory.getCopier();
CopyFile copyFile = new CopyFile();

View File

@ -218,6 +218,9 @@ public class ShareController {
LambdaQueryWrapper<Share> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(Share::getShareBatchNum, checkEndTimeDTO.getShareBatchNum());
Share share = shareService.getOne(lambdaQueryWrapper);
if (share == null) {
return RestResult.fail().message("文件不存在!");
}
String endTime = share.getEndTime();
Date endTimeDate = null;
try {

View File

@ -0,0 +1,26 @@
package com.qiwenshare.file.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(name = "复制文件DTO",required = true)
public class CopyFileDTO {
@Schema(description = "用户文件id", required = true)
private long userFileId;
@Schema(description = "文件路径", required = true)
private String filePath;
@Schema(description = "文件名", required = true)
@Deprecated
private String fileName;
@Schema(description = "旧文件名", required = true)
@Deprecated
private String oldFilePath;
@Schema(description = "扩展名", required = true)
@Deprecated
private String extendName;
}

View File

@ -9,10 +9,9 @@ public class UnzipFileDTO {
@Schema(description = "文件url", required = true)
private long userFileId;
@Schema(description = "文件url", required = true)
@Deprecated
private String fileUrl;
@Schema(description = "文件路径", required = true)
@Deprecated
@Schema(description = "解压模式 0-解压到当前文件夹, 1-自动创建该文件名目录,并解压到目录里, 3-手动选择解压目录", required = true)
private int unzipMode;
@Schema(description = "解压目的文件目录,仅当 unzipMode 为 2 时必传")
private String filePath;
}

View File

@ -17,6 +17,16 @@ public interface UserFileMapper extends BaseMapper<UserFile> {
void updateFilepathByPathAndName(String oldfilePath, String newfilePath, String fileName, String extendName, long userId);
void updateFilepathByFilepath(String oldfilePath, String newfilePath, long userId);
void batchInsertByPathAndName(@Param("oldFilePath") String oldFilePath,
@Param("newFilePath") String newfilePath,
@Param("fileName") String fileName,
@Param("extendName") String extendName,
@Param("userId") long userId);
void batchInsertByFilepath(@Param("oldFilePath") String oldFilePath,
@Param("newFilePath") String newfilePath,
@Param("userId") long userId);
List<FileListVo> selectFileByExtendName(List<String> fileNameList, Long beginCount, Long pageCount, long userId);
Long selectCountByExtendName(List<String> fileNameList, Long beginCount, Long pageCount, long userId);
List<FileListVo> selectFileNotInExtendNames(List<String> fileNameList, Long beginCount, Long pageCount, long userId);

View File

@ -1,23 +1,55 @@
package com.qiwenshare.file.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiwenshare.common.operation.FileOperation;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.file.advice.QiwenException;
import com.qiwenshare.file.api.IFileService;
import com.qiwenshare.file.component.FileDealComp;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.mapper.FileMapper;
import com.qiwenshare.file.mapper.UserFileMapper;
import com.qiwenshare.ufop.factory.UFOPFactory;
import com.qiwenshare.ufop.operation.copy.domain.CopyFile;
import com.qiwenshare.ufop.operation.download.Downloader;
import com.qiwenshare.ufop.operation.download.domain.DownloadFile;
import com.qiwenshare.ufop.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@Slf4j
@Service
@Transactional(rollbackFor=Exception.class)
public class FileService extends ServiceImpl<FileMapper, FileBean> implements IFileService {
public static Executor executor = Executors.newFixedThreadPool(20);
@Resource
FileMapper fileMapper;
@Resource
UserFileMapper userFileMapper;
@Resource
UFOPFactory ufopFactory;
@Value("${ufop.storage-type}")
private Integer storageType;
@Resource
FileDealComp fileDealComp;
@Override
public void increaseFilePointCount(Long fileId) {
@ -37,5 +69,152 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
fileMapper.updateById(fileBean);
}
@Override
public void unzipFile(long userFileId, int unzipMode, String filePath) {
UserFile userFile = userFileMapper.selectById(userFileId);
FileBean fileBean = fileMapper.selectById(userFile.getFileId());
File destFile = new File(UFOPUtils.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl());
Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
downloadFile.setFileSize(fileBean.getFileSize());
InputStream inputStream = downloader.getInputStream(downloadFile);
try {
FileUtils.copyInputStreamToFile(inputStream, destFile);
} catch (IOException e) {
e.printStackTrace();
}
String extendName = userFile.getExtendName();
String unzipUrl = UFOPUtils.getTempPath(fileBean.getFileUrl()).getAbsolutePath().replace("." + extendName, "");
List<String> fileEntryNameList = new ArrayList<>();
if ("zip".equals(extendName)) {
fileEntryNameList = FileOperation.unzip(destFile, unzipUrl);
} else if ("rar".equals(extendName)) {
try {
fileEntryNameList = FileOperation.unrar(destFile, unzipUrl);
} catch (Exception e) {
e.printStackTrace();
log.error("rar解压失败" + e);
throw new QiwenException(500001, "rar解压异常" + e.getMessage());
}
} else {
throw new QiwenException(500002, "不支持的文件格式!");
}
if (destFile.exists()) {
destFile.delete();
}
for (int i = 0; i < fileEntryNameList.size(); i++){
String entryName = fileEntryNameList.get(i);
log.info("文件名:"+ entryName);
executor.execute(() -> {
String totalFileUrl = unzipUrl + entryName;
File currentFile = new File(totalFileUrl);
FileBean tempFileBean = new FileBean();
UserFile saveUserFile = new UserFile();
saveUserFile.setUploadTime(DateUtil.getCurrentTime());
saveUserFile.setUserId(userFile.getUserId());
saveUserFile.setFilePath(UFOPUtils.pathSplitFormat(userFile.getFilePath() + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
if (currentFile.isDirectory()){
saveUserFile.setIsDir(1);
saveUserFile.setFileName(currentFile.getName());
}else{
FileInputStream fileInputStream = null;
FileInputStream fileInputStream1 = null;
try {
fileInputStream = new FileInputStream(currentFile);
String md5Str = DigestUtils.md5Hex(fileInputStream);
Map<String, Object> param = new HashMap<String, Object>();
param.put("identifier", md5Str);
List<FileBean> list = fileMapper.selectByMap(param);
if (list != null && !list.isEmpty()) { //文件已存在
increaseFilePointCount(list.get(0).getFileId());
saveUserFile.setFileId(list.get(0).getFileId());
} else { //文件不存在
fileInputStream1 = new FileInputStream(currentFile);
CopyFile createFile = new CopyFile();
createFile.setExtendName(UFOPUtils.getFileExtendName(totalFileUrl));
String saveFileUrl = ufopFactory.getCopier().copy(fileInputStream1, createFile);
tempFileBean.setFileSize(currentFile.length());
tempFileBean.setFileUrl(saveFileUrl);
tempFileBean.setPointCount(1);
tempFileBean.setStorageType(storageType);
tempFileBean.setIdentifier(md5Str);
fileMapper.insert(tempFileBean);
saveUserFile.setFileId(tempFileBean.getFileId());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
log.info("关闭流");
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream1 != null) {
try {
log.info("关闭流");
fileInputStream1.close();
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
currentFile.delete();
}
}
saveUserFile.setIsDir(0);
saveUserFile.setExtendName(UFOPUtils.getFileExtendName(totalFileUrl));
saveUserFile.setFileName(UFOPUtils.getFileNameNotExtend(currentFile.getName()));
}
saveUserFile.setDeleteFlag(0);
if (unzipMode == 1) {
String destFilePath = "/" + userFile.getFilePath() + saveUserFile.getFilePath();
saveUserFile.setFilePath(destFilePath);
} else if(unzipMode == 2) {
saveUserFile.setFilePath(filePath);
}
String fileName = fileDealComp.getRepeatFileName(saveUserFile, saveUserFile.getFilePath());
if (saveUserFile.getIsDir() == 1 && !fileName.equals(saveUserFile.getFileName())) {
//如果是目录而且重复什么也不做
} else {
saveUserFile.setFileName(fileName);
userFileMapper.insert(saveUserFile);
}
});
}
}
}

View File

@ -1,35 +1,18 @@
package com.qiwenshare.file.service;
import java.io.*;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
import com.qiwenshare.file.api.IFiletransferService;
import com.qiwenshare.file.component.FileDealComp;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.StorageBean;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.dto.DownloadFileDTO;
import com.qiwenshare.file.dto.UploadFileDTO;
import com.qiwenshare.file.dto.file.PreviewDTO;
import com.qiwenshare.file.mapper.FileMapper;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.StorageBean;
import com.qiwenshare.file.mapper.StorageMapper;
import com.qiwenshare.file.mapper.UserFileMapper;
import com.qiwenshare.file.vo.file.FileListVo;
@ -47,12 +30,21 @@ import com.qiwenshare.ufop.operation.preview.domain.PreviewFile;
import com.qiwenshare.ufop.operation.upload.Uploader;
import com.qiwenshare.ufop.operation.upload.domain.UploadFile;
import com.qiwenshare.ufop.operation.upload.domain.UploadFileResult;
import com.qiwenshare.ufop.util.PathUtil;
import com.qiwenshare.ufop.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
@Service
@Transactional(rollbackFor=Exception.class)
@ -103,8 +95,8 @@ public class FiletransferService implements IFiletransferService {
UserFile userFile = new UserFile();
String relativePath = uploadFileDto.getRelativePath();
if (relativePath.contains("/")) {
userFile.setFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/");
fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + PathUtil.getParentPath(relativePath) + "/", userId);
userFile.setFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/");
fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/", userId);
fileDealComp.deleteRepeatSubDirFile(uploadFileDto.getFilePath(), userId);
} else {
@ -157,7 +149,7 @@ public class FiletransferService implements IFiletransferService {
.eq(UserFile::getDeleteFlag, 0);
List<UserFile> userFileList = userFileMapper.selectList(lambdaQueryWrapper);
String staticPath = PathUtil.getStaticPath();
String staticPath = UFOPUtils.getStaticPath();
String tempPath = staticPath + "temp" + File.separator;
File tempDirFile = new File(tempPath);
if (!tempDirFile.exists()) {
@ -224,10 +216,10 @@ public class FiletransferService implements IFiletransferService {
Downloader downloader = ufopFactory.getDownloader(StorageTypeEnum.LOCAL.getCode());
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl("temp" + File.separator+userFile.getFileName() + ".zip");
File tempFile = FileOperation.newFile(PathUtil.getStaticPath() + downloadFile.getFileUrl());
File tempFile = new File(UFOPUtils.getStaticPath() + downloadFile.getFileUrl());
httpServletResponse.setContentLengthLong(tempFile.length());
downloader.download(httpServletResponse, downloadFile);
String zipPath = PathUtil.getStaticPath() + "temp" + File.separator+userFile.getFileName() + ".zip";
String zipPath = UFOPUtils.getStaticPath() + "temp" + File.separator+userFile.getFileName() + ".zip";
File file = new File(zipPath);
if (file.exists()) {
file.delete();

View File

@ -43,10 +43,25 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
lambdaQueryWrapper.eq(UserFile::getFileName, fileName)
.eq(UserFile::getFilePath, filePath)
.eq(UserFile::getUserId, userId)
.eq(UserFile::getDeleteFlag, "0");
.eq(UserFile::getDeleteFlag, 0);
return userFileMapper.selectList(lambdaQueryWrapper);
}
@Override
public boolean isDirExist(String fileName, String filePath, long userId){
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getFileName, fileName)
.eq(UserFile::getFilePath, filePath)
.eq(UserFile::getUserId, userId)
.eq(UserFile::getDeleteFlag, 0)
.eq(UserFile::getIsDir, 1);
List<UserFile> list = userFileMapper.selectList(lambdaQueryWrapper);
if (list != null && !list.isEmpty()) {
return true;
}
return false;
}
@Override
public List<UserFile> selectSameUserFile(String fileName, String filePath, String extendName, Long userId) {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
@ -68,54 +83,6 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
return userFileMapper.userFileList(userFile, beginCount, pageCount);
}
// public void renameUserFile(Long userFileId, String newFileName, Long userId) {
// UserFile userFile = userFileMapper.selectById(userFileId);
// if (1 == userFile.getIsDir()) {
// LambdaUpdateWrapper<UserFile> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
// lambdaUpdateWrapper.set(UserFile::getFileName, newFileName)
// .set(UserFile::getUploadTime, DateUtil.getCurrentTime())
// .eq(UserFile::getUserFileId, userFile.getUserFileId());
// userFileMapper.update(null, lambdaUpdateWrapper);
// replaceUserFilePath(userFile.getFilePath() + newFileName + "/",
// userFile.getFilePath() + userFile.getFileName() + "/", userId);
// } else {
// FileBean fileBean = fileMapper.selectById(userFile.getFileId());
// if (fileBean.getIsOSS() == 1) {
//// LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//// lambdaQueryWrapper.eq(UserFile::getUserFileId, renameFileDto.getUserFileId());
//// UserFile userFile = userFileService.getOne(lambdaQueryWrapper);
////
//// FileBean file = fileService.getById(userFile.getFileId());
// String fileUrl = fileBean.getFileUrl();
// String newFileUrl = fileUrl.replace(userFile.getFileName(), newFileName);
//
// AliyunOSSRename.rename(qiwenFileConfig.getAliyun().getOss(),
// fileUrl.substring(1),
// newFileUrl.substring(1));
// LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
// lambdaUpdateWrapper
// .set(FileBean::getFileUrl, newFileUrl)
// .eq(FileBean::getFileId, fileBean.getFileId());
// fileMapper.update(null, lambdaUpdateWrapper);
//
// LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
// userFileLambdaUpdateWrapper
// .set(UserFile::getFileName, newFileName)
// .set(UserFile::getUploadTime, DateUtil.getCurrentTime())
// .eq(UserFile::getUserFileId, userFileId);
// userFileMapper.update(null, userFileLambdaUpdateWrapper);
// } else {
// LambdaUpdateWrapper<UserFile> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
// lambdaUpdateWrapper.set(UserFile::getFileName, newFileName)
// .set(UserFile::getUploadTime, DateUtil.getCurrentTime())
// .eq(UserFile::getUserFileId, userFileId);
// userFileMapper.update(null, lambdaUpdateWrapper);
// }
//
//
// }
// }
@Override
public void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName, long userId) {
if ("null".equals(extendName)){
@ -139,6 +106,34 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
}
@Override
public void userFileCopy(String oldfilePath, String newfilePath, String fileName, String extendName, long userId) {
if ("null".equals(extendName)){
extendName = null;
}
userFileMapper.batchInsertByPathAndName(oldfilePath, newfilePath, fileName, extendName, userId);
//移动根目录
// userFileMapper.updateFilepathByPathAndName(oldfilePath, newfilePath, fileName, extendName, userId);
//移动子目录
oldfilePath = oldfilePath + fileName + "/";
newfilePath = newfilePath + fileName + "/";
oldfilePath = oldfilePath.replace("\\", "\\\\\\\\");
oldfilePath = oldfilePath.replace("'", "\\'");
oldfilePath = oldfilePath.replace("%", "\\%");
oldfilePath = oldfilePath.replace("_", "\\_");
if (extendName == null) { //为null说明是目录则需要移动子目录
userFileMapper.batchInsertByFilepath(oldfilePath, newfilePath, userId);
}
}
@Override
public List<FileListVo> selectFileByExtendName(List<String> fileNameList, Long beginCount, Long pageCount, long userId) {

View File

@ -16,8 +16,8 @@ logging.level.root=info
#mybatis配置
mybatis.type-aliases-package=com.qiwenshare.file.domain
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.config-locations=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
@ -44,7 +44,7 @@ spring.servlet.multipart.enabled=true
spring.main.allow-bean-definition-overriding=true
mybatis-plus.type-aliases-package=com.qiwenshare.web.domain
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.configuration.map-underscore-to-camel-case=false
mybatis-plus.global-config.banner=false
@ -106,6 +106,6 @@ spring.elasticsearch.rest.password=
# 当前部署外网IP用于office预览
deployment.host: 192.168.31.158:${server.port}
deployment.host=192.168.31.158:${server.port}

View File

@ -96,6 +96,47 @@
and userId = #{param5}
</update>
<insert id="batchInsertByPathAndName">
insert into userfile ( deleteBatchNum, deleteFlag, deleteTime,
extendName, fileId, fileName, filePath, isDir, uploadTime, userId)
(select deleteBatchNum, deleteFlag, deleteTime, extendName, fileId,
fileName, #{newFilePath}, isDir, uploadTime, userId
from userfile
<where>
<if test="userId != 0">
and userId = #{userId}
</if>
<if test="fileName != null">
and fileName = #{fileName}
</if>
<if test="oldFilePath != null">
and filePath = #{oldFilePath}
</if>
<choose>
<when test="extendName != null">
and extendName = #{extendName}
</when>
<otherwise>
and isDir = 1
</otherwise>
</choose>
</where>
)
</insert>
<update id="batchInsertByFilepath">
insert into userfile ( deleteBatchNum, deleteFlag, deleteTime,
extendName, fileId, fileName, filePath, isDir, uploadTime, userId)
(select deleteBatchNum, deleteFlag, deleteTime, extendName, fileId,
fileName, REPLACE(filePath, #{oldFilePath}, #{newFilePath}), isDir,
uploadTime, userId
from userfile
where filePath like N'${oldFilePath}%' and userId = #{userId}
)
</update>
<select id="selectStorageSizeByUserId" resultType="java.lang.Long" parameterType="java.lang.Long">
SELECT SUM(fileSize) FROM userfile
LEFT JOIN file ON file.fileId = userfile.fileId

View File

@ -1,31 +0,0 @@
@echo off & setlocal enabledelayedexpansion
rem if need special jdk
rem JAVA_HOME="c:\Java\jdk1.8.0_191\"
rem PATH=%JAVA_HOME%\bin;%PATH%
rem enter app deploy path
cd ..\
set DEPLOY_PATH=%cd%
set CONF_DIR=%DEPLOY_PATH%/conf
set LIB_JARS=%DEPLOY_PATH%/lib/*
set LOG_PATH=%DEPLOY_PATH%/logs
set CLASSPATH=".;%CONF_DIR%;%LIB_JARS%"
set JAVA_HOME=%DEPLOY_PATH%
set PATH=%JAVA_HOME%\bin;%PATH%
set JAVA_VM=-D64 -server -Xmx512m -Xms512m -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC
set JAVA_OPTIONS=-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=8231
echo DEPLOY_PATH: %DEPLOY_PATH%
echo CLASSPATH=%CLASSPATH%
echo "Start App..."
java -version
start javaw %JAVA_VM% -classpath %CLASSPATH% com.qiwenshare.file.FileApplication
goto end
:end
pause