From 7057b28ff00400aa5b9d25bc55fedc9966bd2621 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Wed, 3 Jan 2024 14:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=B7=AF=E5=BE=84;=E5=B0=86=E5=B9=B6=E5=8F=91=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E7=94=B1synchronize=E6=8D=A2=E6=88=90callable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/GalleryManageController.java | 6 +++-- .../Service/GalleryManageService.java | 22 ++++++++++--------- src/main/resources/application.yaml | 4 ++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java b/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java index 443cdd6..71fe5f9 100644 --- a/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java +++ b/src/main/java/com/lion/lionwebsite/Controller/GalleryManageController.java @@ -9,6 +9,8 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; +import java.util.concurrent.Callable; + @RestController @RequestMapping("/GalleryManage") @Slf4j @@ -77,8 +79,8 @@ public class GalleryManageController { } @GetMapping("/onlineImage/{page}") - public void getCacheImage(String gid, @PathVariable("page") int page, HttpServletRequest request, HttpServletResponse response){ - galleryManageService.getCachedImage(gid, page, request, response); + public Callable getCacheImage(String gid, @PathVariable("page") int page, HttpServletRequest request, HttpServletResponse response){ + return galleryManageService.getCachedImage(gid, page, request, response); } @PostMapping("/reset") diff --git a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java index e1795c3..a72aff9 100644 --- a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java +++ b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java @@ -19,6 +19,7 @@ import org.springframework.stereotype.Service; import java.io.*; import java.net.URI; import java.util.*; +import java.util.concurrent.Callable; import static com.lion.lionwebsite.Util.CustomUtil.objectMapper; import static com.lion.lionwebsite.Util.GalleryUtil.*; @@ -346,7 +347,7 @@ public class GalleryManageService { } String[] suffixes = {".webp", ".gif"}; - public void getCachedImage(String gid, Integer page, HttpServletRequest request, HttpServletResponse response) { + public Callable getCachedImage(String gid, Integer page, HttpServletRequest request, HttpServletResponse response) { //检查文件夹是否存在 File folder = new File(cachePath + gid); if(!folder.isDirectory()) @@ -356,7 +357,7 @@ public class GalleryManageService { for (String suffix : suffixes) { if(new File(cachePath + gid + "/" + page + suffix).exists()){ FileDownload.export(request, response, cachePath + gid + "/" + page + suffix); - return; + return null; } } @@ -366,15 +367,15 @@ public class GalleryManageService { try { log.error("未缓存gid:" + gid); response.sendError(404); - return; + return null; }catch (IOException ignored){ - return; + return null; } - synchronized (this){ + return () -> { if(response.isCommitted()) { log.info("连接已关闭: gid=" + gid + " page=" + page); - return; + return null; } String imageUrl = null; @@ -382,7 +383,7 @@ public class GalleryManageService { ImageKeyCache imageKeyCache = imageCacheMapper.selectImageKeyCacheByGidAndPage(gid, page); if (imageKeyCache == null) { CustomUtil.fourZeroFour(response); - return; + return null; } //获取图片地址 @@ -396,7 +397,7 @@ public class GalleryManageService { if (imageUrl == null) { CustomUtil.fourZeroFour(response); log.error("获取图片url失败:gid=" + gid + " page=" + page + " imageKey=" + imageKeyCache.getImgkey()); - return; + return null; } //下载图片,转格式并返回 @@ -408,12 +409,13 @@ public class GalleryManageService { log.error("下载图片失败:url" + imageUrl); e.printStackTrace(); CustomUtil.fourZeroFour(response); - return; + return null; } if (!suffix.equals(".gif")) imagePath = GalleryUtil.convertImg(imagePath, suffix); FileDownload.export(request, response, imagePath); - } + return null; + }; } public String resetUndone(){ diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 62ddefb..02a9d44 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -8,10 +8,10 @@ server: spring: datasource-main: driver-class-name: org.sqlite.JDBC - jdbc-url: jdbc:sqlite:/root/LionWebsite/LionWebsite.db + jdbc-url: jdbc:sqlite:LionWebsite.db datasource-cache: driver-class-name: org.sqlite.JDBC - jdbc-url: jdbc:sqlite:/root/LionWebsite/cache.db + jdbc-url: jdbc:sqlite:cache.db mvc: view: prefix: /resources/templates/