Skip to content

Commit

Permalink
perf: 优化删除操作和移动操作
Browse files Browse the repository at this point in the history
  • Loading branch information
jamebal committed Jul 25, 2024
1 parent 5071ca6 commit c5222b6
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 123 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/jmal/clouddisk/config/FileProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public class FileProperties {
* 视频转码后的缓存目录, 位于 ${chunkFileDir}/${username}/${videoTranscodeCache}
*/
private String videoTranscodeCache = "videoTranscodeCache";
/**
* 回收站目录, 位于 ${chunkFileDir}/${username}/${jmalcloudTrashDir}
*/
private String jmalcloudTrashDir = ".jmalcloudTrash";
/**
* lucene索引存储目录
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public ResponseResult<Object> unFavorite(@RequestParam String[] fileIds) {
public ResponseResult<Object> delete(@RequestParam String username, @RequestParam String[] fileIds, @RequestParam String currentDirectory, Boolean sweep) {
if (fileIds != null && fileIds.length > 0) {
List<String> list = Arrays.asList(fileIds);
return fileService.delete(username, currentDirectory, list, userLoginHolder.getUsername(), BooleanUtil.isTrue(sweep), true);
return fileService.delete(username, currentDirectory, list, userLoginHolder.getUsername(), BooleanUtil.isTrue(sweep));
} else {
throw new CommonException(ExceptionType.MISSING_PARAMETERS.getCode(), ExceptionType.MISSING_PARAMETERS.getMsg());
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/jmal/clouddisk/controller/sse/SseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
Expand Down Expand Up @@ -94,5 +95,19 @@ private void removeUuid(String username, String uuid) {
}
}
}

/**
* 每3秒发送一次心跳消息
*/
@Scheduled(fixedRate = 3000)
public void heartbeat() {
emitters.forEach((uuid, emitter) -> {
try {
emitter.send("h");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}

24 changes: 16 additions & 8 deletions src/main/java/com/jmal/clouddisk/listener/FileMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ private void fastInterval() throws Exception {
}

/**
* 定时清理临时目录
* 每天凌晨2点执行
* 清理临时目录中7天前的文件
* <p>定时清理临时目录</p>
* 每天凌晨2点执行<br>
* 1.清理临时目录中3天前的文件<br>
* 2.清理视频转码缓存目录中的无效文件<br>
* 3.清理回收站中30天前的文件<br>
*/
@Scheduled(cron = "0 0 2 * * ?")
private void cleanTempDir() {
Expand All @@ -218,13 +220,10 @@ private void cleanTempDir() {
for (File username : FileUtil.ls(tempPath.toString())) {
if (username.isDirectory()) {
for (File file : FileUtil.ls(username.getAbsolutePath())) {
// 是否为七天前的文件
boolean sevenDayAgo = file.lastModified() < (System.currentTimeMillis() - DateUnit.DAY.getMillis() * 7);
// 是否为三天前的文件
boolean sevenDayAgo = file.lastModified() < (System.currentTimeMillis() - DateUnit.DAY.getMillis() * 3);
// 是否为视频转码缓存目录
boolean videoCache = fileProperties.getVideoTranscodeCache().equals(file.getName()) || fileProperties.getLuceneIndexDir().equals(file.getParentFile().getName());
if (sevenDayAgo && !videoCache) {
FileUtil.del(file);
}
if (videoCache) {
if (file.listFiles() != null) {
for (File f : Objects.requireNonNull(file.listFiles())) {
Expand All @@ -235,6 +234,15 @@ private void cleanTempDir() {
}
}
}
return;
}
// 回收站
boolean trash = fileProperties.getJmalcloudTrashDir().equals(file.getName());
if (trash) {
return;
}
if (sevenDayAgo) {
FileUtil.del(file);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/jmal/clouddisk/model/FileDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public class FileDocument extends FileBase {
*/
private Integer delete;

private Boolean move;

/**
* 操作权限
*/
Expand All @@ -218,7 +220,7 @@ public int hashCode() {
return hash;
}

public Trash toTrash(boolean hidden) {
public Trash toTrash(boolean hidden, boolean move) {
Trash trash = new Trash();
trash.setId(this.getId());
trash.setName(this.getName());
Expand All @@ -240,6 +242,7 @@ public Trash toTrash(boolean hidden) {
trash.setM3u8(this.getM3u8());
trash.setVtt(this.getVtt());
trash.setHidden(hidden);
trash.setMove(move);
return trash;
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/jmal/clouddisk/model/Trash.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public class Trash extends FileBase {
/**
* 是否隐藏显示
*/
private boolean hidden;
private Boolean hidden;
/**
* 是否移动到回收站, 从原位置移动到jmalcloudTrashDir
*/
private Boolean move;

@Override
public boolean equals(Object obj) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/jmal/clouddisk/service/IFileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public interface IFileService {
* @param sweep 是否彻底删除
* @return ResponseResult<Object>
*/
ResponseResult<Object> delete(String username, String currentDirectory, List<String> fileIds, String operator, boolean sweep, boolean notify);
ResponseResult<Object> delete(String username, String currentDirectory, List<String> fileIds, String operator, boolean sweep);

/**
* 显示缩略图
Expand Down Expand Up @@ -396,9 +396,9 @@ public interface IFileService {
* 删除文件所有依赖
* @param username 用户名
* @param fileIds 文件id列表
* @param toTrash 是否移动到回收站, 否则就是硬删除
* @param sweep 是否硬删除, 否则就是移动到回收站
*/
void deleteDependencies(String username, List<String> fileIds, boolean toTrash);
void deleteDependencies(String username, List<String> fileIds, boolean sweep);

/**
* 返回原处
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ public String createFile(String username, File file, String userId, Boolean isPu
pushMessage(username, update.getUpdateObject(), Constants.CREATE_FILE);
// 添加文件索引
luceneService.pushCreateIndexQueue(fileId);
// 判断回收站是否存在该文件, 如果存在则删除
checkTrash(file, username);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
Expand All @@ -381,16 +379,6 @@ public String createFile(String username, File file, String userId, Boolean isPu
return fileId;
}

private void checkTrash(File file, String username) {
String userId = userService.getUserIdByUserName(username);
String relativePath = getRelativePath(username, String.valueOf(file.getAbsoluteFile()), file.getName());
Query query = new Query();
query.addCriteria(Criteria.where(IUserService.USER_ID).is(userId));
query.addCriteria(Criteria.where("path").is(relativePath));
query.addCriteria(Criteria.where("name").is(file.getName()));
mongoTemplate.remove(query, TRASH_COLLECTION_NAME);
}

private void updateOtherInfo(FileDocument fileExists, String contentType, Update update) {
if (!contentType.equals(fileExists.getContentType())) {
update.set(Constants.CONTENT_TYPE, contentType);
Expand Down
Loading

0 comments on commit c5222b6

Please sign in to comment.