diff --git a/src/main/java/com/jmal/clouddisk/service/impl/CommonFileService.java b/src/main/java/com/jmal/clouddisk/service/impl/CommonFileService.java index 005dab5b..06f0c8ee 100644 --- a/src/main/java/com/jmal/clouddisk/service/impl/CommonFileService.java +++ b/src/main/java/com/jmal/clouddisk/service/impl/CommonFileService.java @@ -109,10 +109,10 @@ public class CommonFileService { @Autowired public LuceneService luceneService; - /*** + /** * 上传文件夹的写入锁缓存 */ - private final Cache uploadFolderLockCache = CaffeineUtil.getUploadFolderLockCache(); + private final Cache uploadFileLockCache = CaffeineUtil.getUploadFileLockCache(); protected static final Set FILE_PATH_LOCK = new CopyOnWriteArraySet<>(); @@ -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(); @@ -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()) { @@ -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); } } diff --git a/src/main/java/com/jmal/clouddisk/util/CaffeineUtil.java b/src/main/java/com/jmal/clouddisk/util/CaffeineUtil.java index a1ff0d82..266e2da7 100644 --- a/src/main/java/com/jmal/clouddisk/util/CaffeineUtil.java +++ b/src/main/java/com/jmal/clouddisk/util/CaffeineUtil.java @@ -70,10 +70,10 @@ public class CaffeineUtil { */ private static Cache chunkWriteLockCache; - /*** - * 上传文件夹锁 + /** + * 上传文件锁 */ - private static Cache uploadFolderLockCache; + private static Cache uploadFileLockCache; /*** * 用户身份权限缓存 @@ -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(); } } @@ -180,11 +180,11 @@ public static Cache getChunkWriteLockCache(){ return chunkWriteLockCache; } - public static Cache getUploadFolderLockCache(){ - if(uploadFolderLockCache == null){ + public static Cache getUploadFileLockCache(){ + if(uploadFileLockCache == null){ initMyCache(); } - return uploadFolderLockCache; + return uploadFileLockCache; } public static void setSpaceFull(String userId) {