代码重构

This commit is contained in:
马超 2020-12-13 00:25:43 +08:00
parent 97dbaf9e03
commit dbe05ea599
13 changed files with 347 additions and 212 deletions

View File

@ -3,27 +3,26 @@ package com.qiwenshare.file.api;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.domain.UserFile;
import java.util.List;
public interface IFileService extends IService<FileBean> {
void batchInsertFile(List<FileBean> fileBeanList, Long userId);
// void batchInsertFile(List<FileBean> fileBeanList, Long userId);
//void updateFile(FileBean fileBean);
public void increaseFilePointCount(Long fileId);
void increaseFilePointCount(Long fileId);
void decreaseFilePointCount(Long fileId);
// List<FileBean> selectFileListByPath(FileBean fileBean);
public void decreaseFilePointCount(Long fileId);
List<FileBean> selectFilePathTreeByUserId(FileBean fileBean);
List<FileBean> selectFileListByPath(FileBean fileBean);
List<FileBean> selectFileTreeListLikeFilePath(String filePath);
void deleteFile(FileBean fileBean, UserBean sessionUserBean);
void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName);
List<FileBean> selectFileByExtendName(List<String> fileNameList, long userId);
List<FileBean> selectFileNotInExtendNames(List<String> fileNameList, long userId);
}

View File

@ -2,6 +2,7 @@ package com.qiwenshare.file.api;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.domain.UserFile;
import java.util.List;
@ -11,4 +12,11 @@ public interface IUserFileService extends IService<UserFile> {
List<UserFile> selectUserFileByNameAndPath(String fileName, String filePath, Long userId);
void replaceUserFilePath(String filePath, String oldFilePath, Long userId);
List<Map<String, Object>> userFileList(UserFile userFile);
void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName);
List<Map<String, Object>> selectFileByExtendName(List<String> fileNameList, long userId);
List<Map<String, Object>> selectFileNotInExtendNames(List<String> fileNameList, long userId);
List<UserFile> selectFileTreeListLikeFilePath(String filePath);
List<UserFile> selectFilePathTreeByUserId(Long userId);
void deleteUserFile(UserFile userFile, UserBean sessionUserBean);
}

View File

@ -1,5 +1,6 @@
package com.qiwenshare.file.controller;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -200,10 +201,10 @@ public class FileController {
return operationCheck(token);
}
UserBean sessionUserBean = userService.getUserBeanByToken(token);
List<FileBean> fileList = JSON.parseArray(batchDeleteFileDto.getFiles(), FileBean.class);
List<UserFile> userFiles = JSON.parseArray(batchDeleteFileDto.getFiles(), UserFile.class);
for (FileBean file : fileList) {
fileService.deleteFile(file,sessionUserBean);
for (UserFile userFile : userFiles) {
userFileService.deleteUserFile(userFile,sessionUserBean);
}
result.setData("批量删除文件成功");
@ -218,13 +219,16 @@ public class FileController {
*/
@RequestMapping(value = "/deletefile", method = RequestMethod.POST)
@ResponseBody
public String deleteFile(@RequestBody FileBean fileBean, @RequestHeader("token") String token) {
public String deleteFile(@RequestBody DeleteFileDto deleteFileDto, @RequestHeader("token") String token) {
RestResult<String> result = new RestResult<String>();
if (!operationCheck(token).isSuccess()){
return JSON.toJSONString(operationCheck(token));
}
UserBean sessionUserBean = userService.getUserBeanByToken(token);
fileService.deleteFile(fileBean, sessionUserBean);
UserFile userFile = new UserFile();
userFile.setUserFileId(deleteFileDto.getUserFileId());
BeanUtil.copyProperties(deleteFileDto, userFile);
userFileService.deleteUserFile(userFile, sessionUserBean);
result.setSuccess(true);
String resultJson = JSON.toJSONString(result);
@ -238,16 +242,16 @@ public class FileController {
*/
@RequestMapping(value = "/unzipfile", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> unzipFile(@RequestBody FileBean fileBean, @RequestHeader("token") String token) {
public RestResult<String> unzipFile(@RequestBody UnzipFileDto unzipFileDto, @RequestHeader("token") String token) {
RestResult<String> result = new RestResult<String>();
if (!operationCheck(token).isSuccess()){
return operationCheck(token);
}
String zipFileUrl = PathUtil.getStaticPath() + fileBean.getFileUrl();
String zipFileUrl = PathUtil.getStaticPath() + unzipFileDto.getFileUrl();
File file = FileOperation.newFile(zipFileUrl);
String unzipUrl = file.getParent();
String[] arr = fileBean.getFileUrl().split("\\.");
String[] arr = unzipFileDto.getFileUrl().split("\\.");
if (arr.length <= 1) {
result.setErrorMessage("文件名格式错误!");
result.setSuccess(false);
@ -279,29 +283,34 @@ public class FileController {
File currentFile = FileOperation.newFile(totalFileUrl);
FileBean tempFileBean = new FileBean();
tempFileBean.setUploadTime(DateUtil.getCurrentTime());
tempFileBean.setUserId(sessionUserBean.getUserId());
tempFileBean.setFilePath(FileUtil.pathSplitFormat(fileBean.getFilePath() + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
UserFile userFile = new UserFile();
userFile.setUploadTime(DateUtil.getCurrentTime());
userFile.setUserId(sessionUserBean.getUserId());
userFile.setFilePath(FileUtil.pathSplitFormat(unzipFileDto.getFilePath() + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
if (currentFile.isDirectory()){
tempFileBean.setIsDir(1);
userFile.setIsDir(1);
tempFileBean.setFileName(currentFile.getName());
userFile.setFileName(currentFile.getName());
tempFileBean.setTimeStampName(currentFile.getName());
//tempFileBean.setFileUrl(File.separator + (file.getParent() + File.separator + currentFile.getName()).replace(PathUtil.getStaticPath(), ""));
}else{
tempFileBean.setIsDir(0);
tempFileBean.setExtendName(FileUtil.getFileType(totalFileUrl));
tempFileBean.setFileName(FileUtil.getFileNameNotExtend(currentFile.getName()));
userFile.setIsDir(0);
userFile.setExtendName(FileUtil.getFileType(totalFileUrl));
userFile.setFileName(FileUtil.getFileNameNotExtend(currentFile.getName()));
tempFileBean.setFileSize(currentFile.length());
tempFileBean.setTimeStampName(FileUtil.getFileNameNotExtend(currentFile.getName()));
tempFileBean.setFileUrl(File.separator + (currentFile.getPath()).replace(PathUtil.getStaticPath(), ""));
}
fileBeanList.add(tempFileBean);
fileService.save(tempFileBean);
userFile.setFileId(tempFileBean.getFileId());
userFileService.save(userFile);
//fileBeanList.add(tempFileBean);
}
fileService.batchInsertFile(fileBeanList, sessionUserBean.getUserId());
// fileService.batchInsertFile(fileBeanList, sessionUserBean.getUserId());
result.setSuccess(true);
return result;
@ -325,7 +334,7 @@ public class FileController {
String fileName = moveFileDto.getFileName();
String extendName = moveFileDto.getExtendName();
fileService.updateFilepathByFilepath(oldfilePath, newfilePath, fileName, extendName);
userFileService.updateFilepathByFilepath(oldfilePath, newfilePath, fileName, extendName);
result.setSuccess(true);
return result;
}
@ -348,10 +357,10 @@ public class FileController {
String files = batchMoveFileDto.getFiles();
String newfilePath = batchMoveFileDto.getFilePath();
List<FileBean> fileList = JSON.parseArray(files, FileBean.class);
List<UserFile> fileList = JSON.parseArray(files, UserFile.class);
for (FileBean file : fileList) {
fileService.updateFilepathByFilepath(file.getFilePath(), newfilePath, file.getFileName(), file.getExtendName());
for (UserFile userFile : fileList) {
userFileService.updateFilepathByFilepath(userFile.getFilePath(), newfilePath, userFile.getFileName(), userFile.getExtendName());
}
result.setData("批量移动文件成功");
@ -385,14 +394,14 @@ public class FileController {
*/
@RequestMapping(value = "/selectfilebyfiletype", method = RequestMethod.GET)
@ResponseBody
public RestResult<List<FileBean>> selectFileByFileType(int fileType, @RequestHeader("token") String token) {
RestResult<List<FileBean>> result = new RestResult<List<FileBean>>();
public RestResult<List<Map<String, Object>>> selectFileByFileType(int fileType, @RequestHeader("token") String token) {
RestResult<List<Map<String, Object>>> result = new RestResult<List<Map<String, Object>>>();
UserBean sessionUserBean = userService.getUserBeanByToken(token);
long userId = sessionUserBean.getUserId();
if (qiwenFileConfig.isShareMode()){
userId = 2;
}
List<FileBean> fileList = new ArrayList<>();
List<Map<String, Object>> fileList = new ArrayList<>();
if (fileType == FileUtil.OTHER_TYPE) {
List<String> arrList = new ArrayList<>();
@ -400,9 +409,9 @@ public class FileController {
arrList.addAll(Arrays.asList(FileUtil.IMG_FILE));
arrList.addAll(Arrays.asList(FileUtil.VIDEO_FILE));
arrList.addAll(Arrays.asList(FileUtil.MUSIC_FILE));
fileList = fileService.selectFileNotInExtendNames(arrList, userId);
fileList = userFileService.selectFileNotInExtendNames(arrList, userId);
} else {
fileList = fileService.selectFileByExtendName(getFileExtendsByType(fileType), userId);
fileList = userFileService.selectFileByExtendName(getFileExtendsByType(fileType), userId);
}
result.setData(fileList);
result.setSuccess(true);
@ -417,15 +426,15 @@ public class FileController {
@ResponseBody
public RestResult<TreeNode> getFileTree(@RequestHeader("token") String token){
RestResult<TreeNode> result = new RestResult<TreeNode>();
FileBean fileBean = new FileBean();
UserFile userFile = new UserFile();
UserBean sessionUserBean = userService.getUserBeanByToken(token);
if (qiwenFileConfig.isShareMode()){
fileBean.setUserId(2L);
userFile.setUserId(2L);
}else{
fileBean.setUserId(sessionUserBean.getUserId());
userFile.setUserId(sessionUserBean.getUserId());
}
List<FileBean> filePathList = fileService.selectFilePathTreeByUserId(fileBean);
List<UserFile> filePathList = userFileService.selectFilePathTreeByUserId(sessionUserBean.getUserId());
TreeNode resultTreeNode = new TreeNode();
resultTreeNode.setLabel("/");

View File

@ -13,8 +13,7 @@ import javax.persistence.*;
* @author ma116
*/
@Data
@Table(name = "file", uniqueConstraints = {
@UniqueConstraint(name = "fileindex", columnNames = {"fileName", "filePath", "extendName"})})
@Table(name = "file")
@Entity
@TableName("file")
public class FileBean {

View File

@ -6,7 +6,8 @@ import lombok.Data;
import javax.persistence.*;
@Data
@Table(name = "userfile")
@Table(name = "userfile", uniqueConstraints = {
@UniqueConstraint(name = "fileindex", columnNames = {"fileName", "filePath", "extendName"})})
@Entity
@TableName("userfile")
public class UserFile {

View File

@ -0,0 +1,11 @@
package com.qiwenshare.file.dto;
import lombok.Data;
@Data
public class DeleteFileDto {
private Long userFileId;
private String filePath;
private String fileName;
private Integer isDir;
}

View File

@ -0,0 +1,9 @@
package com.qiwenshare.file.dto;
import lombok.Data;
@Data
public class UnzipFileDto {
private String fileUrl;
private String filePath;
}

View File

@ -15,7 +15,7 @@ public interface FileMapper extends BaseMapper<FileBean> {
void updateFilepathByFilepath(String oldfilePath, String newfilePath);
void updateFilepathByPathAndName(String oldfilePath, String newfilePath, String fileName, String extendName);
}

View File

@ -1,6 +1,7 @@
package com.qiwenshare.file.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.domain.UserFile;
import org.apache.ibatis.annotations.Param;
@ -11,4 +12,10 @@ import java.util.Map;
public interface UserFileMapper extends BaseMapper<UserFile> {
void replaceFilePath(@Param("filePath") String filePath, @Param("oldFilePath") String oldFilePath, @Param("userId") Long userId);
List<Map<String, Object>> userFileList(UserFile userFile);
void updateFilepathByPathAndName(String oldfilePath, String newfilePath, String fileName, String extendName);
void updateFilepathByFilepath(String oldfilePath, String newfilePath);
List<Map<String, Object>> selectFileByExtendName(List<String> fileNameList, long userId);
List<Map<String, Object>> selectFileNotInExtendNames(List<String> fileNameList, long userId);
}

View File

@ -11,10 +11,12 @@ import com.qiwenshare.common.util.PathUtil;
import com.qiwenshare.file.api.IFileService;
import com.qiwenshare.file.config.AliyunConfig;
import com.qiwenshare.file.config.QiwenFileConfig;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.mapper.FileMapper;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.StorageBean;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.mapper.UserFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.SecurityUtils;
@ -31,27 +33,29 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
@Resource
FileMapper fileMapper;
@Resource
UserFileMapper userFileMapper;
@Resource
FiletransferService filetransferService;
@Resource
QiwenFileConfig qiwenFileConfig;
@Override
public void batchInsertFile(List<FileBean> fileBeanList, Long userId) {
StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(userId));
long fileSizeSum = 0;
for (FileBean fileBean : fileBeanList) {
if (fileBean.getIsDir() == 0) {
fileSizeSum += fileBean.getFileSize();
}
}
fileMapper.batchInsertFile(fileBeanList);
if (storageBean != null) {
long updateFileSize = storageBean.getStorageSize() + fileSizeSum;
storageBean.setStorageSize(updateFileSize);
filetransferService.updateStorageBean(storageBean);
}
}
// @Override
// public void batchInsertFile(List<FileBean> fileBeanList, Long userId) {
// StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(userId));
// long fileSizeSum = 0;
// for (FileBean fileBean : fileBeanList) {
// if (fileBean.getIsDir() == 0) {
// fileSizeSum += fileBean.getFileSize();
// }
// }
// fileMapper.batchInsertFile(fileBeanList);
// if (storageBean != null) {
// long updateFileSize = storageBean.getStorageSize() + fileSizeSum;
//
// storageBean.setStorageSize(updateFileSize);
// filetransferService.updateStorageBean(storageBean);
// }
// }
@Override
public void increaseFilePointCount(Long fileId) {
@ -76,143 +80,20 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
@Override
public List<FileBean> selectFilePathTreeByUserId(FileBean fileBean) {
LambdaQueryWrapper<FileBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(FileBean::getUserId, fileBean.getUserId()).eq(FileBean::getIsDir, 1);
return fileMapper.selectList(lambdaQueryWrapper);
}
@Override
public List<FileBean> selectFileListByPath(FileBean fileBean) {
LambdaQueryWrapper<FileBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(FileBean::getFilePath, fileBean.getFilePath())
.eq(FileBean::getUserId, fileBean.getUserId())
.orderByDesc(FileBean::getIsDir);
return fileMapper.selectList(lambdaQueryWrapper);
}
// @Override
// public List<FileBean> selectFileListByPath(FileBean fileBean) {
// LambdaQueryWrapper<FileBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// lambdaQueryWrapper.eq(FileBean::getFilePath, fileBean.getFilePath())
// .eq(FileBean::getUserId, fileBean.getUserId())
// .orderByDesc(FileBean::getIsDir);
// return fileMapper.selectList(lambdaQueryWrapper);
// }
@Override
public List<FileBean> selectFileTreeListLikeFilePath(String filePath) {
FileBean fileBean = new FileBean();
filePath = filePath.replace("\\", "\\\\\\\\");
filePath = filePath.replace("'", "\\'");
filePath = filePath.replace("%", "\\%");
filePath = filePath.replace("_", "\\_");
fileBean.setFilePath(filePath);
LambdaQueryWrapper<FileBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
log.info("删除文件路径:" + filePath);
lambdaQueryWrapper.likeRight(FileBean::getFilePath, filePath);
return fileMapper.selectList(lambdaQueryWrapper);
}
@Override
public void deleteFile(FileBean fileBean, UserBean sessionUserBean) {
StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(sessionUserBean.getUserId()));
long deleteSize = 0;
String fileUrl = PathUtil.getStaticPath() + fileBean.getFileUrl();
if (fileBean.getIsDir() == 1) {
//1先删除子目录
String filePath = fileBean.getFilePath() + fileBean.getFileName() + "/";
List<FileBean> fileList = selectFileTreeListLikeFilePath(filePath);
for (int i = 0; i < fileList.size(); i++){
FileBean file = fileList.get(i);
//1.1删除数据库文件
fileMapper.deleteById(file.getFileId());
//1.2如果是文件需要记录文件大小
if (file.getIsDir() != 1){
deleteSize += file.getFileSize();
//1.3删除服务器文件只删除文件目录是虚拟的
if (file.getFileUrl() != null && file.getFileUrl().indexOf("upload") != -1){
if (file.getIsOSS() != null && file.getIsOSS() == 1) {
AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), file.getFileUrl().substring(1));
} else {
FileOperation.deleteFile(PathUtil.getStaticPath() + file.getFileUrl());
if (FileUtil.isImageFile(file.getExtendName())) {
FileOperation.deleteFile(PathUtil.getStaticPath() + file.getFileUrl().replace(file.getTimeStampName(), file.getTimeStampName() + "_min"));
}
}
}
}
}
//2根目录单独删除
fileMapper.deleteById(fileBean.getFileId());
}else{
fileMapper.deleteById(fileBean.getFileId());
deleteSize = FileOperation.getFileSize(fileUrl);
if (deleteSize == 0) {
deleteSize = fileBean.getFileSize();
}
//删除服务器文件
if (fileBean.getFileUrl() != null && fileBean.getFileUrl().indexOf("upload") != -1){
if (fileBean.getIsOSS() != null && fileBean.getIsOSS() == 1) {
AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), fileBean.getFileUrl().substring(1));
} else {
FileOperation.deleteFile(fileUrl);
if (FileUtil.isImageFile(fileBean.getExtendName())) {
FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl().replace(fileBean.getTimeStampName(), fileBean.getTimeStampName() + "_min"));
}
}
}
}
if (storageBean != null) {
long updateFileSize = storageBean.getStorageSize() - deleteSize;
if (updateFileSize < 0) {
updateFileSize = 0;
}
storageBean.setStorageSize(updateFileSize);
filetransferService.updateStorageBean(storageBean);
}
}
@Override
public void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName) {
if ("null".equals(extendName)){
extendName = null;
}
//移动根目录
fileMapper.updateFilepathByPathAndName(oldfilePath, newfilePath, fileName, extendName);
//移动子目录
oldfilePath = oldfilePath + fileName + "/";
newfilePath = newfilePath + fileName + "/";
oldfilePath = oldfilePath.replace("\\", "\\\\\\\\");
oldfilePath = oldfilePath.replace("'", "\\'");
oldfilePath = oldfilePath.replace("%", "\\%");
oldfilePath = oldfilePath.replace("_", "\\_");
if (extendName == null) { //为null说明是目录则需要移动子目录
fileMapper.updateFilepathByFilepath(oldfilePath, newfilePath);
}
}
@Override
public List<FileBean> selectFileByExtendName(List<String> fileNameList, long userId) {
LambdaQueryWrapper<FileBean> wrapper = new LambdaQueryWrapper<>();
wrapper.in(FileBean::getExtendName, fileNameList).eq(FileBean::getUserId, userId);
List<FileBean> fileBeans = fileMapper.selectList(wrapper);
return fileBeans;
//return fileMapper.selectFileByExtendName(fileNameList, userId);
}
@Override
public List<FileBean> selectFileNotInExtendNames(List<String> fileNameList, long userId) {
LambdaQueryWrapper<FileBean> wrapper = new LambdaQueryWrapper<>();
wrapper.notIn(FileBean::getExtendName, fileNameList).eq(FileBean::getUserId, userId);
List<FileBean> fileBeans = fileMapper.selectList(wrapper);
return fileBeans;
}
}

View File

@ -1,20 +1,36 @@
package com.qiwenshare.file.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiwenshare.common.cbb.DateUtil;
import com.qiwenshare.file.api.IUserFileService;
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.mapper.FileMapper;
import com.qiwenshare.file.mapper.UserFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> implements IUserFileService {
@Resource
UserFileMapper userFileMapper;
@Resource
FileMapper fileMapper;
@Resource
FileService fileService;
@Resource
FiletransferService filetransferService;
@Override
public List<UserFile> selectUserFileByNameAndPath(String fileName, String filePath, Long userId) {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
@ -31,4 +47,167 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
public List<Map<String, Object>> userFileList(UserFile userFile) {
return userFileMapper.userFileList(userFile);
}
@Override
public void updateFilepathByFilepath(String oldfilePath, String newfilePath, String fileName, String extendName) {
if ("null".equals(extendName)){
extendName = null;
}
//移动根目录
userFileMapper.updateFilepathByPathAndName(oldfilePath, newfilePath, fileName, extendName);
//移动子目录
oldfilePath = oldfilePath + fileName + "/";
newfilePath = newfilePath + fileName + "/";
oldfilePath = oldfilePath.replace("\\", "\\\\\\\\");
oldfilePath = oldfilePath.replace("'", "\\'");
oldfilePath = oldfilePath.replace("%", "\\%");
oldfilePath = oldfilePath.replace("_", "\\_");
if (extendName == null) { //为null说明是目录则需要移动子目录
userFileMapper.updateFilepathByFilepath(oldfilePath, newfilePath);
}
}
@Override
public List<Map<String, Object>> selectFileByExtendName(List<String> fileNameList, long userId) {
// LambdaQueryWrapper<FileBean> wrapper = new LambdaQueryWrapper<>();
// wrapper.in(FileBean::getExtendName, fileNameList).eq(FileBean::getUserId, userId);
// List<FileBean> fileBeans = fileMapper.selectList(wrapper);
// return fileBeans;
return userFileMapper.selectFileByExtendName(fileNameList, userId);
}
@Override
public List<Map<String, Object>> selectFileNotInExtendNames(List<String> fileNameList, long userId) {
// LambdaQueryWrapper<FileBean> wrapper = new LambdaQueryWrapper<>();
// wrapper.notIn(FileBean::getExtendName, fileNameList).eq(FileBean::getUserId, userId);
// List<FileBean> fileBeans = fileMapper.selectList(wrapper);
return userFileMapper.selectFileNotInExtendNames(fileNameList, userId);
}
@Override
public List<UserFile> selectFileTreeListLikeFilePath(String filePath) {
UserFile userFile = new UserFile();
filePath = filePath.replace("\\", "\\\\\\\\");
filePath = filePath.replace("'", "\\'");
filePath = filePath.replace("%", "\\%");
filePath = filePath.replace("_", "\\_");
userFile.setFilePath(filePath);
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
log.info("删除文件路径:" + filePath);
lambdaQueryWrapper.likeRight(UserFile::getFilePath, filePath);
return userFileMapper.selectList(lambdaQueryWrapper);
}
@Override
public List<UserFile> selectFilePathTreeByUserId(Long userId) {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getUserId, userId)
.eq(UserFile::getIsDir, 1);
return userFileMapper.selectList(lambdaQueryWrapper);
}
@Override
public void deleteUserFile(UserFile userFile, UserBean sessionUserBean) {
StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(sessionUserBean.getUserId()));
long deleteSize = 0;
//String fileUrl = PathUtil.getStaticPath() + fileBean.getFileUrl();
if (userFile.getIsDir() == 1) {
//1先删除子目录
String filePath = userFile.getFilePath() + userFile.getFileName() + "/";
List<UserFile> fileList = selectFileTreeListLikeFilePath(filePath);
for (int i = 0; i < fileList.size(); i++){
UserFile userFileTemp = fileList.get(i);
//1.1删除数据库文件
//userFileMapper.deleteById(userFileTemp.getUserFileId());
//1.2如果是文件需要记录文件大小
if (userFileTemp.getIsDir() != 1){
FileBean fileBean = fileMapper.selectById(userFileTemp.getFileId());
deleteSize += fileBean.getFileSize();
//1.3删除服务器文件只删除文件目录是虚拟的
// if (file.getFileUrl() != null && file.getFileUrl().indexOf("upload") != -1){
// if (file.getIsOSS() != null && file.getIsOSS() == 1) {
// AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), file.getFileUrl().substring(1));
// } else {
// FileOperation.deleteFile(PathUtil.getStaticPath() + file.getFileUrl());
// if (FileUtil.isImageFile(file.getExtendName())) {
// FileOperation.deleteFile(PathUtil.getStaticPath() + file.getFileUrl().replace(file.getTimeStampName(), file.getTimeStampName() + "_min"));
// }
// }
//
// }
LambdaUpdateWrapper<FileBean> fileBeanLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
fileBeanLambdaUpdateWrapper.set(FileBean::getPointCount, fileBean.getPointCount() -1)
.eq(FileBean::getFileId, fileBean.getFileId());
// fileService.decreaseFilePointCount(fileBean.getFileId());
}
//标记删除标志
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1)
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId());
userFileMapper.update(null, userFileLambdaUpdateWrapper);
}
//2根目录单独删除
//userFileMapper.deleteById(userFile.getUserFileId());
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1)
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
.eq(UserFile::getUserFileId, userFile.getUserFileId());
userFileMapper.update(null, userFileLambdaUpdateWrapper);
}else{
//userFileMapper.deleteById(userFile.getUserFileId());
UserFile userFileTemp = userFileMapper.selectById(userFile.getUserFileId());
FileBean fileBean = fileMapper.selectById(userFileTemp.getFileId());
deleteSize = fileBean.getFileSize();
//删除服务器文件
// if (fileBean.getFileUrl() != null && fileBean.getFileUrl().indexOf("upload") != -1){
// if (fileBean.getIsOSS() != null && fileBean.getIsOSS() == 1) {
// AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), fileBean.getFileUrl().substring(1));
// } else {
// FileOperation.deleteFile(fileUrl);
// if (FileUtil.isImageFile(fileBean.getExtendName())) {
// FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl().replace(fileBean.getTimeStampName(), fileBean.getTimeStampName() + "_min"));
// }
// }
// }
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1)
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId());
userFileMapper.update(null, userFileLambdaUpdateWrapper);
LambdaUpdateWrapper<FileBean> fileBeanLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
fileBeanLambdaUpdateWrapper.set(FileBean::getPointCount, fileBean.getPointCount() -1)
.eq(FileBean::getFileId, fileBean.getFileId());
}
if (storageBean != null) {
long updateFileSize = storageBean.getStorageSize() - deleteSize;
if (updateFileSize < 0) {
updateFileSize = 0;
}
storageBean.setStorageSize(updateFileSize);
filetransferService.updateStorageBean(storageBean);
}
}
}

View File

@ -47,21 +47,6 @@
<!-- </update>-->
<update id="updateFilepathByFilepath">
UPDATE file SET filePath=REPLACE(filePath, #{param1}, #{param2})
WHERE filePath like N'${param1}%'
</update>
<update id="updateFilepathByPathAndName">
update file set filePath = #{param2}
where filePath = #{param1} and fileName = #{param3}
<if test="param4 != null">
and extendName = #{param4}
</if>
<if test="param4 == null">
and extendName is null
</if>
</update>

View File

@ -14,8 +14,55 @@
<select id="userFileList" parameterType="com.qiwenshare.file.domain.UserFile" resultType="java.util.Map">
select * from userfile
left join file on file.fileId = userfile.fileId
where userId = #{userId} and filePath = #{filePath}
<where>
<if test="userId != null">
and userId = #{userId}
</if>
<if test="filePath != null">
and filePath = #{filePath}
</if>
<if test="extendName != null">
and extendName = #{extendName}
</if>
</where>
</select>
<select id="selectFileByExtendName" parameterType="com.qiwenshare.file.domain.UserFile" resultType="java.util.Map">
select * from userfile
left join file on file.fileId = userfile.fileId
where extendName in
<foreach collection="fileNameList" open="(" close=")" separator="," item="fileName" >
#{fileName}
</foreach>
and userId = #{userId}
</select>
<select id="selectFileNotInExtendNames" parameterType="com.qiwenshare.file.domain.UserFile" resultType="java.util.Map">
select * from userfile
left join file on file.fileId = userfile.fileId
where extendName not in
<foreach collection="fileNameList" open="(" close=")" separator="," item="fileName" >
#{fileName}
</foreach>
and userId = #{userId}
</select>
<update id="updateFilepathByFilepath">
UPDATE file SET filePath=REPLACE(filePath, #{param1}, #{param2})
WHERE filePath like N'${param1}%'
</update>
<update id="updateFilepathByPathAndName">
update userfile set filePath = #{param2}
where filePath = #{param1} and fileName = #{param3}
<if test="param4 != null">
and extendName = #{param4}
</if>
<if test="param4 == null">
and extendName is null
</if>
</update>
</mapper>