From 325b9b321fa4dbdb081566d086c7ca7c20947c75 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Mon, 16 Sep 2024 16:40:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E9=A2=84=E8=A7=88=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=9B=BE=E7=89=87=E7=94=B1webp=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=8D=A2=E6=88=90avif,=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 11 ++--------- .../Service/GalleryManageService.java | 2 +- .../lion/lionwebsite/Service/QueryService.java | 9 +++++---- .../com/lion/lionwebsite/Util/GalleryUtil.java | 18 ++++++------------ 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index f0cfafd..ce70337 100644 --- a/pom.xml +++ b/pom.xml @@ -68,19 +68,12 @@ 4.5.14 - org.apache.httpcomponents httpmime 4.5.14 - - org.im4java - im4java - 1.4.0 - - org.apache.commons commons-lang3 @@ -106,7 +99,7 @@ com.github.pengrad java-telegram-bot-api - 6.9.1 + 7.9.1 @@ -125,7 +118,7 @@ --gc=G1 -H:+ReportExceptionStackTraces - --initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService + --initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService,org.sqlite.util.ProcessRunner true diff --git a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java index 6477e34..05ccda4 100644 --- a/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java +++ b/src/main/java/com/lion/lionwebsite/Service/GalleryManageService.java @@ -346,7 +346,7 @@ public class GalleryManageService { return response.toJSONString(); } - String[] suffixes = {".webp", ".gif"}; + String[] suffixes = {".avif", ".gif"}; public Callable getCachedImage(String gid, Integer page, HttpServletRequest request, HttpServletResponse response) { //检查文件夹是否存在 File folder = new File(cachePath + gid); diff --git a/src/main/java/com/lion/lionwebsite/Service/QueryService.java b/src/main/java/com/lion/lionwebsite/Service/QueryService.java index f6b1c81..694b22f 100644 --- a/src/main/java/com/lion/lionwebsite/Service/QueryService.java +++ b/src/main/java/com/lion/lionwebsite/Service/QueryService.java @@ -103,7 +103,7 @@ public class QueryService { String fileName = path.substring(path.lastIndexOf("/") + 1); String suffix = fileName.substring(fileName.lastIndexOf(".")); fileName = fileName.substring(0, fileName.lastIndexOf(".")); - File image = new File(CachePath, fileName + ".webp"); + File image = new File(CachePath, fileName + ".avif"); if(image.isFile()){ FileDownload.export(request, response, image.getAbsolutePath()); @@ -111,12 +111,13 @@ public class QueryService { } path = "https://ehgt.org/" + path; - try(ServletOutputStream outputStream = response.getOutputStream()){ + try(ServletOutputStream outputStream = response.getOutputStream(); + FileInputStream inputStream = new FileInputStream(image.getAbsoluteFile())){ new URI(path).toURL().openConnection().getInputStream().transferTo(new FileOutputStream(CachePath + fileName + suffix)); GalleryUtil.convertImg(CachePath + fileName + suffix, suffix); - outputStream.write(new FileInputStream(image.getAbsolutePath()).readAllBytes()); + outputStream.write(inputStream.readAllBytes()); }catch (IOException | URISyntaxException e){ - e.printStackTrace(); + System.out.println(e.getMessage()); } } } diff --git a/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java b/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java index 5ddfea3..d3254ca 100644 --- a/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java +++ b/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java @@ -13,9 +13,6 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; -import org.im4java.core.ConvertCmd; -import org.im4java.core.IM4JavaException; -import org.im4java.core.IMOperation; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -228,16 +225,13 @@ public class GalleryUtil { } public static String convertImg(String imagePath, String suffix){ - ConvertCmd convertCmd = new ConvertCmd(true); - IMOperation operation = new IMOperation(); - operation.addImage(imagePath); - operation.format("webp"); - operation.addImage(imagePath.replace(suffix, ".webp")); + Runtime rt = Runtime.getRuntime(); try { - convertCmd.run(operation); - new File(imagePath).delete(); - return imagePath.replace(suffix, ".webp"); - } catch (IOException | IM4JavaException | InterruptedException e) { + Process exec = rt.exec(new String[]{"convert", imagePath, imagePath.replace(suffix, ".avif")}); + exec.waitFor(); + boolean ignored = new File(imagePath).delete(); + return imagePath.replace(suffix, ".avif"); + } catch (IOException| InterruptedException e) { System.out.println("文件" + imagePath + "转换失败"); return null; }