Skip to content

Commit

Permalink
Merge pull request #200 from jamebal/develop
Browse files Browse the repository at this point in the history
perf:优化文件历史版本记录
  • Loading branch information
jamebal authored Dec 7, 2024
2 parents 0d433e1 + a53575c commit afa4c87
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 620 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/jmal/clouddisk/lucene/LuceneService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

import static com.jmal.clouddisk.service.Constants.UPDATE_DATE;

/**
* @author jmal
* <p>
Expand Down Expand Up @@ -536,7 +538,7 @@ private static Sort getSort(SearchDTO searchDTO) {
}
// 创建排序规则
SortField sortField;
if ("updateDate".equals(searchDTO.getSortProp())) {
if (UPDATE_DATE.equals(searchDTO.getSortProp())) {
sortField = new SortField("modified", SortField.Type.LONG, "descending".equalsIgnoreCase(searchDTO.getSortOrder()));
} else if ("size".equals(searchDTO.getSortProp())) {
sortField = new SortField("size", SortField.Type.LONG, "descending".equalsIgnoreCase(searchDTO.getSortOrder()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private void updateVideoInfo(File file, FileDocument fileExists, String contentT
}
}

private static void setDateTime(File file, Update update) {
public static LocalDateTime setDateTime(File file, Update update) {
LocalDateTime updateDateTime;
LocalDateTime uploadDateTime;
try {
Expand All @@ -462,6 +462,7 @@ private static void setDateTime(File file, Update update) {
}
update.set(UPLOAD_DATE, uploadDateTime);
update.set(UPDATE_DATE, updateDateTime);
return updateDateTime;
}

public FileDocument getFileDocument(String userId, String fileName, String relativePath, Query query) {
Expand Down Expand Up @@ -987,11 +988,14 @@ public void modifyFile(String username, File file) {
update.set(Constants.SUFFIX, suffix);
String fileContentType = getContentType(file, contentType);
update.set(Constants.CONTENT_TYPE, fileContentType);
LocalDateTime updateDate = LocalDateTime.now(TimeUntils.ZONE_ID);
update.set("updateDate", updateDate);
LocalDateTime updateTime = setDateTime(file, update);
// 如果size相同,不更新,且更新时间在1秒内,则不更新
if (fileDocument.getSize() == file.length() && TimeUntils.isWithinOneSecond(fileDocument.getUpdateDate(), updateTime)) {
return;
}
UpdateResult updateResult = mongoTemplate.upsert(query, update, COLLECTION_NAME);
fileDocument.setSize(file.length());
fileDocument.setUpdateDate(updateDate);
fileDocument.setUpdateDate(updateTime);
if (contentType.contains(Constants.CONTENT_TYPE_MARK_DOWN) || "md".equals(suffix)) {
// 写入markdown内容
String markDownContent = FileUtil.readString(file, MyFileUtils.getFileCharset(file));
Expand All @@ -1006,6 +1010,7 @@ public void modifyFile(String username, File file) {
// 修改文件之后保存历史版本
fileVersionService.saveFileVersion(username, Paths.get(relativePath, file.getName()).toString(), userId);
}
log.info("修改文件完成: {}", file.getAbsolutePath());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ private boolean renameFileError(String newFileName, String fileId, String filePa
Update update = new Update();
update.set("name", newFileName);
update.set(Constants.SUFFIX, MyFileUtils.extName(newFileName));
update.set("updateDate", LocalDateTime.now(TimeUntils.ZONE_ID));
setDateTime(file, update);
mongoTemplate.upsert(query, update, COLLECTION_NAME);
} else {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.commons.io.IOUtils;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mozilla.universalchardet.UniversalDetector;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -58,6 +57,8 @@
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import static com.jmal.clouddisk.service.Constants.UPDATE_DATE;

/**
* @author jmal
* @Description 文件版本管理
Expand Down Expand Up @@ -157,18 +158,15 @@ private static Metadata setMetadata(long size, String filepath, String filename,
*/
public InputStream readFileVersion(String gridFSId) throws IOException {
GridFSFile gridFSFile = getGridFSFile(gridFSId);
if (gridFSFile != null) {
return getInputStream(gridFSFile);
}
return null;
return getInputStream(gridFSFile);
}

private InputStream getInputStream(GridFSFile gridFSFile) throws IOException {
GridFsResource gridFsResource = gridFsTemplate.getResource(gridFSFile);
return gzipDecompress(gridFsResource.getInputStream(), gridFSFile.getMetadata());
}

@Nullable
@NotNull
private GridFSFile getGridFSFile(String gridFSId) {
Query query = getQueryOfId(gridFSId);
return gridFsTemplate.findOne(query);
Expand Down Expand Up @@ -227,7 +225,7 @@ public ResponseResult<List<GridFSBO>> listFileVersionByPath(String path, Integer
@Override
public FileDocument getFileById(String gridFSId) {
GridFSFile gridFSFile = getGridFSFile(gridFSId);
if (gridFSFile == null || gridFSFile.getMetadata() == null) {
if (gridFSFile.getMetadata() == null) {
return null;
}
String fileId = gridFSFile.getFilename();
Expand Down Expand Up @@ -261,9 +259,6 @@ private Charset getCharset(GridFSFile gridFSFile) {
public StreamingResponseBody getStreamFileById(String gridFSId) {
return outputStream -> {
GridFSFile gridFSFile = getGridFSFile(gridFSId);
if (gridFSFile == null) {
return;
}
Charset charset = getCharset(gridFSFile);
try (InputStream inputStream = getInputStream(gridFSFile);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
Expand Down Expand Up @@ -327,7 +322,7 @@ public void rename(String sourceFileId, String destinationFileId) {
@Override
public Long recovery(String gridFSId) {
GridFSFile gridFSFile = getGridFSFile(gridFSId);
if (gridFSFile == null || gridFSFile.getMetadata() == null) {
if (gridFSFile.getMetadata() == null) {
throw new CommonException(ExceptionType.FILE_NOT_FIND);
}
String fileId = gridFSFile.getFilename();
Expand All @@ -352,7 +347,7 @@ public Long recovery(String gridFSId) {
FileUtil.writeFromStream(inputStream, file);
Query query = new Query().addCriteria(Criteria.where("_id").is(fileId));
Update update = new Update();
update.set("updateDate", time);
update.set(UPDATE_DATE, time);
mongoTemplate.updateFirst(query, update, FileDocument.class);
luceneService.pushCreateIndexQueue(fileId);
} catch (IOException e) {
Expand All @@ -364,7 +359,7 @@ public Long recovery(String gridFSId) {
@Override
public ResponseEntity<InputStreamResource> readHistoryFile(String gridFSId) {
GridFSFile gridFSFile = getGridFSFile(gridFSId);
if (gridFSFile == null || gridFSFile.getMetadata() == null) {
if (gridFSFile.getMetadata() == null) {
return ResponseEntity.notFound().build();
}
try (InputStream inputStream = getInputStream(gridFSFile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ public ResponseResult<Object> deleteDraft(String fileId, String username) {

File file = Paths.get(fileProperties.getRootDir(), username, fileDocument.getPath(), fileDocument.getName()).toFile();
FileUtil.writeString(fileDocument.getContentText(), file, StandardCharsets.UTF_8);

Update update = new Update();
update.unset(Constants.DRAFT);
mongoTemplate.upsert(query, update, CommonFileService.COLLECTION_NAME);
Expand Down Expand Up @@ -639,7 +638,6 @@ public ResponseResult<Object> editTextByPath(UploadApiParamDTO upload) {
String userId = upload.getUserId();
commonFileService.checkPermissionUserId(userId, upload.getOperationPermissionList(), OperationPermission.PUT);
FileUtil.writeString(upload.getContentText(), file, StandardCharsets.UTF_8);
commonFileService.modifyFile(upload.getUsername(), file);
return ResultUtil.success();
}

Expand Down
45 changes: 44 additions & 1 deletion src/main/java/com/jmal/clouddisk/util/MyFileUtils.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.jmal.clouddisk.util;

import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.CharsetDetector;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import com.jmal.clouddisk.service.Constants;
import lombok.extern.slf4j.Slf4j;
import org.mozilla.universalchardet.UniversalDetector;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;

Expand All @@ -28,8 +34,13 @@ private MyFileUtils() {

}

public static void main(String[] args) {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
System.out.println(extName("file"));
File file1 = new File("/Users/jmal/Downloads/归档.zip");
File file2 = new File("/Users/jmal/Downloads/归档.zip");
TimeInterval timer = new TimeInterval();
System.out.println(hashEquals(file1.getAbsolutePath(), file2.getAbsolutePath()));
System.out.println(timer.interval());
}

public static String extName(File file) {
Expand Down Expand Up @@ -126,6 +137,38 @@ public static boolean checkNoCacheFile(File file) {
public static boolean hasContentFile(String type) {
return hasContentTypes.contains(type);
}

public static MessageDigest digest;

static {
try {
digest = MessageDigest.getInstance(DigestAlgorithm.MD5.getValue());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static String calculateHash(String filePath) throws IOException {
try (FileInputStream fis = new FileInputStream(filePath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
digest.update(buffer, 0, bytesRead);
}
}
byte[] hashBytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}

public static boolean hashEquals(String filePath1, String filePath2) throws IOException {
String hash1 = calculateHash(filePath1);
String hash2 = calculateHash(filePath2);
return hash1.equals(hash2);
}
}


Loading

0 comments on commit afa4c87

Please sign in to comment.