抽取图片格式转换方法;封面图加上缓存;更换缓存存放位置
This commit is contained in:
parent
07b5b1c376
commit
54b1dd47a6
@ -3,6 +3,7 @@ package com.lion.lionwebsite.Controller;
|
||||
|
||||
import com.lion.lionwebsite.Domain.User;
|
||||
import com.lion.lionwebsite.Service.PublicServiceImpl;
|
||||
import com.lion.lionwebsite.Service.QueryService;
|
||||
import com.lion.lionwebsite.Service.RemoteService;
|
||||
import com.lion.lionwebsite.Service.SubService;
|
||||
import com.lion.lionwebsite.Util.Response;
|
||||
@ -28,10 +29,13 @@ public class PublicController {
|
||||
|
||||
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.remoteService = remoteService;
|
||||
this.subService = subService;
|
||||
this.queryService = queryService;
|
||||
}
|
||||
|
||||
@GetMapping("/ip")
|
||||
@ -70,7 +74,7 @@ public class PublicController {
|
||||
black_share_codes.add(ShareCode);
|
||||
|
||||
if(black_share_codes.size() > 100)
|
||||
black_share_codes.remove(0);
|
||||
black_share_codes.removeFirst();
|
||||
}
|
||||
|
||||
@PostMapping("/validate")
|
||||
@ -90,8 +94,7 @@ public class PublicController {
|
||||
}
|
||||
|
||||
@GetMapping("/GalleryManage/ehThumbnail")
|
||||
public void getEhThumbnail(String path, HttpServletResponse response){
|
||||
publicService.getEhThumbnail(path, response);
|
||||
public void getEhThumbnail(String path, HttpServletRequest request, HttpServletResponse response){
|
||||
queryService.getEhThumbnail(path, request, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
package com.lion.lionwebsite.Controller;
|
||||
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@ -22,10 +20,4 @@ public class QueryController {
|
||||
public String query(String keyword, String prev, String next){
|
||||
return queryService.query(keyword, prev, next);
|
||||
}
|
||||
|
||||
@GetMapping("/image")
|
||||
public void image(HttpServletResponse response, String path){
|
||||
queryService.image(response, path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -14,9 +14,6 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.Data;
|
||||
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 java.io.*;
|
||||
@ -30,8 +27,7 @@ import static com.lion.lionwebsite.Util.GalleryUtil.*;
|
||||
@Data
|
||||
@Slf4j
|
||||
public class GalleryManageService {
|
||||
String TargetPath = "/root/gallery/";
|
||||
String cachePath = "/root/galleryCache/";
|
||||
String cachePath = "/storage/galleryCache/onlineImages/";
|
||||
|
||||
GalleryMapper galleryMapper;
|
||||
|
||||
@ -428,20 +424,8 @@ public class GalleryManageService {
|
||||
CustomUtil.fourZeroFour(response);
|
||||
return;
|
||||
}
|
||||
if (!suffix.equals(".gif")) {
|
||||
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();
|
||||
imagePath = imagePath.replace(suffix, ".webp");
|
||||
} catch (IOException | IM4JavaException | InterruptedException e) {
|
||||
log.error("文件" + imagePath + "转换失败");
|
||||
}
|
||||
}
|
||||
if (!suffix.equals(".gif"))
|
||||
imagePath = GalleryUtil.convertImg(imagePath, suffix);
|
||||
FileDownload.export(request, response, imagePath);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,25 +13,16 @@ import com.lion.lionwebsite.Util.FileDownload;
|
||||
import com.lion.lionwebsite.Util.Response;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.ServletOutputStream;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
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.util.Calendar;
|
||||
|
||||
import static com.lion.lionwebsite.Util.CustomUtil.fourZeroFour;
|
||||
|
||||
|
||||
@Service
|
||||
public class PublicServiceImpl {
|
||||
|
||||
@ -117,49 +108,4 @@ public class PublicServiceImpl {
|
||||
public User getUserId(String 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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,14 @@
|
||||
package com.lion.lionwebsite.Service;
|
||||
|
||||
import com.lion.lionwebsite.Domain.GalleryForQuery;
|
||||
import com.lion.lionwebsite.Util.FileDownload;
|
||||
import com.lion.lionwebsite.Util.GalleryUtil;
|
||||
import com.lion.lionwebsite.Util.Response;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.ServletOutputStream;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
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.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
@ -18,17 +16,21 @@ import org.jsoup.select.Elements;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.lion.lionwebsite.Util.CustomUtil.fourZeroFour;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class QueryService {
|
||||
|
||||
String CachePath = "/storage/hentaiCache/";
|
||||
String CachePath = "/storage/galleryCache/thumbnails/";
|
||||
|
||||
public String query(String keyword, String prev, String next) {
|
||||
Response response = Response.generateResponse();
|
||||
String get;
|
||||
String result;
|
||||
String param = "?f_search=" + keyword.replace(" ", "+");
|
||||
|
||||
if(prev != null)
|
||||
@ -37,14 +39,14 @@ public class QueryService {
|
||||
param += "&next=" + next;
|
||||
|
||||
try{
|
||||
get = requests("https://exhentai.org/" + param, false, null, null);
|
||||
result = GalleryUtil.requests("https://exhentai.org/" + param, "get", null, null);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
response.failure("query failure");
|
||||
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");
|
||||
|
||||
ArrayList<GalleryForQuery> galleries = new ArrayList<>();
|
||||
@ -73,7 +75,6 @@ public class QueryService {
|
||||
galleries.add(gallery);
|
||||
}
|
||||
|
||||
|
||||
response.success(new ObjectMapper().valueToTree(galleries).toString());
|
||||
Elements nextLink = parse.select("#unext");
|
||||
if(nextLink.hasAttr("href"))
|
||||
@ -94,83 +95,28 @@ public class QueryService {
|
||||
return response.toJSONString();
|
||||
}
|
||||
|
||||
public void image(HttpServletResponse response, String path){
|
||||
String imageName = path.substring(path.lastIndexOf("/") + 1);
|
||||
File image = new File(CachePath, imageName);
|
||||
if(image.isFile()){ //hit cache
|
||||
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();
|
||||
public void getEhThumbnail(String path, HttpServletRequest request, HttpServletResponse response){
|
||||
if(!path.contains("/")){
|
||||
fourZeroFour(response);
|
||||
return;
|
||||
}
|
||||
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");
|
||||
|
||||
if(image.isFile()){
|
||||
FileDownload.export(request, response, image.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
else{ // transfer image and save it as cache
|
||||
log.info("miss cache:{}", imageName);
|
||||
try {
|
||||
if(!path.startsWith("/t"))
|
||||
path = "/t" + path;
|
||||
requests("https://s.exhentai.org" + path, true, response, new FileOutputStream(image));
|
||||
}catch (IOException e){
|
||||
|
||||
path = "https://ehgt.org/" + path;
|
||||
try(ServletOutputStream outputStream = response.getOutputStream()){
|
||||
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());
|
||||
}catch (IOException | URISyntaxException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String requests(String url, boolean isDirect, HttpServletResponse response, OutputStream local) throws IOException {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpResponse httpResponse;
|
||||
|
||||
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);
|
||||
|
||||
HttpEntity responseEntity = httpResponse.getEntity();
|
||||
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||
|
||||
try {
|
||||
if (statusCode == 200) {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,12 +13,16 @@ 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;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证链接
|
||||
|
||||
Loading…
Reference in New Issue
Block a user