storageNode/src/main/java/lion/Service/DownloadCheckService.java

175 lines
6.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package lion.Service;
import io.netty.channel.Channel;
import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.Promise;
import lion.CustomUtil;
import lion.Domain.GalleryTask;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import lion.Message.AbstractMessage;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j
@Data
public class DownloadCheckService {
Map<Integer, GalleryTask> queue;
String downloadPath = "/root/gallery/hentai/download/";
String storagePath = "/root/gallery/gallery/";
ScheduledThreadPoolExecutor convert_thread;
ArrayList<GalleryTask> compress_queue;
Channel node;
HashMap<Integer, Promise<AbstractMessage>> promises;
EventLoop eventLoop;
public DownloadCheckService(Map<Integer, GalleryTask> queue, HashMap<Integer, Promise<AbstractMessage>> promises){
this.queue = queue;
this.promises = promises;
eventLoop = new DefaultEventLoop();
compress_queue = new ArrayList<>(0);
convert_thread = new ScheduledThreadPoolExecutor(1);
convert_thread.scheduleAtFixedRate(this::compress, 0, 5, TimeUnit.SECONDS);
}
public boolean downloadCheck(){
if(queue.isEmpty())
return false;
log.info("下载检查:{}", Arrays.toString(queue.values().toArray()));
File downloadDirectory = new File(downloadPath);
File[] fileArray = downloadDirectory.listFiles();
if(fileArray == null || fileArray.length == 0)
return false;
ArrayList<File> files = new ArrayList<>(Arrays.asList(fileArray));
boolean result = false;
//扫描进度
Iterator<File> fileIterator = files.iterator();
for(GalleryTask galleryTask: queue.values()){
//跳过已经下载完成或者压缩完成的任务
if(galleryTask.is_compress_complete() || galleryTask.is_compressing()) {
result = true;
continue;
}
while(fileIterator.hasNext()){
File file = fileIterator.next();
if(!file.getName().contains(String.valueOf(galleryTask.getGid())))
continue;
galleryTask.setStatus(GalleryTask.DOWNLOADING);
fileIterator.remove();
if(galleryTask.getName() == null || !galleryTask.getName().equals(file.getName()))
galleryTask.setName(file.getName());
File[] pages = file.listFiles((dir, name) -> !name.equals("galleryinfo.txt"));
if (pages == null || pages.length == 0)
continue;
if(galleryTask.getProceeding() != pages.length)
result = true;
galleryTask.setProceeding(pages.length);
if (new File(file.getPath(), "galleryinfo.txt").exists()) {
result = true;
galleryTask.setStatus(GalleryTask.DOWNLOAD_COMPLETE);
galleryTask.setPath(file.getPath());
}
}
fileIterator = files.iterator();
}
//压缩队列
for(GalleryTask galleryTask: queue.values())
if (galleryTask.is_download_complete()) {
galleryTask.setStatus(GalleryTask.COMPRESSING);
compress_queue.add(galleryTask);
}
return result;
}
/**
* 压缩线程:将压缩队列的任务复制一份,进行转换
*/
public void compress() {
if(compress_queue.isEmpty())
return;
ReentrantLock reentrantLock = new ReentrantLock();
reentrantLock.lock();
ArrayList<GalleryTask> galleryTasks = new ArrayList<>(compress_queue);
compress_queue.clear();
reentrantLock.unlock();
for (GalleryTask galleryTask : galleryTasks) {
try {
log.info("开始压缩:{}", galleryTask.getName());
//创建文件夹
File file = new File(storagePath + galleryTask.getName());
if (file.isDirectory() || file.mkdirs()) {
log.info("{}文件夹创建成功", galleryTask.getName());
} else {
log.error("{}文件夹创建失败", galleryTask.getName());
continue;
}
//生成压缩包
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
log.info("{}压缩完成", galleryTask.getName());
FileUtil.del(galleryTask.getPath());
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
} catch (Exception e){
log.error("{}压缩失败:{}", galleryTask, e.getMessage());
}
}
}
/**
* 检查改任务是否为已完成任务如已完成则返回true若未完成则加入队列
* @return true if compress complete, false otherwise
*/
public boolean addToQueue(GalleryTask galleryTask){
//是否含有名字,进行中任务一般有名字,没有名字则肯定为初始任务,存在名字至少在下载路径出现过
if(galleryTask.getName() == null || !galleryTask.getName().isEmpty()){
queue.putIfAbsent(galleryTask.getGid(), galleryTask);
return false;
}
//查询hah下载路径中是否存在该任务下载路径存在则为下载中或下载完成任务加入队列
if(new File(downloadPath + galleryTask.getGid()).isDirectory()){
queue.putIfAbsent(galleryTask.getGid(), galleryTask);
return false;
}
//查询存放路径中是否含有该任务的压缩包,存在则为下载完成任务
if(new File(storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip").exists()){
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
CustomUtil.notifyMe(String.format("任务:%s在添加时已下载完成更新任务状态", galleryTask.getName()));
return true;
}
//异常情况,发送通知
CustomUtil.notifyMe(String.format("任务:%s存在名字但是下载路径为空且不存在压缩包", galleryTask.getName() ));
return false;
}
}