From 3430a3dd889ba51d06cbdb77014aa63d0a070e48 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Fri, 16 Aug 2024 16:38:08 +0800
Subject: [PATCH] 优化国土对接接口

---
 src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java |  162 ++++++++++++++++++++++++++++++++---------------------
 1 files changed, 97 insertions(+), 65 deletions(-)

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..950203a 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.*;
@@ -108,79 +110,104 @@
                 .eq(MediaFileEntity::getFileId, fileId));
         return Optional.ofNullable(fileEntity);
     }
-
+    public void saveMarkFile(String workspaceId, MediaFileEntity file,File file1) {
+        try {
+            MediaFileMarkEntity mediaFileMarkEntity= new MediaFileMarkEntity();
+            mediaFileMarkEntity.setWorkspaceId(workspaceId);
+            mediaFileMarkEntity.setIsadd(0);
+            mediaFileMarkEntity.setIsOriginal(false);
+            mediaFileMarkEntity.setFileId(UUID.randomUUID().toString());
+            mediaFileMarkEntity.setObjectKey("/mark" + file.getFilePath()+ "/" + file.getFileName());
+            mediaFileMarkEntity.setFileName("mark" + file.getFileName());
+            mediaFileMarkEntity.setFilePath("mark" + file.getFilePath());
+            uploadFile(pojo.getEndpoint(), pojo.getAccessKey(), pojo.getSecretKey(), pojo.getBucket(), mediaFileMarkEntity.getObjectKey(), file1, "image/jpeg");
+            markMapper.insert(mediaFileMarkEntity);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
     @Override
     public Boolean checkExist(String workspaceId, String fingerprint) {
         return this.getMediaByFingerprint(workspaceId, fingerprint).isPresent();
     }
 
     @Override
-    public Integer saveFile(String workspaceId, FileUploadDTO file) {
+    public Integer saveFile(String workspaceId, FileUploadDTO file) throws ImageProcessingException, IOException {
         MediaFileEntity fileEntity = this.fileUploadConvertToEntity(file);
         fileEntity.setWorkspaceId(workspaceId);
         fileEntity.setFileId(UUID.randomUUID().toString());
+        //避免对视频处理引发报错
+        if (file.getObjectKey().endsWith("jpeg")){
+            fileEntity.setIsadd(0);
+            String url = pojo.getEndpoint() + "/" + pojo.getBucket() + file.getObjectKey();
+            File downloadedFile = TbFjServiceImpl.downloadFile(url);
+            Object data = ImgUtil.getInfo(downloadedFile);
+            fileEntity.setDroneData(data);
+            updateStatue(file.getName());
+        }
         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);
+
+        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();
         }
     }
 
-    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);
-        }
-    }
 
     public void updateStatue(String filename) {
         if (filename.contains("~")) {
@@ -707,6 +734,10 @@
 
         // 如果所有 fileName 都包含 '~' 或者都不包含 '~',将 is_add 字段改为 1
         if (allContainTilde || noneContainTilde) {
+            mediaFiles.forEach(file -> {
+                file.setIsadd(1);
+                updateMediaById(file.getId(), file);
+            });
             return;
         }
 
@@ -1073,7 +1104,7 @@
     }
 
     public void updateById(Integer id, MediaFileNailEntity entity) {
-        entity.setIsadd(1);
+//        entity.setIsadd(1);
         UpdateWrapper<MediaFileNailEntity> updateWrapper = new UpdateWrapper<>();
         updateWrapper.eq("id", id);
         nailMapper.update(entity, updateWrapper);
@@ -1087,18 +1118,19 @@
 
 
     public void updateMediaById(Integer id, MediaFileEntity entity) {
+        entity.setIsadd(1);
         UpdateWrapper<MediaFileEntity> updateWrapper = new UpdateWrapper<>();
         updateWrapper.eq("id", id);
         mapper.update(entity, updateWrapper);
     }
 
     public void getNoaddFile() {
-        List<MediaFileNailEntity> markEntities = nailMapper.selectList(
-                new LambdaQueryWrapper<MediaFileNailEntity>().eq(MediaFileNailEntity::getIsadd, 0));
-        for (MediaFileNailEntity mark : markEntities) {
-            updateNailMediaFileNames(mark.getJobId());
-            updateMediaFileNames(mark.getJobId());
-            updateMediaZipFileNames(mark.getJobId());
+        List<MediaFileEntity> mediaEntities = mapper.selectList(
+                new LambdaQueryWrapper<MediaFileEntity>().eq(MediaFileEntity::getIsadd, 0));
+        for (MediaFileEntity media : mediaEntities) {
+//            updateNailMediaFileNames(media.getJobId());
+            updateMediaFileNames(media.getJobId());
+//            updateMediaZipFileNames(media.getJobId());
         }
     }
 

--
Gitblit v1.9.3