代码优化

This commit is contained in:
马超 2020-12-11 18:42:04 +08:00
parent 4fd4e96906
commit 3646db5880
13 changed files with 213 additions and 105 deletions

View File

@ -10,11 +10,12 @@ public interface IFileService extends IService<FileBean> {
void batchInsertFile(List<FileBean> fileBeanList, Long userId);
void updateFile(FileBean fileBean);
List<FileBean> selectFileByNameAndPath(FileBean fileBean);
//void updateFile(FileBean fileBean);
List<FileBean> selectFileByNameAndPath(String fileName, String filePath);
List<FileBean> selectFilePathTreeByUserId(FileBean fileBean);
List<FileBean> selectFileListByPath(FileBean fileBean);
void replaceFilePath(String filePath, String oldFilePath);
List<FileBean> selectFileTreeListLikeFilePath(String filePath);

View File

@ -4,6 +4,7 @@ import com.qiwenshare.common.cbb.RestResult;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.StorageBean;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.dto.UploadFileDto;
import java.util.List;
@ -18,9 +19,9 @@ public interface IFiletransferService {
/**
* 上传文件
* @param request 请求
* @param fileBean 文件信息
* @param UploadFileDto 文件信息
*/
void uploadFile(HttpServletRequest request, FileBean fileBean, UserBean sessionUserBean);
void uploadFile(HttpServletRequest request, UploadFileDto UploadFileDto, UserBean sessionUserBean);
StorageBean selectStorageBean(StorageBean storageBean);

View File

@ -2,6 +2,7 @@ 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.cbb.DateUtil;
import com.qiwenshare.common.cbb.RestResult;
import com.qiwenshare.common.operation.FileOperation;
@ -14,6 +15,10 @@ import com.qiwenshare.file.config.QiwenFileConfig;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.TreeNode;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.dto.BatchDeleteFileDto;
import com.qiwenshare.file.dto.BatchMoveFileDto;
import com.qiwenshare.file.dto.MoveFileDto;
import com.qiwenshare.file.dto.RenameFileDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.*;
@ -52,7 +57,7 @@ public class FileController {
if (!operationCheck(token).isSuccess()){
return operationCheck(token);
}
List<FileBean> fileBeans = fileService.selectFileByNameAndPath(fileBean);
List<FileBean> fileBeans = fileService.selectFileByNameAndPath(fileBean.getFileName(), fileBean.getFilePath());
if (fileBeans != null && !fileBeans.isEmpty()) {
restResult.setErrorMessage("同名文件已存在");
restResult.setSuccess(false);
@ -76,36 +81,57 @@ public class FileController {
*/
@RequestMapping(value = "/renamefile", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> renameFile(@RequestBody FileBean fileBean, @RequestHeader("token") String token) {
public RestResult<String> renameFile(@RequestBody RenameFileDto renameFileDto, @RequestHeader("token") String token) {
RestResult<String> restResult = new RestResult<>();
if (!operationCheck(token).isSuccess()){
return operationCheck(token);
}
UserBean sessionUserBean = userService.getUserBeanByToken(token);
fileBean.setUserId(sessionUserBean.getUserId());
fileBean.setUploadTime(DateUtil.getCurrentTime());
List<FileBean> fileBeans = fileService.selectFileByNameAndPath(fileBean);
// fileBean.setUserId(sessionUserBean.getUserId());
// fileBean.setUploadTime(DateUtil.getCurrentTime());
List<FileBean> fileBeans = fileService.selectFileByNameAndPath(renameFileDto.getFileName(), renameFileDto.getFilePath());
if (fileBeans != null && !fileBeans.isEmpty()) {
restResult.setErrorMessage("同名文件已存在");
restResult.setSuccess(false);
return restResult;
}
if (1 == fileBean.getIsDir()) {
fileBean.setOldFilePath(fileBean.getFilePath() + fileBean.getOldFileName() + "/");
fileBean.setFilePath(fileBean.getFilePath() + fileBean.getFileName() + "/");
if (1 == renameFileDto.getIsDir()) {
LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.set(FileBean::getFileName, renameFileDto.getFileName())
.set(FileBean::getUploadTime, DateUtil.getCurrentTime())
.eq(FileBean::getFileId, renameFileDto.getFileId());
fileService.update(lambdaUpdateWrapper);
fileService.replaceFilePath(renameFileDto.getFilePath() + renameFileDto.getFileName() + "/",
renameFileDto.getFilePath() + renameFileDto.getOldFileName() + "/");
// fileBean.setOldFilePath(renameFileDto.getFilePath() + renameFileDto.getOldFileName() + "/");
// fileBean.setFilePath(renameFileDto.getFilePath() + renameFileDto.getFileName() + "/");
} else {
if (fileBean.getIsOSS() == 1) {
FileBean file = fileService.getById(fileBean.getFileId());
if (renameFileDto.getIsOSS() == 1) {
FileBean file = fileService.getById(renameFileDto.getFileId());
String fileUrl = file.getFileUrl();
String newFileUrl = fileUrl.replace(file.getFileName(), fileBean.getFileName());
fileBean.setFileUrl(newFileUrl);
String newFileUrl = fileUrl.replace(file.getFileName(), renameFileDto.getFileName());
// renameFileDto.setFileUrl(newFileUrl);
AliyunOSSRename.rename(qiwenFileConfig.getAliyun().getOss(),
fileUrl.substring(1),
newFileUrl.substring(1));
LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.set(FileBean::getFileName, renameFileDto.getFileName())
.set(FileBean::getUploadTime, DateUtil.getCurrentTime())
.set(FileBean::getFileUrl, newFileUrl)
.eq(FileBean::getFileId, renameFileDto.getFileId());
fileService.update(lambdaUpdateWrapper);
} else {
LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.set(FileBean::getFileName, renameFileDto.getFileName())
.set(FileBean::getUploadTime, DateUtil.getCurrentTime())
.eq(FileBean::getFileId, renameFileDto.getFileId());
fileService.update(lambdaUpdateWrapper);
}
}
fileService.updateFile(fileBean);
// fileService.updateFile(fileBean);
restResult.setSuccess(true);
return restResult;
}
@ -148,13 +174,13 @@ public class FileController {
*/
@RequestMapping(value = "/batchdeletefile", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> deleteImageByIds(@RequestBody FileBean fileBean, @RequestHeader("token") String token) {
public RestResult<String> deleteImageByIds(@RequestBody BatchDeleteFileDto batchDeleteFileDto, @RequestHeader("token") String token) {
RestResult<String> result = new RestResult<String>();
if (!operationCheck(token).isSuccess()) {
return operationCheck(token);
}
UserBean sessionUserBean = userService.getUserBeanByToken(token);
List<FileBean> fileList = JSON.parseArray(fileBean.getFiles(), FileBean.class);
List<FileBean> fileList = JSON.parseArray(batchDeleteFileDto.getFiles(), FileBean.class);
for (FileBean file : fileList) {
fileService.deleteFile(file,sessionUserBean);
@ -269,15 +295,15 @@ public class FileController {
*/
@RequestMapping(value = "/movefile", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> moveFile(@RequestBody FileBean fileBean, @RequestHeader("token") String token) {
public RestResult<String> moveFile(@RequestBody MoveFileDto moveFileDto, @RequestHeader("token") String token) {
RestResult<String> result = new RestResult<String>();
if (!operationCheck(token).isSuccess()){
return operationCheck(token);
}
String oldfilePath = fileBean.getOldFilePath();
String newfilePath = fileBean.getFilePath();
String fileName = fileBean.getFileName();
String extendName = fileBean.getExtendName();
String oldfilePath = moveFileDto.getOldFilePath();
String newfilePath = moveFileDto.getFilePath();
String fileName = moveFileDto.getFileName();
String extendName = moveFileDto.getExtendName();
fileService.updateFilepathByFilepath(oldfilePath, newfilePath, fileName, extendName);
result.setSuccess(true);
@ -292,15 +318,15 @@ public class FileController {
*/
@RequestMapping(value = "/batchmovefile", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> batchMoveFile(@RequestBody FileBean fileBean, @RequestHeader("token") String token) {
public RestResult<String> batchMoveFile(@RequestBody BatchMoveFileDto batchMoveFileDto, @RequestHeader("token") String token) {
RestResult<String> result = new RestResult<String>();
if (!operationCheck(token).isSuccess()) {
return operationCheck(token);
}
String files = fileBean.getFiles();
String newfilePath = fileBean.getFilePath();
String files = batchMoveFileDto.getFiles();
String newfilePath = batchMoveFileDto.getFilePath();
List<FileBean> fileList = JSON.parseArray(files, FileBean.class);
@ -339,7 +365,7 @@ public class FileController {
*/
@RequestMapping(value = "/selectfilebyfiletype", method = RequestMethod.GET)
@ResponseBody
public RestResult<List<FileBean>> selectFileByFileType(FileBean fileBean, @RequestHeader("token") String token) {
public RestResult<List<FileBean>> selectFileByFileType(int fileType, @RequestHeader("token") String token) {
RestResult<List<FileBean>> result = new RestResult<List<FileBean>>();
UserBean sessionUserBean = userService.getUserBeanByToken(token);
long userId = sessionUserBean.getUserId();
@ -347,7 +373,7 @@ public class FileController {
userId = 2;
}
List<FileBean> fileList = new ArrayList<>();
if (fileBean.getFileType() == FileUtil.OTHER_TYPE) {
if (fileType == FileUtil.OTHER_TYPE) {
List<String> arrList = new ArrayList<>();
arrList.addAll(Arrays.asList(FileUtil.DOC_FILE));
@ -356,7 +382,7 @@ public class FileController {
arrList.addAll(Arrays.asList(FileUtil.MUSIC_FILE));
fileList = fileService.selectFileNotInExtendNames(arrList, userId);
} else {
fileList = fileService.selectFileByExtendName(getFileExtendsByType(fileBean.getFileType()), userId);
fileList = fileService.selectFileByExtendName(getFileExtendsByType(fileType), userId);
}
result.setData(fileList);
result.setSuccess(true);

View File

@ -120,13 +120,12 @@ public class FiletransferController {
restResult.setErrorMessage("没权限,请联系管理员!");
return restResult;
}
FileBean fileBean = new FileBean();
BeanUtil.copyProperties(uploadFileDto, fileBean);
fileBean.setUserId(sessionUserBean.getUserId());
filetransferService.uploadFile(request, fileBean, sessionUserBean);
uploadFileDto.setUserId(sessionUserBean.getUserId());
filetransferService.uploadFile(request, uploadFileDto, sessionUserBean);
UploadFileVo uploadFileVo = new UploadFileVo();
uploadFileVo.setTimeStampName(fileBean.getTimeStampName());
uploadFileVo.setTimeStampName(uploadFileDto.getTimeStampName());
restResult.setData(uploadFileVo);
return restResult;
}

View File

@ -68,41 +68,41 @@ public class FileBean {
@Column(columnDefinition="varchar(32) comment 'md5标识'")
private String identifier;
@Transient
@TableField(exist = false)
private String oldFilePath;
// @Transient
// @TableField(exist = false)
// private String oldFilePath;
//
// @Transient
// @TableField(exist = false)
// private String oldFileName;
@Transient
@TableField(exist = false)
private String oldFileName;
// @Transient
// @TableField(exist = false)
// private String files;
@Transient
@TableField(exist = false)
private String files;
@Transient
@TableField(exist = false)
private Integer fileType;
// @Transient
// @TableField(exist = false)
// private Integer fileType;
//切片上传相关参数
@Transient
@TableField(exist = false)
private String taskId;
@Transient
@TableField(exist = false)
private int chunkNumber;
@Transient
@TableField(exist = false)
private long chunkSize;
@Transient
@TableField(exist = false)
private int totalChunks;
@Transient
@TableField(exist = false)
private long totalSize;
@Transient
@TableField(exist = false)
private long currentChunkSize;
// @Transient
// @TableField(exist = false)
// private String taskId;
// @Transient
// @TableField(exist = false)
// private int chunkNumber;
// @Transient
// @TableField(exist = false)
// private long chunkSize;
// @Transient
// @TableField(exist = false)
// private int totalChunks;
// @Transient
// @TableField(exist = false)
// private long totalSize;
// @Transient
// @TableField(exist = false)
// private long currentChunkSize;
}

View File

@ -0,0 +1,12 @@
package com.qiwenshare.file.dto;
import lombok.Data;
@Data
public class BatchDeleteFileDto {
private String files;
}

View File

@ -0,0 +1,12 @@
package com.qiwenshare.file.dto;
import lombok.Data;
@Data
public class BatchMoveFileDto {
private String files;
private String filePath;
}

View File

@ -0,0 +1,20 @@
package com.qiwenshare.file.dto;
import lombok.Data;
@Data
public class MoveFileDto {
/**
* 文件路径
*/
private String filePath;
/**
* 文件名
*/
private String fileName;
private String oldFilePath;
private String extendName;
}

View File

@ -0,0 +1,22 @@
package com.qiwenshare.file.dto;
import lombok.Data;
@Data
public class RenameFileDto {
private Long fileId;
/**
* 文件路径
*/
private String filePath;
/**
* 文件名
*/
private String fileName;
private Integer isDir;
private String oldFileName;
private Integer isOSS;
}

View File

@ -11,7 +11,9 @@ public interface FileMapper extends BaseMapper<FileBean> {
void batchInsertFile(List<FileBean> fileBeanList);
void updateFile(FileBean fileBean);
// void updateFile(FileBean fileBean);
void replaceFilePath(@Param("filePath") String filePath, @Param("oldFilePath") String oldFilePath);
void updateFilepathByFilepath(String oldfilePath, String newfilePath);
void updateFilepathByPathAndName(String oldfilePath, String newfilePath, String fileName, String extendName);

View File

@ -53,16 +53,16 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
}
}
@Override
public void updateFile(FileBean fileBean) {
fileBean.setUploadTime(DateUtil.getCurrentTime());
fileMapper.updateFile(fileBean);
}
// @Override
// public void updateFile(FileBean fileBean) {
// fileBean.setUploadTime(DateUtil.getCurrentTime());
// fileMapper.updateFile(fileBean);
// }
@Override
public List<FileBean> selectFileByNameAndPath(FileBean fileBean) {
public List<FileBean> selectFileByNameAndPath(String fileName, String filePath) {
LambdaQueryWrapper<FileBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(FileBean::getFileName, fileBean.getFileName()).eq(FileBean::getFilePath, fileBean.getFilePath());
lambdaQueryWrapper.eq(FileBean::getFileName, fileName).eq(FileBean::getFilePath, filePath);
return fileMapper.selectList(lambdaQueryWrapper);
}
@ -82,6 +82,11 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
return fileMapper.selectList(lambdaQueryWrapper);
}
@Override
public void replaceFilePath(String filePath, String oldFilePath) {
fileMapper.replaceFilePath(filePath, oldFilePath);
}
@Override
public List<FileBean> selectFileTreeListLikeFilePath(String filePath) {

View File

@ -5,6 +5,7 @@ import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
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.cbb.DateUtil;
@ -16,6 +17,7 @@ import com.qiwenshare.file.api.IFiletransferService;
import com.qiwenshare.common.domain.AliyunOSS;
import com.qiwenshare.file.config.QiwenFileConfig;
import com.qiwenshare.file.dto.UploadFileDto;
import com.qiwenshare.file.mapper.FileMapper;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.StorageBean;
@ -38,17 +40,17 @@ public class FiletransferService implements IFiletransferService {
@Override
public void uploadFile(HttpServletRequest request, FileBean fileBean, UserBean sessionUserBean) {
public void uploadFile(HttpServletRequest request, UploadFileDto UploadFileDto, UserBean sessionUserBean) {
AliyunOSS oss = qiwenFileConfig.getAliyun().getOss();
request.setAttribute("oss", oss);
Uploader uploader;
UploadFile uploadFile = new UploadFile();
uploadFile.setChunkNumber(fileBean.getChunkNumber());
uploadFile.setChunkSize(fileBean.getChunkSize());
uploadFile.setTotalChunks(fileBean.getTotalChunks());
uploadFile.setIdentifier(fileBean.getIdentifier());
uploadFile.setTotalSize(fileBean.getTotalSize());
uploadFile.setCurrentChunkSize(fileBean.getCurrentChunkSize());
uploadFile.setChunkNumber(UploadFileDto.getChunkNumber());
uploadFile.setChunkSize(UploadFileDto.getChunkSize());
uploadFile.setTotalChunks(UploadFileDto.getTotalChunks());
uploadFile.setIdentifier(UploadFileDto.getIdentifier());
uploadFile.setTotalSize(UploadFileDto.getTotalSize());
uploadFile.setCurrentChunkSize(UploadFileDto.getCurrentChunkSize());
if (oss.isEnabled()) {
uploader = new AliyunOSSUploaderFactory().getUploader(uploadFile);
} else {
@ -58,6 +60,8 @@ public class FiletransferService implements IFiletransferService {
List<UploadFile> uploadFileList = uploader.upload(request);
for (int i = 0; i < uploadFileList.size(); i++){
uploadFile = uploadFileList.get(i);
FileBean fileBean = new FileBean();
BeanUtil.copyProperties(UploadFileDto, fileBean);
fileBean.setTimeStampName(uploadFile.getTimeStampName());
if (uploadFile.getSuccess() == 1){
fileBean.setFileUrl(uploadFile.getUrl());

View File

@ -18,32 +18,36 @@
</foreach>
</insert>
<update id="updateFile" parameterType="com.qiwenshare.file.domain.FileBean">
<choose>
<when test="isDir == 1">
UPDATE file SET filename=#{fileName}, uploadTime = #{uploadTime}
where fileId = #{fileId};
UPDATE file SET filepath=REPLACE(filepath, #{oldFilePath}, #{filePath}) WHERE filepath LIKE N'${oldFilePath}%';
</when>
<otherwise>
update file
<set>
<if test="fileName != null">
fileName = #{fileName},
</if>
<if test="uploadTime != null">
uploadTime = #{uploadTime},
</if>
<if test="fileUrl != null">
fileUrl = #{fileUrl},
</if>
</set>
where fileId = #{fileId}
</otherwise>
</choose>
<update id="replaceFilePath" parameterType="java.lang.String">
UPDATE file SET filepath=REPLACE(filepath, #{oldFilePath}, #{filePath}) WHERE filepath LIKE N'${oldFilePath}%';
</update>
<!-- <update id="updateFile" parameterType="java">-->
<!-- <choose>-->
<!-- <when test="isDir == 1">-->
<!-- UPDATE file SET filename=#{fileName}, uploadTime = #{uploadTime}-->
<!-- where fileId = #{fileId};-->
<!-- UPDATE file SET filepath=REPLACE(filepath, #{oldFilePath}, #{filePath}) WHERE filepath LIKE N'${oldFilePath}%';-->
<!-- </when>-->
<!-- <otherwise>-->
<!-- update file-->
<!-- <set>-->
<!-- <if test="fileName != null">-->
<!-- fileName = #{fileName},-->
<!-- </if>-->
<!-- <if test="uploadTime != null">-->
<!-- uploadTime = #{uploadTime},-->
<!-- </if>-->
<!-- <if test="fileUrl != null">-->
<!-- fileUrl = #{fileUrl},-->
<!-- </if>-->
<!-- </set>-->
<!-- where fileId = #{fileId}-->
<!-- </otherwise>-->
<!-- </choose>-->
<!-- </update>-->
<update id="updateFilepathByFilepath">
UPDATE file SET filePath=REPLACE(filePath, #{param1}, #{param2})