回收站功能完善
This commit is contained in:
parent
be257c6ac0
commit
dcaafdb079
@ -12,6 +12,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
@MapperScan("com.qiwenshare.file.mapper")
|
@MapperScan("com.qiwenshare.file.mapper")
|
||||||
|
@EnableScheduling
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
public class FileApplication {
|
public class FileApplication {
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public interface IFileService extends IService<FileBean> {
|
|||||||
|
|
||||||
// List<FileBean> selectFileListByPath(FileBean fileBean);
|
// List<FileBean> selectFileListByPath(FileBean fileBean);
|
||||||
|
|
||||||
|
void deleteLocalFile(FileBean fileBean);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,4 +19,5 @@ public interface IUserFileService extends IService<UserFile> {
|
|||||||
List<UserFile> selectFileTreeListLikeFilePath(String filePath);
|
List<UserFile> selectFileTreeListLikeFilePath(String filePath);
|
||||||
List<UserFile> selectFilePathTreeByUserId(Long userId);
|
List<UserFile> selectFilePathTreeByUserId(Long userId);
|
||||||
void deleteUserFile(UserFile userFile, UserBean sessionUserBean);
|
void deleteUserFile(UserFile userFile, UserBean sessionUserBean);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -319,10 +319,10 @@ public class FileController {
|
|||||||
tempFileBean.setFileSize(currentFile.length());
|
tempFileBean.setFileSize(currentFile.length());
|
||||||
tempFileBean.setTimeStampName(FileUtil.getFileNameNotExtend(currentFile.getName()));
|
tempFileBean.setTimeStampName(FileUtil.getFileNameNotExtend(currentFile.getName()));
|
||||||
tempFileBean.setFileUrl(File.separator + (currentFile.getPath()).replace(PathUtil.getStaticPath(), ""));
|
tempFileBean.setFileUrl(File.separator + (currentFile.getPath()).replace(PathUtil.getStaticPath(), ""));
|
||||||
|
tempFileBean.setPointCount(1);
|
||||||
|
fileService.save(tempFileBean);
|
||||||
}
|
}
|
||||||
tempFileBean.setPointCount(1);
|
|
||||||
|
|
||||||
fileService.save(tempFileBean);
|
|
||||||
userFile.setFileId(tempFileBean.getFileId());
|
userFile.setFileId(tempFileBean.getFileId());
|
||||||
userFile.setDeleteFlag(0);
|
userFile.setDeleteFlag(0);
|
||||||
userFileService.save(userFile);
|
userFileService.save(userFile);
|
||||||
|
@ -89,6 +89,8 @@ public class FiletransferController {
|
|||||||
userFile.setFileName(fileName.substring(0, fileName.lastIndexOf(".")));
|
userFile.setFileName(fileName.substring(0, fileName.lastIndexOf(".")));
|
||||||
userFile.setExtendName(FileUtil.getFileType(fileName));
|
userFile.setExtendName(FileUtil.getFileType(fileName));
|
||||||
userFile.setDeleteFlag(0);
|
userFile.setDeleteFlag(0);
|
||||||
|
userFile.setIsDir(0);
|
||||||
|
userFile.setUploadTime(DateUtil.getCurrentTime());
|
||||||
userFileService.save(userFile);
|
userFileService.save(userFile);
|
||||||
fileService.increaseFilePointCount(file.getFileId());
|
fileService.increaseFilePointCount(file.getFileId());
|
||||||
uploadFileVo.setSkipUpload(true);
|
uploadFileVo.setSkipUpload(true);
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.qiwenshare.file.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.qiwenshare.file.domain.FileBean;
|
||||||
|
import com.qiwenshare.file.domain.UserFile;
|
||||||
|
import com.qiwenshare.file.service.FileService;
|
||||||
|
import com.qiwenshare.file.service.UserFileService;
|
||||||
|
import com.qiwenshare.file.service.UserService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class TaskController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
UserFileService userFileService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
FileService fileService;
|
||||||
|
|
||||||
|
@Scheduled(cron = "* * * 0/1 * ?")
|
||||||
|
public void deleteFile() {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.add(Calendar.DATE, -3);
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String threeDaysAgo = sdf.format(calendar.getTime());
|
||||||
|
LambdaQueryWrapper<UserFile> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
userFileLambdaQueryWrapper.eq(UserFile::getDeleteFlag, 1)
|
||||||
|
.lt(UserFile::getDeleteTime, threeDaysAgo + " 00:00:00");
|
||||||
|
List<UserFile> userFiles = userFileService.list(userFileLambdaQueryWrapper);
|
||||||
|
for (UserFile userFile : userFiles) {
|
||||||
|
userFileService.removeById(userFile.getUserFileId());
|
||||||
|
FileBean fileBean = fileService.getById(userFile.getFileId());
|
||||||
|
Integer pointCount = fileBean.getPointCount();
|
||||||
|
if (pointCount <= 1) {
|
||||||
|
fileService.removeById(fileBean.getFileId());
|
||||||
|
fileService.deleteLocalFile(fileBean);
|
||||||
|
} else {
|
||||||
|
fileBean.setPointCount(fileBean.getPointCount() - 1);
|
||||||
|
fileService.updateById(fileBean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("11111");
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package com.qiwenshare.file.service;
|
package com.qiwenshare.file.service;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
@ -88,8 +89,21 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
|
|||||||
// .orderByDesc(FileBean::getIsDir);
|
// .orderByDesc(FileBean::getIsDir);
|
||||||
// return fileMapper.selectList(lambdaQueryWrapper);
|
// return fileMapper.selectList(lambdaQueryWrapper);
|
||||||
// }
|
// }
|
||||||
|
@Override
|
||||||
|
public void deleteLocalFile(FileBean fileBean) {
|
||||||
|
log.info("删除本地文件:" + JSON.toJSONString(fileBean));
|
||||||
|
//删除服务器文件
|
||||||
|
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(PathUtil.getStaticPath() + fileBean.getFileUrl());
|
||||||
|
if (FileUtil.isImageFile(FileUtil.getFileType(fileBean.getFileUrl()))) {
|
||||||
|
FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl().replace(fileBean.getTimeStampName(), fileBean.getTimeStampName() + "_min"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.qiwenshare.common.cbb.DateUtil;
|
import com.qiwenshare.common.cbb.DateUtil;
|
||||||
|
import com.qiwenshare.common.operation.FileOperation;
|
||||||
|
import com.qiwenshare.common.oss.AliyunOSSDelete;
|
||||||
|
import com.qiwenshare.common.util.FileUtil;
|
||||||
|
import com.qiwenshare.common.util.PathUtil;
|
||||||
import com.qiwenshare.file.api.IUserFileService;
|
import com.qiwenshare.file.api.IUserFileService;
|
||||||
|
import com.qiwenshare.file.config.QiwenFileConfig;
|
||||||
import com.qiwenshare.file.domain.FileBean;
|
import com.qiwenshare.file.domain.FileBean;
|
||||||
import com.qiwenshare.file.domain.StorageBean;
|
import com.qiwenshare.file.domain.StorageBean;
|
||||||
import com.qiwenshare.file.domain.UserBean;
|
import com.qiwenshare.file.domain.UserBean;
|
||||||
@ -17,6 +22,8 @@ import org.springframework.stereotype.Service;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@ -26,9 +33,11 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
|
|||||||
@Resource
|
@Resource
|
||||||
FileMapper fileMapper;
|
FileMapper fileMapper;
|
||||||
@Resource
|
@Resource
|
||||||
FileService fileService;
|
|
||||||
@Resource
|
|
||||||
FiletransferService filetransferService;
|
FiletransferService filetransferService;
|
||||||
|
@Resource
|
||||||
|
QiwenFileConfig qiwenFileConfig;
|
||||||
|
|
||||||
|
public static Executor executor = Executors.newFixedThreadPool(20);
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -121,61 +130,25 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
|
|||||||
@Override
|
@Override
|
||||||
public void deleteUserFile(UserFile userFile, UserBean sessionUserBean) {
|
public void deleteUserFile(UserFile userFile, UserBean sessionUserBean) {
|
||||||
StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(sessionUserBean.getUserId()));
|
StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(sessionUserBean.getUserId()));
|
||||||
long deleteSize = 0;
|
|
||||||
|
|
||||||
if (userFile.getIsDir() == 1) {
|
if (userFile.getIsDir() == 1) {
|
||||||
//1、先删除子目录
|
|
||||||
String filePath = userFile.getFilePath() + userFile.getFileName() + "/";
|
|
||||||
List<UserFile> fileList = selectFileTreeListLikeFilePath(filePath);
|
|
||||||
|
|
||||||
for (int i = 0; i < fileList.size(); i++){
|
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<UserFile>();
|
||||||
UserFile userFileTemp = fileList.get(i);
|
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1).set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
||||||
|
|
||||||
if (userFileTemp.getIsDir() != 1){
|
|
||||||
FileBean fileBean = fileMapper.selectById(userFileTemp.getFileId());
|
|
||||||
deleteSize += fileBean.getFileSize();
|
|
||||||
if (fileBean.getPointCount() != null) {
|
|
||||||
|
|
||||||
LambdaUpdateWrapper<FileBean> fileBeanLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
||||||
fileBeanLambdaUpdateWrapper.set(FileBean::getPointCount, fileBean.getPointCount() -1)
|
|
||||||
.eq(FileBean::getFileId, fileBean.getFileId());
|
|
||||||
fileMapper.update(null, fileBeanLambdaUpdateWrapper);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//标记删除标志
|
|
||||||
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<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
||||||
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1)
|
|
||||||
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
|
||||||
.eq(UserFile::getUserFileId, userFile.getUserFileId());
|
.eq(UserFile::getUserFileId, userFile.getUserFileId());
|
||||||
userFileMapper.update(null, userFileLambdaUpdateWrapper);
|
userFileMapper.update(null, userFileLambdaUpdateWrapper);
|
||||||
|
|
||||||
|
String filePath = userFile.getFilePath() + userFile.getFileName() + "/";
|
||||||
|
updateFileDeleteStateByFilePath(filePath);
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
//userFileMapper.deleteById(userFile.getUserFileId());
|
//userFileMapper.deleteById(userFile.getUserFileId());
|
||||||
UserFile userFileTemp = userFileMapper.selectById(userFile.getUserFileId());
|
UserFile userFileTemp = userFileMapper.selectById(userFile.getUserFileId());
|
||||||
FileBean fileBean = fileMapper.selectById(userFileTemp.getFileId());
|
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<>();
|
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||||
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1)
|
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 1)
|
||||||
@ -188,14 +161,40 @@ public class UserFileService extends ServiceImpl<UserFileMapper, UserFile> impl
|
|||||||
.eq(FileBean::getFileId, fileBean.getFileId());
|
.eq(FileBean::getFileId, fileBean.getFileId());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (storageBean != null) {
|
|
||||||
long updateFileSize = storageBean.getStorageSize() - deleteSize;
|
|
||||||
if (updateFileSize < 0) {
|
|
||||||
updateFileSize = 0;
|
|
||||||
}
|
|
||||||
storageBean.setStorageSize(updateFileSize);
|
|
||||||
filetransferService.updateStorageBean(storageBean);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateFileDeleteStateByFilePath(String filePath) {
|
||||||
|
new Thread(()->{
|
||||||
|
List<UserFile> fileList = selectFileTreeListLikeFilePath(filePath);
|
||||||
|
for (int i = 0; i < fileList.size(); i++){
|
||||||
|
UserFile userFileTemp = fileList.get(i);
|
||||||
|
executor.execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (userFileTemp.getIsDir() != 1){
|
||||||
|
FileBean fileBean = fileMapper.selectById(userFileTemp.getFileId());
|
||||||
|
if (fileBean.getPointCount() != null) {
|
||||||
|
|
||||||
|
LambdaUpdateWrapper<FileBean> fileBeanLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||||
|
fileBeanLambdaUpdateWrapper.set(FileBean::getPointCount, fileBean.getPointCount() -1)
|
||||||
|
.eq(FileBean::getFileId, fileBean.getFileId());
|
||||||
|
fileMapper.update(null, fileBeanLambdaUpdateWrapper);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//标记删除标志
|
||||||
|
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
|
||||||
|
userFileLambdaUpdateWrapper1.set(UserFile::getDeleteFlag, 1)
|
||||||
|
.set(UserFile::getDeleteTime, DateUtil.getCurrentTime())
|
||||||
|
.eq(UserFile::getUserFileId, userFileTemp.getUserFileId());
|
||||||
|
userFileMapper.update(null, userFileLambdaUpdateWrapper1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,22 +11,22 @@
|
|||||||
WHERE filepath LIKE N'${oldFilePath}%' and userId = #{userId};
|
WHERE filepath LIKE N'${oldFilePath}%' and userId = #{userId};
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<select id="userFileList" parameterType="com.qiwenshare.file.domain.UserFile" resultType="java.util.Map">
|
<select id="userFileList" resultType="java.util.Map">
|
||||||
select * from userfile
|
select * from userfile a
|
||||||
left join file on file.fileId = userfile.fileId
|
left join file on file.fileId = a.fileId
|
||||||
<where>
|
<where>
|
||||||
<if test="userId != null">
|
<if test="userFile.userId != null">
|
||||||
and userId = #{userId}
|
and a.userId = #{userFile.userId}
|
||||||
</if>
|
</if>
|
||||||
<if test="filePath != null">
|
<if test="userFile.filePath != null">
|
||||||
and filePath = #{filePath}
|
and a.filePath = #{userFile.filePath}
|
||||||
</if>
|
</if>
|
||||||
<if test="extendName != null">
|
<if test="userFile.extendName != null">
|
||||||
and extendName = #{extendName}
|
and a.extendName = #{userFile.extendName}
|
||||||
</if>
|
</if>
|
||||||
and deleteFlag = 0
|
and a.deleteFlag = 0
|
||||||
</where>
|
</where>
|
||||||
limit #{beginCount} #{pageCount}
|
limit #{beginCount}, #{pageCount}
|
||||||
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
@ -72,7 +72,7 @@
|
|||||||
<select id="selectStorageSizeByUserId" resultType="java.lang.Long" parameterType="java.lang.Long">
|
<select id="selectStorageSizeByUserId" resultType="java.lang.Long" parameterType="java.lang.Long">
|
||||||
SELECT SUM(fileSize) FROM userfile
|
SELECT SUM(fileSize) FROM userfile
|
||||||
LEFT JOIN file ON file.fileId = userfile.fileId
|
LEFT JOIN file ON file.fileId = userfile.fileId
|
||||||
WHERE userId = 2
|
WHERE userfile.userId = 2
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
Loading…
Reference in New Issue
Block a user