From f54b12fbbe60d1d974daf9975fc38ee4430261de Mon Sep 17 00:00:00 2001 From: jmal Date: Sat, 18 May 2024 18:51:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E5=8C=85=E6=9C=89=E5=87=A0=E7=8E=87=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=20https://github.com/jameb?= =?UTF-8?q?al/jmal-cloud-view/issues/102?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CommonFileService.java | 54 ++++++++++--------- .../com/jmal/clouddisk/util/CaffeineUtil.java | 16 +++--- 2 files changed, 36 insertions(+), 34 deletions(-) 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) {