在线预览相关图片由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>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
@ -106,7 +99,7 @@
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>6.9.1</version>
<version>7.9.1</version>
</dependency>
<dependency>
@ -125,7 +118,7 @@
<buildArgs>
<arg>--gc=G1</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>
<metadataRepository>
<enabled>true</enabled>

View File

@ -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);

View File

@ -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());
}
}
}

View File

@ -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;
}