Skip to content

Commit

Permalink
Merge pull request #52 from jamebal/develop
Browse files Browse the repository at this point in the history
fix: 修复上传压缩包有几率报错的问题
  • Loading branch information
jamebal authored May 18, 2024
2 parents 7645639 + f54b12f commit 44574a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public class CommonFileService {
@Autowired
public LuceneService luceneService;

/***
/**
* 上传文件夹的写入锁缓存
*/
private final Cache<String, Lock> uploadFolderLockCache = CaffeineUtil.getUploadFolderLockCache();
private final Cache<String, Lock> uploadFileLockCache = CaffeineUtil.getUploadFileLockCache();

protected static final Set<String> FILE_PATH_LOCK = new CopyOnWriteArraySet<>();

Expand Down Expand Up @@ -304,12 +304,9 @@ public String createFile(String username, File file, String userId, Boolean isPu
}

String fileAbsolutePath = file.getAbsolutePath();
Lock lock = null;
if (file.isDirectory()) {
lock = uploadFolderLockCache.get(fileAbsolutePath, key -> new ReentrantLock());
if (lock != null) {
lock.lock();
}
Lock lock = uploadFileLockCache.get(fileAbsolutePath, key -> new ReentrantLock());
if (lock != null) {
lock.lock();
}
UpdateResult updateResult;
ObjectId objectId = new ObjectId();
Expand Down Expand Up @@ -356,6 +353,7 @@ public String createFile(String username, File file, String userId, Boolean isPu
} finally {
if (lock != null) {
lock.unlock();
uploadFileLockCache.invalidate(fileAbsolutePath);
}
}
if (null != updateResult.getUpsertedId()) {
Expand Down Expand Up @@ -446,24 +444,28 @@ public void imageFileToWebp(File outputFile, BufferedImage image) throws IOExcep
}

private void setFileConfig(String fileId, String username, File file, String fileName, String suffix, String contentType, String relativePath, Update update) {
long size = file.length();
update.set("size", size);
update.set("md5", size + relativePath + fileName);
update.set(Constants.CONTENT_TYPE, getContentType(file, contentType));
update.set(Constants.SUFFIX, suffix);
if (contentType.contains(Constants.AUDIO)) {
setMusic(file, update);
}
if (contentType.contains(Constants.VIDEO)) {
setMediaCover(fileId, username, fileName, relativePath, update);
}
if (contentType.startsWith(Constants.CONTENT_TYPE_IMAGE) && (!"ico".equals(suffix) && !"svg".equals(suffix))) {
generateThumbnail(file, update);
}
if (contentType.contains(Constants.CONTENT_TYPE_MARK_DOWN) || "md".equals(suffix)) {
// 写入markdown内容
String markDownContent = FileUtil.readString(file, MyFileUtils.getFileCharset(file));
update.set("contentText", markDownContent);
try {
long size = file.length();
update.set("size", size);
update.set("md5", size + relativePath + fileName);
update.set(Constants.CONTENT_TYPE, getContentType(file, contentType));
update.set(Constants.SUFFIX, suffix);
if (contentType.contains(Constants.AUDIO)) {
setMusic(file, update);
}
if (contentType.contains(Constants.VIDEO)) {
setMediaCover(fileId, username, fileName, relativePath, update);
}
if (contentType.startsWith(Constants.CONTENT_TYPE_IMAGE) && (!"ico".equals(suffix) && !"svg".equals(suffix))) {
generateThumbnail(file, update);
}
if (contentType.contains(Constants.CONTENT_TYPE_MARK_DOWN) || "md".equals(suffix)) {
// 写入markdown内容
String markDownContent = FileUtil.readString(file, MyFileUtils.getFileCharset(file));
update.set("contentText", markDownContent);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/jmal/clouddisk/util/CaffeineUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public class CaffeineUtil {
*/
private static Cache<String, Lock> chunkWriteLockCache;

/***
* 上传文件夹锁
/**
* 上传文件锁
*/
private static Cache<String, Lock> uploadFolderLockCache;
private static Cache<String, Lock> uploadFileLockCache;

/***
* 用户身份权限缓存
Expand Down Expand Up @@ -147,8 +147,8 @@ public static void initMyCache(){
if(chunkWriteLockCache == null){
chunkWriteLockCache = Caffeine.newBuilder().build();
}
if(uploadFolderLockCache == null) {
uploadFolderLockCache = Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).build();
if(uploadFileLockCache == null) {
uploadFileLockCache = Caffeine.newBuilder().build();
}
}

Expand Down Expand Up @@ -180,11 +180,11 @@ public static Cache<String, Lock> getChunkWriteLockCache(){
return chunkWriteLockCache;
}

public static Cache<String, Lock> getUploadFolderLockCache(){
if(uploadFolderLockCache == null){
public static Cache<String, Lock> getUploadFileLockCache(){
if(uploadFileLockCache == null){
initMyCache();
}
return uploadFolderLockCache;
return uploadFileLockCache;
}

public static void setSpaceFull(String userId) {
Expand Down

0 comments on commit 44574a1

Please sign in to comment.