在线预览相关图片由webp格式换成avif,优化部分代码

This commit is contained in:
chuzhongzai 2024-09-16 16:40:00 +08:00
parent 25cd6d73b6
commit 325b9b321f
4 changed files with 14 additions and 26 deletions

11
pom.xml
View File

@ -68,19 +68,12 @@
<version>4.5.14</version> <version>4.5.14</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId> <artifactId>httpmime</artifactId>
<version>4.5.14</version> <version>4.5.14</version>
</dependency> </dependency>
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
@ -106,7 +99,7 @@
<dependency> <dependency>
<groupId>com.github.pengrad</groupId> <groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId> <artifactId>java-telegram-bot-api</artifactId>
<version>6.9.1</version> <version>7.9.1</version>
</dependency> </dependency>
<dependency> <dependency>
@ -125,7 +118,7 @@
<buildArgs> <buildArgs>
<arg>--gc=G1</arg> <arg>--gc=G1</arg>
<arg>-H:+ReportExceptionStackTraces</arg> <arg>-H:+ReportExceptionStackTraces</arg>
<arg>--initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService</arg> <arg>--initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService,org.sqlite.util.ProcessRunner</arg>
</buildArgs> </buildArgs>
<metadataRepository> <metadataRepository>
<enabled>true</enabled> <enabled>true</enabled>

View File

@ -346,7 +346,7 @@ public class GalleryManageService {
return response.toJSONString(); return response.toJSONString();
} }
String[] suffixes = {".webp", ".gif"}; String[] suffixes = {".avif", ".gif"};
public Callable<?> 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);

View File

@ -103,7 +103,7 @@ public class QueryService {
String fileName = path.substring(path.lastIndexOf("/") + 1); String fileName = path.substring(path.lastIndexOf("/") + 1);
String suffix = fileName.substring(fileName.lastIndexOf(".")); String suffix = fileName.substring(fileName.lastIndexOf("."));
fileName = fileName.substring(0, 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()){ if(image.isFile()){
FileDownload.export(request, response, image.getAbsolutePath()); FileDownload.export(request, response, image.getAbsolutePath());
@ -111,12 +111,13 @@ public class QueryService {
} }
path = "https://ehgt.org/" + path; 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)); new URI(path).toURL().openConnection().getInputStream().transferTo(new FileOutputStream(CachePath + fileName + suffix));
GalleryUtil.convertImg(CachePath + fileName + suffix, suffix); GalleryUtil.convertImg(CachePath + fileName + suffix, suffix);
outputStream.write(new FileInputStream(image.getAbsolutePath()).readAllBytes()); outputStream.write(inputStream.readAllBytes());
}catch (IOException | URISyntaxException e){ }catch (IOException | URISyntaxException e){
e.printStackTrace(); System.out.println(e.getMessage());
} }
} }
} }

View File

@ -13,9 +13,6 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; 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.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -228,16 +225,13 @@ public class GalleryUtil {
} }
public static String convertImg(String imagePath, String suffix){ public static String convertImg(String imagePath, String suffix){
ConvertCmd convertCmd = new ConvertCmd(true); Runtime rt = Runtime.getRuntime();
IMOperation operation = new IMOperation();
operation.addImage(imagePath);
operation.format("webp");
operation.addImage(imagePath.replace(suffix, ".webp"));
try { try {
convertCmd.run(operation); Process exec = rt.exec(new String[]{"convert", imagePath, imagePath.replace(suffix, ".avif")});
new File(imagePath).delete(); exec.waitFor();
return imagePath.replace(suffix, ".webp"); boolean ignored = new File(imagePath).delete();
} catch (IOException | IM4JavaException | InterruptedException e) { return imagePath.replace(suffix, ".avif");
} catch (IOException| InterruptedException e) {
System.out.println("文件" + imagePath + "转换失败"); System.out.println("文件" + imagePath + "转换失败");
return null; return null;
} }