From 43b6292b4b2810560bbf2d22c96bd44c11d4160b Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 22 Jul 2024 10:29:06 +0800
Subject: [PATCH] 将文件获取后压缩在服务器

---
 src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java |  244 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 199 insertions(+), 45 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 27fdcb0..c1d2c61 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
@@ -22,6 +22,7 @@
 import com.dji.sample.media.model.*;
 import com.dji.sample.media.service.IFileService;
 import com.dji.sample.media.util.ImgUtil;
+import com.dji.sample.media.util.MinioFileDownloader;
 import com.dji.sample.patches.dao.GetPatchesMapper;
 import com.dji.sample.patches.model.entity.LotInfo;
 import com.dji.sample.patches.utils.DistrictCodeUtils;
@@ -43,6 +44,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletResponse;
 import java.awt.*;
 import java.io.File;
 import java.io.FileInputStream;
@@ -54,6 +56,8 @@
 import java.util.*;
 import java.util.List;
 import java.util.stream.Collectors;
+
+import static com.dji.sample.media.util.ImageDownloaderAndCompressor.*;
 
 /**
  * @author sean
@@ -108,50 +112,55 @@
     }
 
     @Override
-    public Integer saveFile(String workspaceId, FileUploadDTO file) {
+    public Integer saveFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException {
         MediaFileEntity fileEntity = this.fileUploadConvertToEntity(file);
         fileEntity.setWorkspaceId(workspaceId);
         fileEntity.setFileId(UUID.randomUUID().toString());
+        if (!file.getObjectKey().endsWith(".mp4")){
+        String url = pojo.getEndpoint() + "/" + pojo.getBucket() + file.getObjectKey();
+        File file1 = TbFjServiceImpl.downloadFile(url);
+        Object data = ImgUtil.getInfo(file1);
+        fileEntity.setDroneData(data);
+        }
         return mapper.insert(fileEntity);
     }
 
-    public void saveMarkFile(String workspaceId, FileUploadDTO file) throws IOException, FontFormatException, ImageProcessingException {
-        String endpoint = pojo.getEndpoint();
-        String accessKey = pojo.getAccessKey();
-        String secretKey = pojo.getSecretKey();
-        String bucketName = pojo.getBucket();
-        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);
-        nailEntity.setIsOriginal(false);
-        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(endpoint, accessKey, secretKey, bucketName, nailName, nailFile, "image/jpeg");
-        nailMapper.insert(nailEntity);
-        try {
-            boolean contains = file.getName().contains("~");
-            if (contains) {
-                String name = TimerUtil.getDkbh(file.getName());
-                List<LotInfo> lotInfos = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getDkbh, name));
-                if (!lotInfos.isEmpty()) {
-                    LambdaUpdateWrapper<LotInfo> updateWrapper = new LambdaUpdateWrapper<>();
-                    updateWrapper.eq(LotInfo::getDkbh, name)
-                            .eq(LotInfo::getInvestigate, 0)
-                            .set(LotInfo::getInvestigate, 1);
-                    patchesMapper.update(null, updateWrapper);
-                }
-
-                boolean endsWith = file.getObjectKey().endsWith(".mp4");
-                if (endsWith) {
-                    MediaFileMarkEntity mediaFileMarkEntity = this.fileUploadConvertToMarkEntity(file);
-                    mediaFileMarkEntity.setWorkspaceId(workspaceId);
-                    mediaFileMarkEntity.setFileId(UUID.randomUUID().toString());
-                    markMapper.insert(mediaFileMarkEntity);
+    public void saveMarkFile(String workspaceId, FileUploadDTO file) throws IOException {
+        boolean endsWith = file.getObjectKey().endsWith(".mp4");
+        if (endsWith) {
+            MediaFileNailEntity nailEntity = this.fileUploadConvertToNailEntity((file));
+            nailEntity.setWorkspaceId(workspaceId);
+            nailEntity.setFileId(UUID.randomUUID().toString());
+            nailMapper.insert(nailEntity);
+        } else {
+            String endpoint = pojo.getEndpoint();
+            String accessKey = pojo.getAccessKey();
+            String secretKey = pojo.getSecretKey();
+            String bucketName = pojo.getBucket();
+            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);
+            nailEntity.setIsOriginal(false);
+            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(endpoint, accessKey, secretKey, bucketName, nailName, nailFile, "image/jpeg");
+            nailMapper.insert(nailEntity);
+            try {
+                if (file.getName().contains("~")) {
+                    String name = TimerUtil.getDkbh(file.getName());
+                    List<LotInfo> lotInfos = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getDkbh, name));
+                    if (!lotInfos.isEmpty()) {
+                        LambdaUpdateWrapper<LotInfo> updateWrapper = new LambdaUpdateWrapper<>();
+                        updateWrapper.eq(LotInfo::getDkbh, name)
+                                .eq(LotInfo::getInvestigate, 0)
+                                .set(LotInfo::getInvestigate, 1);
+                        patchesMapper.update(null, updateWrapper);
+                    }
                 }
                 MediaFileMarkEntity mediaFileMarkEntity = this.fileUploadConvertToMarkEntity(file);
                 long timestamp = convertToTimestamp(file.getMetadata().getCreatedTime());
@@ -169,26 +178,41 @@
                 String objectName = mediaFileMarkEntity.getObjectKey(); // 例如 "folder/file.txt"
                 uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, markFile, "image/jpeg");
                 markMapper.insert(mediaFileMarkEntity);
+            } catch (Exception e) {
+                e.printStackTrace();
             }
-        } catch (Exception e) {
-            e.printStackTrace();
         }
-            }
+    }
 
     @Override
     public Object mediaInfo(String filename) {
         String name = filename;
+        List<MediaFileEntity> entitys = mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
+                .eq(MediaFileEntity::getFileName, name));
+        if (entitys != null && !entitys.isEmpty()) {
+            for (MediaFileEntity entity : entitys) {
+                if (entity.getDroneData() != null) {
+                    return entity.getDroneData();
+                }
+            }
+        }
+
         if (!filename.contains("mark")) {
             name = "mark" + filename;
         }
         List<MediaFileMarkEntity> entity = markMapper.selectList(new LambdaQueryWrapper<MediaFileMarkEntity>()
                 .eq(MediaFileMarkEntity::getFileName, name));
-        if (entity != null) {
-            return entity.get(0).getDronedata();
-        } else {
-            return "该图片信息正在加载";
+        if (entity != null && !entity.isEmpty()) {
+            for (MediaFileMarkEntity markEntity : entity) {
+                if (markEntity.getDronedata() != null) {
+                    return markEntity.getDronedata();
+                }
+            }
         }
+
+        return "null";
     }
+
 
     @Override
     public List<MediaFileDTO> getAllFilesByWorkspaceId(String workspaceId) {
@@ -408,6 +432,36 @@
         return ossService.getObjectUrl(OssConfiguration.bucket, mediaFileOpt.get().getObjectKey());
     }
 
+    @Override
+    public ResponseResult downloadImages(List<String> jobIds) {
+        try {
+            String bucketPath = "/data/software/minio-data/cloud-bucket/";
+            List<String> prefixes = getUniqueFilePaths(jobIds);
+
+            MinioFileDownloader downloader = new MinioFileDownloader(bucketPath);
+
+            // 下载并压缩文件到本地目录
+            String localSaveDir = "/data/software/minio-data/zip-bucket/"; // 修改为你想保存的本地目录
+            downloader.downloadAndZipFolders(prefixes, localSaveDir);
+
+            System.out.println("压缩文件已保存到:" + localSaveDir);
+
+        }  catch (Exception e) {
+            e.printStackTrace();
+            return ResponseResult.error("下载失败"+e.getMessage());
+        }
+        return ResponseResult.success("下载成功");
+    }
+    @Override
+    public List<String> getUniqueFilePaths(List<String> jobIds) {
+        return mapper.selectList(
+                        new LambdaQueryWrapper<MediaFileEntity>()
+                                .in(MediaFileEntity::getJobId, jobIds))
+                .stream()
+                .map(MediaFileEntity::getFilePath)
+                .distinct()
+                .collect(Collectors.toList());
+    }
     @Override
     public List<MediaFileDTO> getFilesByWorkspaceAndJobId(String workspaceId, String jobId) {
         return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
@@ -769,7 +823,107 @@
             e.printStackTrace();
         }
     }
+    @Override
+    public PaginationData<MediaFileEntity> getJobId(int pageNum, int pageSize, String workspaceId) {
+        List<MediaFileEntity> allRecords = new ArrayList<>();
+        int currentPageNum = 1;  // 从第一页开始获取记录
 
+        // 用于存储去重后的记录
+        Map<String, MediaFileEntity> uniqueFilesMap = new HashMap<>();
+
+        while (true) {
+            // 分页查询
+            Page<MediaFileEntity> page = new Page<>(currentPageNum, pageSize);
+            Page<MediaFileEntity> resultPage = mapper.selectPage(page, new LambdaQueryWrapper<MediaFileEntity>()
+                    .eq(MediaFileEntity::getWorkspaceId, workspaceId));
+
+            List<MediaFileEntity> result = resultPage.getRecords();
+
+            // 检查是否还有更多记录
+            if (result.isEmpty()) {
+                break;  // 没有更多记录了,停止请求
+            }
+
+            // 根据 fileName 字段进行去重并设置 JobName
+            result.stream()
+                    .peek(mediaFile -> {
+                        String taskNameResult = waylineJobService.getName(mediaFile.getJobId());
+                        mediaFile.setJobName(taskNameResult);
+                    })
+                    .forEach(mediaFile -> uniqueFilesMap.putIfAbsent(mediaFile.getFileName(), mediaFile));
+
+            // 增加当前页数以获取更多记录
+            currentPageNum++;
+        }
+
+        // 获取去重后的实际总数
+        List<MediaFileEntity> uniqueFiles = new ArrayList<>(uniqueFilesMap.values());
+        int uniqueTotal = uniqueFiles.size();
+
+        // 计算当前页的起始和结束索引
+        int fromIndex = Math.min((pageNum - 1) * pageSize, uniqueTotal);
+        int toIndex = Math.min(fromIndex + pageSize, uniqueTotal);
+
+        // 获取当前页的数据
+        List<MediaFileEntity> pagedUniqueFiles = uniqueFiles.subList(fromIndex, toIndex);
+
+        // 创建新的分页对象,使用去重后的总数
+        Page<MediaFileEntity> uniquePage = new Page<>(pageNum, pageSize, uniqueTotal);
+
+        // 使用新的分页对象构造 Pagination
+        Pagination pagination = new Pagination(uniquePage);
+
+        return new PaginationData<>(pagedUniqueFiles, pagination);
+    }
+
+
+
+
+    @Override
+    public PaginationData<MediaFileEntity> getPhotoByJobId(int pageNum, int pageSize, String workspaceId, String jobId) {
+
+        // 分页查询
+        Page<MediaFileEntity> page = new Page<>(pageNum, pageSize);
+        Page<MediaFileEntity> resultPage = mapper.selectPage(page, new LambdaQueryWrapper<MediaFileEntity>()
+                .eq(MediaFileEntity::getJobId, jobId)
+                .eq(MediaFileEntity::getWorkspaceId, workspaceId));
+
+        List<MediaFileEntity> result = resultPage.getRecords();
+
+        // 根据 fileName 字段进行去重
+        Map<String, MediaFileEntity> uniqueFilesMap = result.stream()
+                .collect(Collectors.toMap(
+                        MediaFileEntity::getFileName,
+                        entity -> entity,
+                        (existing, replacement) -> existing // 如果有重复的,保留已有的
+                ));
+
+        List<MediaFileEntity> uniqueFiles = new ArrayList<>(uniqueFilesMap.values());
+
+        // 使用去重后的列表重新计算总数
+        int uniqueTotal = uniqueFiles.size();
+
+        // 创建新的分页对象,使用去重后的总数
+        Page<MediaFileEntity> uniquePage = new Page<>(pageNum, pageSize, uniqueTotal);
+
+        // 截取当前页的数据
+        int fromIndex = Math.min((pageNum - 1) * pageSize, uniqueTotal);
+        int toIndex = Math.min(fromIndex + pageSize, uniqueTotal);
+        List<MediaFileEntity> pagedUniqueFiles = uniqueFiles.subList(fromIndex, toIndex);
+
+        // 使用新的分页对象构造 Pagination
+        Pagination pagination = new Pagination(uniquePage);
+
+        return new PaginationData<>(pagedUniqueFiles, pagination);
+    }
+
+
+
+    public List<MediaFileEntity> getMedia(String jobId) {
+        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
+                .eq(MediaFileEntity::getJobId, jobId)
+                .groupBy(MediaFileEntity::getFileName));  // 使用groupBy去重
+    }
 
     public void updateById(Integer id, MediaFileMarkEntity entity) {
         entity.setIsadd(1);

--
Gitblit v1.9.3