修改数据库路径;将并发控制由synchronize换成callable

This commit is contained in:
chuzhongzai 2024-01-03 14:36:23 +08:00
parent fd016c7809
commit 7057b28ff0
3 changed files with 18 additions and 14 deletions

View File

@ -9,6 +9,8 @@ import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.concurrent.Callable;
@RestController @RestController
@RequestMapping("/GalleryManage") @RequestMapping("/GalleryManage")
@Slf4j @Slf4j
@ -77,8 +79,8 @@ public class GalleryManageController {
} }
@GetMapping("/onlineImage/{page}") @GetMapping("/onlineImage/{page}")
public void getCacheImage(String gid, @PathVariable("page") int page, HttpServletRequest request, HttpServletResponse response){ public Callable<?> getCacheImage(String gid, @PathVariable("page") int page, HttpServletRequest request, HttpServletResponse response){
galleryManageService.getCachedImage(gid, page, request, response); return galleryManageService.getCachedImage(gid, page, request, response);
} }
@PostMapping("/reset") @PostMapping("/reset")

View File

@ -19,6 +19,7 @@ import org.springframework.stereotype.Service;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
import java.util.*; import java.util.*;
import java.util.concurrent.Callable;
import static com.lion.lionwebsite.Util.CustomUtil.objectMapper; import static com.lion.lionwebsite.Util.CustomUtil.objectMapper;
import static com.lion.lionwebsite.Util.GalleryUtil.*; import static com.lion.lionwebsite.Util.GalleryUtil.*;
@ -346,7 +347,7 @@ public class GalleryManageService {
} }
String[] suffixes = {".webp", ".gif"}; 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); File folder = new File(cachePath + gid);
if(!folder.isDirectory()) if(!folder.isDirectory())
@ -356,7 +357,7 @@ public class GalleryManageService {
for (String suffix : suffixes) { for (String suffix : suffixes) {
if(new File(cachePath + gid + "/" + page + suffix).exists()){ if(new File(cachePath + gid + "/" + page + suffix).exists()){
FileDownload.export(request, response, cachePath + gid + "/" + page + suffix); FileDownload.export(request, response, cachePath + gid + "/" + page + suffix);
return; return null;
} }
} }
@ -366,15 +367,15 @@ public class GalleryManageService {
try { try {
log.error("未缓存gid:" + gid); log.error("未缓存gid:" + gid);
response.sendError(404); response.sendError(404);
return; return null;
}catch (IOException ignored){ }catch (IOException ignored){
return; return null;
} }
synchronized (this){ return () -> {
if(response.isCommitted()) { if(response.isCommitted()) {
log.info("连接已关闭: gid=" + gid + " page=" + page); log.info("连接已关闭: gid=" + gid + " page=" + page);
return; return null;
} }
String imageUrl = null; String imageUrl = null;
@ -382,7 +383,7 @@ public class GalleryManageService {
ImageKeyCache imageKeyCache = imageCacheMapper.selectImageKeyCacheByGidAndPage(gid, page); ImageKeyCache imageKeyCache = imageCacheMapper.selectImageKeyCacheByGidAndPage(gid, page);
if (imageKeyCache == null) { if (imageKeyCache == null) {
CustomUtil.fourZeroFour(response); CustomUtil.fourZeroFour(response);
return; return null;
} }
//获取图片地址 //获取图片地址
@ -396,7 +397,7 @@ public class GalleryManageService {
if (imageUrl == null) { if (imageUrl == null) {
CustomUtil.fourZeroFour(response); CustomUtil.fourZeroFour(response);
log.error("获取图片url失败:gid=" + gid + " page=" + page + " imageKey=" + imageKeyCache.getImgkey()); log.error("获取图片url失败:gid=" + gid + " page=" + page + " imageKey=" + imageKeyCache.getImgkey());
return; return null;
} }
//下载图片转格式并返回 //下载图片转格式并返回
@ -408,12 +409,13 @@ public class GalleryManageService {
log.error("下载图片失败:url" + imageUrl); log.error("下载图片失败:url" + imageUrl);
e.printStackTrace(); e.printStackTrace();
CustomUtil.fourZeroFour(response); CustomUtil.fourZeroFour(response);
return; return null;
} }
if (!suffix.equals(".gif")) if (!suffix.equals(".gif"))
imagePath = GalleryUtil.convertImg(imagePath, suffix); imagePath = GalleryUtil.convertImg(imagePath, suffix);
FileDownload.export(request, response, imagePath); FileDownload.export(request, response, imagePath);
} return null;
};
} }
public String resetUndone(){ public String resetUndone(){

View File

@ -8,10 +8,10 @@ server:
spring: spring:
datasource-main: datasource-main:
driver-class-name: org.sqlite.JDBC driver-class-name: org.sqlite.JDBC
jdbc-url: jdbc:sqlite:/root/LionWebsite/LionWebsite.db jdbc-url: jdbc:sqlite:LionWebsite.db
datasource-cache: datasource-cache:
driver-class-name: org.sqlite.JDBC driver-class-name: org.sqlite.JDBC
jdbc-url: jdbc:sqlite:/root/LionWebsite/cache.db jdbc-url: jdbc:sqlite:cache.db
mvc: mvc:
view: view:
prefix: /resources/templates/ prefix: /resources/templates/