去除格式转换,仅下载图片;去除图片查询,不再参与在线看
This commit is contained in:
parent
45a0cee73c
commit
9b389d7756
7
pom.xml
7
pom.xml
@ -33,12 +33,6 @@
|
||||
<version>1.18.30</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.im4java</groupId>
|
||||
<artifactId>im4java</artifactId>
|
||||
<version>1.4.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
@ -101,6 +95,7 @@
|
||||
<buildArgs>
|
||||
<arg>-H:+ReportExceptionStackTraces</arg>
|
||||
<arg>--gc=G1</arg>
|
||||
<arg>-Ob</arg>
|
||||
<arg>--enable-url-protocols=https</arg>
|
||||
<arg>-H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json</arg>
|
||||
</buildArgs>
|
||||
|
||||
@ -17,26 +17,15 @@ public class GalleryTask {
|
||||
|
||||
public static byte COMPRESSING = 5;
|
||||
|
||||
|
||||
public static byte DOWNLOAD_ALL = 3;
|
||||
public static byte DOWNLOAD_PREVIEW = 2;
|
||||
public static byte DOWNLOAD_SOURCE = 1;
|
||||
|
||||
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private String name;
|
||||
|
||||
private int gid;
|
||||
|
||||
private int pages;
|
||||
|
||||
private byte status;
|
||||
|
||||
private int proceeding;
|
||||
|
||||
private byte type;
|
||||
|
||||
@JsonIgnore
|
||||
private String path;
|
||||
|
||||
|
||||
@ -10,14 +10,10 @@ public class AbstractMessage {
|
||||
|
||||
public static final byte RESPONSE_MESSAGE = 0;
|
||||
|
||||
public static final byte GALLERY_PAGE_QUERY_MESSAGE = 5;
|
||||
|
||||
public static final byte IDENTITY_MESSAGE = 6;
|
||||
|
||||
public static final byte MAINTAIN_MESSAGE = 7;
|
||||
|
||||
public static final byte GALLERY_REQUEST_MESSAGE = 101;
|
||||
|
||||
public byte messageType;
|
||||
|
||||
public int messageId;
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
package lion.Message.Main;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lion.Message.AbstractMessage;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GalleryPageQueryMessage extends AbstractMessage {
|
||||
{
|
||||
messageType = GALLERY_PAGE_QUERY_MESSAGE;
|
||||
}
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
String name;
|
||||
|
||||
int page;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
String pageName;
|
||||
|
||||
byte result;
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package lion.Message.Main;
|
||||
|
||||
import lion.Message.AbstractMessage;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
//请求预览/压缩包
|
||||
public class GalleryRequestMessage extends AbstractMessage {
|
||||
|
||||
public static final byte SOURCE = 1;
|
||||
public static final byte PREVIEW = 2;
|
||||
|
||||
public static final byte COMPRESS_SOURCE = 3;
|
||||
|
||||
{
|
||||
messageType = GALLERY_REQUEST_MESSAGE;
|
||||
}
|
||||
|
||||
String galleryName;
|
||||
|
||||
byte type;
|
||||
|
||||
short page;
|
||||
|
||||
short port;
|
||||
}
|
||||
@ -41,10 +41,8 @@ public class MessageCodec extends ByteToMessageCodec<AbstractMessage> {
|
||||
AbstractMessage abstractMessage = switch (messageType){
|
||||
case AbstractMessage.DOWNLOAD_POST_MESSAGE -> objectMapper.readValue(metadata, DownloadPostMessage.class);
|
||||
case AbstractMessage.DOWNLOAD_STATUS_MESSAGE -> objectMapper.readValue(metadata, DownloadStatusMessage.class);
|
||||
case AbstractMessage.GALLERY_REQUEST_MESSAGE -> objectMapper.readValue(metadata, GalleryRequestMessage.class);
|
||||
case AbstractMessage.RESPONSE_MESSAGE -> objectMapper.readValue(metadata, ResponseMessage.class);
|
||||
case AbstractMessage.DELETE_GALLERY_MESSAGE -> objectMapper.readValue(metadata, DeleteGalleryMessage.class);
|
||||
case AbstractMessage.GALLERY_PAGE_QUERY_MESSAGE -> objectMapper.readValue(metadata, GalleryPageQueryMessage.class);
|
||||
case AbstractMessage.IDENTITY_MESSAGE -> objectMapper.readValue(metadata, IdentityMessage.class);
|
||||
case AbstractMessage.MAINTAIN_MESSAGE -> objectMapper.readValue(metadata, MaintainMessage.class);
|
||||
default -> null;
|
||||
|
||||
@ -1,102 +0,0 @@
|
||||
package lion.Service;
|
||||
import lion.ErrorCode.ErrorCode;
|
||||
import lion.Message.Main.GalleryPageQueryMessage;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class DeliveryService {
|
||||
|
||||
static String storagePath = "/root/gallery/gallery/";
|
||||
//缓存排序后的页数
|
||||
static LinkedHashMap<String, ArrayList<String>> pageCache;
|
||||
|
||||
static ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
|
||||
|
||||
|
||||
static {
|
||||
pageCache = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public static byte deliveryPreview(String name, short page, short port){
|
||||
if(!pageCache.containsKey(name) || page > pageCache.get(name).size()){ //缓存中不存在或者页数超出缓存数量
|
||||
byte result;
|
||||
if((result = pageCache(name)) != 0)
|
||||
return result;
|
||||
}
|
||||
|
||||
//0页为缩略图
|
||||
if(page == 0){
|
||||
return delivery(new File(storagePath, name + "/thumbnail.webp"), port);
|
||||
}else{
|
||||
return delivery(new File(storagePath, name + "/" + pageCache.get(name).get(page)), port);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte delivery(File file, short port){
|
||||
if(!file.exists())
|
||||
return ErrorCode.FILE_NOT_FOUND;
|
||||
|
||||
singleThreadPool.submit(() -> {
|
||||
try(SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("lionwebsite.xyz", port));
|
||||
FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ)) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
while (fileChannel.read(buffer)!=-1){
|
||||
buffer.flip();
|
||||
socketChannel.write(buffer);
|
||||
buffer.clear();
|
||||
}
|
||||
socketChannel.shutdownOutput();
|
||||
return 0;
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
return ErrorCode.IO_ERROR;
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static byte pageQuery(GalleryPageQueryMessage gpqm){
|
||||
if(!pageCache.containsKey(gpqm.getName())){
|
||||
byte result;
|
||||
if((result = pageCache(gpqm.getName())) != 0)
|
||||
return result;
|
||||
}
|
||||
ArrayList<String> pages = pageCache.get(gpqm.getName());
|
||||
if(pages.size() <= gpqm.getPage())
|
||||
return ErrorCode.FILE_NOT_FOUND;
|
||||
gpqm.setPageName(pageCache.get(gpqm.getName()).get(gpqm.getPage()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static byte pageCache(String name){
|
||||
File directory = new File(storagePath, name);
|
||||
if(!directory.isDirectory()) {
|
||||
System.out.printf("文件夹%s没找到\n", directory.getAbsolutePath());
|
||||
return ErrorCode.FILE_NOT_FOUND;
|
||||
}
|
||||
ArrayList<String> pageList = new ArrayList<>();
|
||||
File[] files = directory.listFiles(((dir, name1) -> !name1.equals("galleryinfo.txt") && !name1.equals("thumbnail.webp") && !name1.endsWith(".zip")));
|
||||
|
||||
if(files == null)
|
||||
return ErrorCode.FILE_NOT_FOUND;
|
||||
ArrayList<File> fileArrayList = new ArrayList<>(Arrays.asList(files));
|
||||
fileArrayList.sort(Comparator.naturalOrder());
|
||||
pageList.add("thumbnail.webp");
|
||||
fileArrayList.forEach((f) -> pageList.add(f.getName()));
|
||||
pageCache.put(name, pageList);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -11,9 +11,6 @@ import cn.hutool.core.util.ZipUtil;
|
||||
import lion.Message.AbstractMessage;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.im4java.core.ConvertCmd;
|
||||
import org.im4java.core.IM4JavaException;
|
||||
import org.im4java.core.IMOperation;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
@ -120,8 +117,6 @@ public class DownloadCheckService {
|
||||
galleryTask.setStatus(ErrorCode.COMPRESS_ERROR);
|
||||
continue;
|
||||
}
|
||||
//仅比较字典序
|
||||
images = Arrays.stream(images).sorted(Comparator.naturalOrder()).toArray(File[]::new);
|
||||
|
||||
//创建文件夹
|
||||
File file = new File(storagePath + galleryTask.getName());
|
||||
@ -132,43 +127,12 @@ public class DownloadCheckService {
|
||||
continue;
|
||||
}
|
||||
|
||||
//thumbnail
|
||||
log.info("文件" + images[0].getName() + ",转换为thumbnail.webp");
|
||||
convert_local(galleryTask, images[0], true);
|
||||
//生成压缩包
|
||||
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
|
||||
log.info(galleryTask.getName() + "压缩完成" );
|
||||
|
||||
if ((galleryTask.getType() & GalleryTask.DOWNLOAD_PREVIEW) != 0)
|
||||
for (int i = 0; i < images.length; i++) {
|
||||
log.info("文件" + images[i].getName() + ",转换为webp[" + i + "/" + images.length + "]");
|
||||
convert_local(galleryTask, images[i], false);
|
||||
}
|
||||
|
||||
if ((galleryTask.getType() & GalleryTask.DOWNLOAD_SOURCE) != 0) {
|
||||
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
|
||||
log.info(galleryTask.getName() + "压缩完成" );
|
||||
}
|
||||
FileUtil.del(galleryTask.getPath());
|
||||
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
|
||||
}
|
||||
}
|
||||
|
||||
public void convert_local(GalleryTask galleryTask, File file, boolean isThumbnail){
|
||||
ConvertCmd convertCmd = new ConvertCmd(true);
|
||||
IMOperation operation = new IMOperation();
|
||||
operation.addImage(file.getAbsolutePath());
|
||||
|
||||
if(isThumbnail) {
|
||||
operation.resize(500, 500);
|
||||
operation.format("webp");
|
||||
operation.addImage(storagePath + galleryTask.getName() + "/thumbnail.webp");
|
||||
} else {
|
||||
operation.format("webp");
|
||||
operation.addImage(storagePath + galleryTask.getName() + "/" + file.getName().replace(".png", ".webp").replace(".jpg", ".webp"));
|
||||
}
|
||||
try {
|
||||
convertCmd.run(operation);
|
||||
} catch (IOException | IM4JavaException | InterruptedException e) {
|
||||
log.error("文件" + file.getName() + "转换失败");
|
||||
galleryTask.setStatus(ErrorCode.COMPRESS_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import lion.Domain.GalleryTask;
|
||||
import lion.Message.*;
|
||||
import lion.Message.Main.*;
|
||||
import lion.Service.DeleteService;
|
||||
import lion.Service.DeliveryService;
|
||||
import lion.Service.DownloadCheckService;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
@ -155,21 +154,6 @@ public class storageNode {
|
||||
ResponseMessage responseMessage = new ResponseMessage(deleteGalleryMessage.messageId, result);
|
||||
ctx.writeAndFlush(responseMessage);
|
||||
}
|
||||
case AbstractMessage.GALLERY_REQUEST_MESSAGE -> {
|
||||
GalleryRequestMessage grm = (GalleryRequestMessage) abstractMessage;
|
||||
byte result = DeliveryService.deliveryPreview(grm.getGalleryName(), grm.getPage(), grm.getPort());
|
||||
|
||||
ResponseMessage responseMessage = new ResponseMessage();
|
||||
responseMessage.messageId = grm.messageId;
|
||||
responseMessage.setResult(result);
|
||||
ctx.writeAndFlush(responseMessage);
|
||||
}
|
||||
case AbstractMessage.GALLERY_PAGE_QUERY_MESSAGE -> {
|
||||
GalleryPageQueryMessage gpqm = (GalleryPageQueryMessage) abstractMessage;
|
||||
byte result = DeliveryService.pageQuery(gpqm);
|
||||
gpqm.setResult(result);
|
||||
ctx.writeAndFlush(gpqm);
|
||||
}
|
||||
}
|
||||
//
|
||||
// //修复预览
|
||||
|
||||
Loading…
Reference in New Issue
Block a user