抽取图片格式转换方法;封面图加上缓存;更换缓存存放位置

This commit is contained in:
chuzhongzai 2023-12-27 23:16:58 +08:00
parent 07b5b1c376
commit 54b1dd47a6
6 changed files with 60 additions and 169 deletions

View File

@ -3,6 +3,7 @@ package com.lion.lionwebsite.Controller;
import com.lion.lionwebsite.Domain.User; import com.lion.lionwebsite.Domain.User;
import com.lion.lionwebsite.Service.PublicServiceImpl; import com.lion.lionwebsite.Service.PublicServiceImpl;
import com.lion.lionwebsite.Service.QueryService;
import com.lion.lionwebsite.Service.RemoteService; import com.lion.lionwebsite.Service.RemoteService;
import com.lion.lionwebsite.Service.SubService; import com.lion.lionwebsite.Service.SubService;
import com.lion.lionwebsite.Util.Response; import com.lion.lionwebsite.Util.Response;
@ -28,10 +29,13 @@ public class PublicController {
SubService subService; SubService subService;
public PublicController(PublicServiceImpl publicService, RemoteService remoteService, SubService subService) { QueryService queryService;
public PublicController(PublicServiceImpl publicService, RemoteService remoteService, SubService subService, QueryService queryService) {
this.publicService = publicService; this.publicService = publicService;
this.remoteService = remoteService; this.remoteService = remoteService;
this.subService = subService; this.subService = subService;
this.queryService = queryService;
} }
@GetMapping("/ip") @GetMapping("/ip")
@ -70,7 +74,7 @@ public class PublicController {
black_share_codes.add(ShareCode); black_share_codes.add(ShareCode);
if(black_share_codes.size() > 100) if(black_share_codes.size() > 100)
black_share_codes.remove(0); black_share_codes.removeFirst();
} }
@PostMapping("/validate") @PostMapping("/validate")
@ -90,8 +94,7 @@ public class PublicController {
} }
@GetMapping("/GalleryManage/ehThumbnail") @GetMapping("/GalleryManage/ehThumbnail")
public void getEhThumbnail(String path, HttpServletResponse response){ public void getEhThumbnail(String path, HttpServletRequest request, HttpServletResponse response){
publicService.getEhThumbnail(path, response); queryService.getEhThumbnail(path, request, response);
} }
} }

View File

@ -1,8 +1,6 @@
package com.lion.lionwebsite.Controller; package com.lion.lionwebsite.Controller;
import com.lion.lionwebsite.Service.QueryService; import com.lion.lionwebsite.Service.QueryService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -22,10 +20,4 @@ public class QueryController {
public String query(String keyword, String prev, String next){ public String query(String keyword, String prev, String next){
return queryService.query(keyword, prev, next); return queryService.query(keyword, prev, next);
} }
@GetMapping("/image")
public void image(HttpServletResponse response, String path){
queryService.image(response, path);
}
} }

View File

@ -14,9 +14,6 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.*; import java.io.*;
@ -30,8 +27,7 @@ import static com.lion.lionwebsite.Util.GalleryUtil.*;
@Data @Data
@Slf4j @Slf4j
public class GalleryManageService { public class GalleryManageService {
String TargetPath = "/root/gallery/"; String cachePath = "/storage/galleryCache/onlineImages/";
String cachePath = "/root/galleryCache/";
GalleryMapper galleryMapper; GalleryMapper galleryMapper;
@ -428,20 +424,8 @@ public class GalleryManageService {
CustomUtil.fourZeroFour(response); CustomUtil.fourZeroFour(response);
return; return;
} }
if (!suffix.equals(".gif")) { if (!suffix.equals(".gif"))
ConvertCmd convertCmd = new ConvertCmd(true); imagePath = GalleryUtil.convertImg(imagePath, suffix);
IMOperation operation = new IMOperation();
operation.addImage(imagePath);
operation.format("webp");
operation.addImage(imagePath.replace(suffix, ".webp"));
try {
convertCmd.run(operation);
new File(imagePath).delete();
imagePath = imagePath.replace(suffix, ".webp");
} catch (IOException | IM4JavaException | InterruptedException e) {
log.error("文件" + imagePath + "转换失败");
}
}
FileDownload.export(request, response, imagePath); FileDownload.export(request, response, imagePath);
} }
} }

View File

@ -13,25 +13,16 @@ import com.lion.lionwebsite.Util.FileDownload;
import com.lion.lionwebsite.Util.Response; import com.lion.lionwebsite.Util.Response;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Calendar; import java.util.Calendar;
import static com.lion.lionwebsite.Util.CustomUtil.fourZeroFour;
@Service @Service
public class PublicServiceImpl { public class PublicServiceImpl {
@ -117,49 +108,4 @@ public class PublicServiceImpl {
public User getUserId(String AuthCode){ public User getUserId(String AuthCode){
return userMapper.selectUserByAuthCode(AuthCode); return userMapper.selectUserByAuthCode(AuthCode);
} }
public void getEhThumbnail(String path, HttpServletResponse response){
String url;
if(!path.contains("/")){
fourZeroFour(response);
return;
}
url = "https://ehgt.org/" + path;
try{
byte[] imageBytes = getImageBytesFromUrl(url);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(imageBytes);
outputStream.close();
}catch (IOException | URISyntaxException e){
e.printStackTrace();
}
}
public static byte[] getImageBytesFromUrl(String imageUrl) throws IOException, URISyntaxException {
URL url = new URI(imageUrl).toURL();
InputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
// 打开URL连接
inputStream = url.openStream();
byte[] buffer = new byte[1024];
int bytesRead;
// 从输入流读取数据并写入输出流
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} finally {
// 关闭流
if (inputStream != null) {
inputStream.close();
}
outputStream.close();
}
// 返回图片的字节数组
return outputStream.toByteArray();
}
} }

View File

@ -1,16 +1,14 @@
package com.lion.lionwebsite.Service; package com.lion.lionwebsite.Service;
import com.lion.lionwebsite.Domain.GalleryForQuery; import com.lion.lionwebsite.Domain.GalleryForQuery;
import com.lion.lionwebsite.Util.FileDownload;
import com.lion.lionwebsite.Util.GalleryUtil; import com.lion.lionwebsite.Util.GalleryUtil;
import com.lion.lionwebsite.Util.Response; import com.lion.lionwebsite.Util.Response;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
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;
@ -18,17 +16,21 @@ import org.jsoup.select.Elements;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.*; import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*; import java.util.*;
import static com.lion.lionwebsite.Util.CustomUtil.fourZeroFour;
@Service @Service
@Slf4j @Slf4j
public class QueryService { public class QueryService {
String CachePath = "/storage/hentaiCache/"; String CachePath = "/storage/galleryCache/thumbnails/";
public String query(String keyword, String prev, String next) { public String query(String keyword, String prev, String next) {
Response response = Response.generateResponse(); Response response = Response.generateResponse();
String get; String result;
String param = "?f_search=" + keyword.replace(" ", "+"); String param = "?f_search=" + keyword.replace(" ", "+");
if(prev != null) if(prev != null)
@ -37,14 +39,14 @@ public class QueryService {
param += "&next=" + next; param += "&next=" + next;
try{ try{
get = requests("https://exhentai.org/" + param, false, null, null); result = GalleryUtil.requests("https://exhentai.org/" + param, "get", null, null);
}catch (IOException e){ }catch (IOException e){
e.printStackTrace(); e.printStackTrace();
response.failure("query failure"); response.failure("query failure");
return response.toJSONString(); return response.toJSONString();
} }
Document parse = Jsoup.parse(get); Document parse = Jsoup.parse(result);
Elements elements = parse.select("body > div.ido > div:nth-child(2) > table > tbody > tr"); Elements elements = parse.select("body > div.ido > div:nth-child(2) > table > tbody > tr");
ArrayList<GalleryForQuery> galleries = new ArrayList<>(); ArrayList<GalleryForQuery> galleries = new ArrayList<>();
@ -73,7 +75,6 @@ public class QueryService {
galleries.add(gallery); galleries.add(gallery);
} }
response.success(new ObjectMapper().valueToTree(galleries).toString()); response.success(new ObjectMapper().valueToTree(galleries).toString());
Elements nextLink = parse.select("#unext"); Elements nextLink = parse.select("#unext");
if(nextLink.hasAttr("href")) if(nextLink.hasAttr("href"))
@ -94,83 +95,28 @@ public class QueryService {
return response.toJSONString(); return response.toJSONString();
} }
public void image(HttpServletResponse response, String path){ public void getEhThumbnail(String path, HttpServletRequest request, HttpServletResponse response){
String imageName = path.substring(path.lastIndexOf("/") + 1); if(!path.contains("/")){
File image = new File(CachePath, imageName); fourZeroFour(response);
if(image.isFile()){ //hit cache return;
log.info("hit cache:{}", imageName);
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(image));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream())){
bufferedInputStream.transferTo(bufferedOutputStream);
}catch (IOException e){
e.printStackTrace();
}
} }
else{ // transfer image and save it as cache String fileName = path.substring(path.lastIndexOf("/") + 1);
log.info("miss cache:{}", imageName); String suffix = fileName.substring(fileName.lastIndexOf("."));
try { fileName = fileName.substring(0, fileName.lastIndexOf("."));
if(!path.startsWith("/t")) File image = new File(CachePath, fileName + ".webp");
path = "/t" + path;
requests("https://s.exhentai.org" + path, true, response, new FileOutputStream(image));
}catch (IOException e){
e.printStackTrace();
}
}
}
public String requests(String url, boolean isDirect, HttpServletResponse response, OutputStream local) throws IOException { if(image.isFile()){
CloseableHttpClient httpClient = HttpClients.createDefault(); FileDownload.export(request, response, image.getAbsolutePath());
CloseableHttpResponse httpResponse; return;
HashMap<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
headers.put("Upgrade-Insecure-Requests", "1");
headers.put("Cookie", "ipb_member_id=5774855; ipb_pass_hash=4b061c3abe25289568b5a8e0123fb3b9; igneous=cea2e08fb; sk=oye107wk02gtomb56x65dmv4qzbn; nw=1");
HttpGet httpGet = new HttpGet(url);
for (Map.Entry<String, String> header: headers.entrySet()){
httpGet.addHeader(header.getKey(), header.getValue());
} }
httpResponse = httpClient.execute(httpGet); path = "https://ehgt.org/" + path;
try(ServletOutputStream outputStream = response.getOutputStream()){
HttpEntity responseEntity = httpResponse.getEntity(); new URI(path).toURL().openConnection().getInputStream().transferTo(new FileOutputStream(CachePath + fileName + suffix));
int statusCode = httpResponse.getStatusLine().getStatusCode(); GalleryUtil.convertImg(CachePath + fileName + suffix, suffix);
outputStream.write(new FileInputStream(image.getAbsolutePath()).readAllBytes());
try { }catch (IOException | URISyntaxException e){
if (statusCode == 200) { e.printStackTrace();
if (isDirect) {
InputStream inputStream = new BufferedInputStream(responseEntity.getContent());
byte[] bytes = inputStream.readAllBytes();
if(response != null) {
OutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.close();
}
if(local != null){
local.write(bytes);
local.close();
}
inputStream.close();
return null;
} else {
StringBuilder stringBuilder = new StringBuilder();
String str;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str).append("\n");
}
return stringBuilder.toString();
}
} else {
System.out.println(statusCode);
return null;
}
}finally {
httpClient.close();
httpResponse.close();
} }
} }
} }

View File

@ -13,12 +13,16 @@ 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;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.*; import java.util.*;
@ -219,6 +223,22 @@ 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"));
try {
convertCmd.run(operation);
new File(imagePath).delete();
return imagePath.replace(suffix, ".webp");
} catch (IOException | IM4JavaException | InterruptedException e) {
System.out.println("文件" + imagePath + "转换失败");
return null;
}
}
/** /**
* 验证链接 * 验证链接