diff --git a/.idea/misc.xml b/.idea/misc.xml index 82dbec8..c3f3b0a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/main/java/lion/Domain/GalleryTask.java b/src/main/java/lion/Domain/GalleryTask.java index 3c59457..09b7904 100644 --- a/src/main/java/lion/Domain/GalleryTask.java +++ b/src/main/java/lion/Domain/GalleryTask.java @@ -15,6 +15,8 @@ public class GalleryTask { public static byte COMPRESS_COMPLETE = 4; + public static byte COMPRESSING = 5; + public static byte DOWNLOAD_ALL = 3; public static byte DOWNLOAD_PREVIEW = 2; @@ -47,4 +49,9 @@ public class GalleryTask { public boolean is_compress_complete(){ return status == COMPRESS_COMPLETE; } + + @JsonIgnore + public boolean is_compressing(){ + return status == COMPRESSING; + } } diff --git a/src/main/java/lion/Service/DownloadCheckService.java b/src/main/java/lion/Service/DownloadCheckService.java index ac125c0..8b618e1 100644 --- a/src/main/java/lion/Service/DownloadCheckService.java +++ b/src/main/java/lion/Service/DownloadCheckService.java @@ -55,6 +55,12 @@ public class DownloadCheckService { //扫描进度 Iterator fileIterator = files.iterator(); for(GalleryTask galleryTask: queue){ + //跳过已经下载完成或者压缩完成的任务 + if(galleryTask.is_download_complete() + || galleryTask.is_compress_complete() + || galleryTask.is_compressing()) + continue; + while(fileIterator.hasNext()){ File file = fileIterator.next(); if(!file.getName().contains(String.valueOf(galleryTask.getGid()))) @@ -78,8 +84,10 @@ public class DownloadCheckService { //转格式队列 for(GalleryTask galleryTask: queue) - if (galleryTask.is_download_complete()) + if (galleryTask.is_download_complete()) { + galleryTask.setStatus(GalleryTask.COMPRESSING); convert_queue.add(galleryTask); + } return true; } @@ -102,7 +110,7 @@ public class DownloadCheckService { galleryTask.setStatus(ErrorCode.COMPRESS_ERROR); continue; } - //长度相同比较字典序,否则比较长度 + //仅比较字典序 images = Arrays.stream(images).sorted(Comparator.naturalOrder()).toArray(File[]::new); //创建文件夹 @@ -135,6 +143,7 @@ public class DownloadCheckService { operation = new IMOperation(); operation.addImage(images[i].getAbsolutePath()); operation.format("webp"); + operation.resize(2000, 2000); operation.addImage(storagePath + galleryTask.getName() + "/" + images[i].getName().replace(".png", ".webp").replace(".jpg", ".webp")); try { convertCmd.run(operation); @@ -144,12 +153,13 @@ public class DownloadCheckService { break; } } + 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.DOWNLOAD_COMPLETE); + galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE); } } diff --git a/src/main/java/lion/storageNode.java b/src/main/java/lion/storageNode.java index 44ecc09..50817ae 100644 --- a/src/main/java/lion/storageNode.java +++ b/src/main/java/lion/storageNode.java @@ -20,7 +20,6 @@ import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.util.ArrayList; -import java.util.ListIterator; import java.util.concurrent.*; import java.util.concurrent.locks.ReentrantLock; @@ -84,7 +83,7 @@ public class storageNode { } lock.unlock(); //检查 - if (!downloadCheckService.downloadCheck()) { + if (!downloadCheckService.downloadCheck() && queue.isEmpty()) { counter++; if(server != null && server.isActive() && counter > 10) { server.writeAndFlush(new MaintainMessage()); @@ -93,19 +92,15 @@ public class storageNode { return; } //发送 + //上锁后再发送,避免出现发送完之后再下载完成 + lock.lock(); DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage(); downloadStatusMessage.setGalleryTasks(queue.toArray(GalleryTask[]::new)); server.writeAndFlush(downloadStatusMessage); - - ListIterator listIterator = queue.listIterator(); - lock.lock(); - while (listIterator.hasNext()) { - GalleryTask galleryTask = listIterator.next(); - if (galleryTask.is_download_complete()) - listIterator.remove(); - } - lock.unlock(); log.info("任务状态发送完成"); + + queue.removeIf(GalleryTask::is_compress_complete); + lock.unlock(); }catch (Exception e){ e.printStackTrace(); try (OutputStream outputStream = new FileOutputStream("/root/gallery/storageNode/err.txt")){