From dfae9043b11e788fa4d442e4576d73571e976cea Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Fri, 02 Aug 2024 08:49:47 +0800
Subject: [PATCH] 修改异步上传图片文件、增加图斑列表刷新

---
 src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java   |  125 +++++++++++++++++++++++------------------
 src/main/java/com/dji/sample/media/service/impl/MediaServiceImpl.java  |    2 
 src/main/java/com/dji/sample/media/controller/FileController.java      |    5 +
 src/main/java/com/dji/sample/media/service/IFileService.java           |    8 ++
 src/main/java/com/dji/sample/patches/controller/PatchesController.java |    1 
 5 files changed, 81 insertions(+), 60 deletions(-)

diff --git a/src/main/java/com/dji/sample/media/controller/FileController.java b/src/main/java/com/dji/sample/media/controller/FileController.java
index 7fad72c..d1c231d 100644
--- a/src/main/java/com/dji/sample/media/controller/FileController.java
+++ b/src/main/java/com/dji/sample/media/controller/FileController.java
@@ -178,4 +178,9 @@
     public ResponseResult downloadImages(@RequestBody List<String> jobIds) {
         return fileService.downloadImages(jobIds);
     }
+
+    @GetMapping("/refreshPhoto")
+    public void refreshPhoto(){
+        fileService.getNoaddFile();
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/com/dji/sample/media/service/IFileService.java b/src/main/java/com/dji/sample/media/service/IFileService.java
index 19b1696..6d29baa 100644
--- a/src/main/java/com/dji/sample/media/service/IFileService.java
+++ b/src/main/java/com/dji/sample/media/service/IFileService.java
@@ -37,9 +37,11 @@
      * @return
      */
     Integer saveFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException;
-    void saveNailFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException;
 
-    void saveZipFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException;
+    void saveFiles(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException;
+//    void saveNailFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException;
+
+//    void saveZipFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException;
     void updateNailMediaFileNames(String jobId);
     PaginationData<MediaFileEntity> getJobId( int pageNum, int pageSize,String workspaceId);
     PaginationData<MediaFileEntity> getPhotoByJobId(int page,int pageSize ,String workspaceId,String jobId);
@@ -119,4 +121,6 @@
     Boolean updateMediaFile(String workspaceId, MediaFileEntity mediaFileEntity);
 
     int deleteMedia(String workspaceId, String fileId);
+
+    void getNoaddFile();
 }
diff --git a/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java b/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
index b6a047e..7c98643 100644
--- a/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
+++ b/src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
@@ -56,6 +56,8 @@
 import java.time.ZoneId;
 import java.util.*;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 
 import static com.dji.sample.media.util.ImageDownloaderAndCompressor.*;
@@ -121,64 +123,75 @@
         fileEntity.setFileId(UUID.randomUUID().toString());
         return mapper.insert(fileEntity);
     }
-
-    public void saveNailFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException {
+    public void saveFiles(String workspaceId, FileUploadDTO file) {
+        // 更新文件状态
         updateStatue(file.getName());
-        boolean endsWith = file.getObjectKey().endsWith(".mp4");
-        if (endsWith) {
-            MediaFileNailEntity nailEntity = this.fileUploadConvertToNailEntity((file));
-            nailEntity.setWorkspaceId(workspaceId);
-            nailEntity.setFileId(UUID.randomUUID().toString());
-            nailMapper.insert(nailEntity);
-            MediaFileZipEntity zipEntity = this.fileUploadConvertToZipEntity((file));
-            zipEntity.setWorkspaceId(workspaceId);
-            zipEntity.setFileId(UUID.randomUUID().toString());
-            zipMapper.insert(zipEntity);
-        } else {
-            String url = pojo.getEndpoint() + "/" + pojo.getBucket() + file.getObjectKey();
-            File file1 = TbFjServiceImpl.downloadFile(url);
-            File nailFile = new File(ImgZipUtil.compressImage(file1, 50).toURI());
-            MediaFileNailEntity nailEntity = this.fileUploadConvertToNailEntity(file);
-            Object data = ImgUtil.getInfo(file1);
-            nailEntity.setIsadd(0);
-            nailEntity.setIsOriginal(false);
-            nailEntity.setDronedata(data);
-            nailEntity.setWorkspaceId(workspaceId);
-            nailEntity.setFileName("nail" + file.getName());
-            nailEntity.setObjectKey("/nail" + file.getPath() + "/" + file.getName());
-            nailEntity.setFilePath("nail" + file.getPath());
-            String nailName = nailEntity.getObjectKey();
-            nailEntity.setFileId(UUID.randomUUID().toString());
-            uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", file.getObjectKey(), file1, "image/jpeg");
-            uploadFile(pojo.getEndpoint(), pojo.getAccessKey(), pojo.getSecretKey(), pojo.getBucket(), nailName, nailFile, "image/jpeg");
-            uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", nailName, nailFile, "image/jpeg");
-            nailMapper.insert(nailEntity);
-        }
-    }
 
-    public void saveZipFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException {
-        updateStatue(file.getName());
-        boolean endsWith = file.getObjectKey().endsWith(".mp4");
-        if (endsWith) {
-            MediaFileZipEntity zipEntity = this.fileUploadConvertToZipEntity((file));
-            zipEntity.setWorkspaceId(workspaceId);
-            zipEntity.setFileId(UUID.randomUUID().toString());
-            zipMapper.insert(zipEntity);
-        } else {
-            String url = pojo.getEndpoint() + "/" + pojo.getBucket() + file.getObjectKey();
-            File file1 = TbFjServiceImpl.downloadFile(url);
-            File nailFile = new File(ImgZipUtil.compressImageAndGetFile(file1, 0.5f).toURI());
-            MediaFileZipEntity zipEntity = this.fileUploadConvertToZipEntity(file);
-            zipEntity.setIsOriginal(false);
-            zipEntity.setWorkspaceId(workspaceId);
-            zipEntity.setFileName("zip" + file.getName());
-            zipEntity.setObjectKey("/zip" + file.getPath() + "/" + file.getName());
-            zipEntity.setFilePath("zip" + file.getPath());
-            String nailName = zipEntity.getObjectKey();
-            zipEntity.setFileId(UUID.randomUUID().toString());
-            uploadFile(pojo.getEndpoint(), pojo.getAccessKey(), pojo.getSecretKey(), pojo.getBucket(), nailName, nailFile, "image/jpeg");
-            uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", nailName, nailFile, "image/jpeg");
-            zipMapper.insert(zipEntity);
+        // 使用 CompletableFuture 异步执行
+        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
+            try {
+                // 下载文件
+                boolean endsWithMp4 = file.getObjectKey().endsWith(".mp4");
+
+                if (endsWithMp4) {
+                    // 处理视频文件
+                    MediaFileZipEntity zipEntity = this.fileUploadConvertToZipEntity(file);
+                    zipEntity.setWorkspaceId(workspaceId);
+                    zipEntity.setFileId(UUID.randomUUID().toString());
+                    zipMapper.insert(zipEntity);
+
+                    // 处理视频文件的其他逻辑(如果有)
+                } else {
+                    // 处理图片文件
+                    String url = pojo.getEndpoint() + "/" + pojo.getBucket() + file.getObjectKey();
+                    File downloadedFile = TbFjServiceImpl.downloadFile(url);
+                    File nailFile = new File(ImgZipUtil.compressImage(downloadedFile, 50).toURI());
+                    MediaFileNailEntity nailEntity = this.fileUploadConvertToNailEntity(file);
+                    Object data = ImgUtil.getInfo(downloadedFile);
+                    nailEntity.setIsadd(0);
+                    nailEntity.setIsOriginal(false);
+                    nailEntity.setDronedata(data);
+                    nailEntity.setWorkspaceId(workspaceId);
+                    nailEntity.setFileName("nail" + file.getName());
+                    nailEntity.setObjectKey("/nail" + file.getPath() + "/" + file.getName());
+                    nailEntity.setFilePath("nail" + file.getPath());
+                    String nailName = nailEntity.getObjectKey();
+                    nailEntity.setFileId(UUID.randomUUID().toString());
+
+                    // 上传图片文件
+                    uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", file.getObjectKey(), downloadedFile, "image/jpeg");
+                    uploadFile(pojo.getEndpoint(), pojo.getAccessKey(), pojo.getSecretKey(), pojo.getBucket(), nailName, nailFile, "image/jpeg");
+                    uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", nailName, nailFile, "image/jpeg");
+                    nailMapper.insert(nailEntity);
+
+                    // 处理压缩后的图片文件(如果有)
+                    File zipFile = new File(ImgZipUtil.compressImageAndGetFile(downloadedFile, 0.5f).toURI());
+                    MediaFileZipEntity zipEntity = this.fileUploadConvertToZipEntity(file);
+                    zipEntity.setIsOriginal(false);
+                    zipEntity.setWorkspaceId(workspaceId);
+                    zipEntity.setFileName("zip" + file.getName());
+                    zipEntity.setObjectKey("/zip" + file.getPath() + "/" + file.getName());
+                    zipEntity.setFilePath("zip" + file.getPath());
+                    String zipName = zipEntity.getObjectKey();
+                    zipEntity.setFileId(UUID.randomUUID().toString());
+
+                    // 上传压缩文件
+                    uploadFile(pojo.getEndpoint(), pojo.getAccessKey(), pojo.getSecretKey(), pojo.getBucket(), zipName, zipFile, "image/jpeg");
+                    uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", zipName, zipFile, "image/jpeg");
+                    zipMapper.insert(zipEntity);
+                }
+            } catch (IOException | ImageProcessingException e) {
+                // 处理异常
+                e.printStackTrace();
+            }
+        });
+
+        // 等待异步任务完成
+        try {
+            future.get();
+        } catch (InterruptedException | ExecutionException e) {
+            Thread.currentThread().interrupt();
+            // 处理异步任务的中断和异常
         }
     }
 
diff --git a/src/main/java/com/dji/sample/media/service/impl/MediaServiceImpl.java b/src/main/java/com/dji/sample/media/service/impl/MediaServiceImpl.java
index d02bea9..e5a09dd 100644
--- a/src/main/java/com/dji/sample/media/service/impl/MediaServiceImpl.java
+++ b/src/main/java/com/dji/sample/media/service/impl/MediaServiceImpl.java
@@ -227,7 +227,7 @@
         String objectKey = callback.getFile().getObjectKey();
         callback.getFile().setPath(objectKey.substring(objectKey.indexOf("/") + 1, objectKey.lastIndexOf("/")));
         int count =fileService.saveFile(job.getWorkspaceId(), callback.getFile());
-//        fileService.saveNailFile(job.getWorkspaceId(), callback.getFile());
+        fileService.saveFiles(job.getWorkspaceId(), callback.getFile());
 //        fileService.saveZipFile(job.getWorkspaceId(),callback.getFile());
         return count > 0;
     }
diff --git a/src/main/java/com/dji/sample/patches/controller/PatchesController.java b/src/main/java/com/dji/sample/patches/controller/PatchesController.java
index 2d9d74d..4f410ba 100644
--- a/src/main/java/com/dji/sample/patches/controller/PatchesController.java
+++ b/src/main/java/com/dji/sample/patches/controller/PatchesController.java
@@ -74,7 +74,6 @@
                                                         @RequestParam(name = "dkmj", required = false) Double dkmj,
                                                         @RequestParam(name = "isPush",required = false) Integer isPush
                                                         ) {
-        fileService.getNoaddFile();
         //调用service分页查询
         PatchesParam param = PatchesParam.builder()
                 .page(page)

--
Gitblit v1.9.3