rain
2024-07-29 86384846e317381e7bb9e31ee76760e4ecb3ee67
src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
@@ -19,6 +19,7 @@
import com.dji.sample.media.dao.IFileMapper;
import com.dji.sample.media.dao.IMarkMapper;
import com.dji.sample.media.dao.INailMapper;
import com.dji.sample.media.dao.IZipMapper;
import com.dji.sample.media.model.*;
import com.dji.sample.media.service.IFileService;
import com.dji.sample.media.util.ImgUtil;
@@ -74,6 +75,8 @@
    private IMarkMapper markMapper;
    @Autowired
    private INailMapper nailMapper;
    @Autowired
    private IZipMapper zipMapper;
    @Autowired
    private GetPatchesMapper patchesMapper;
    @Autowired
@@ -148,6 +151,32 @@
        }
    }
    public void saveZipFile(String workspaceId, FileUploadDTO file) throws IOException, ImageProcessingException {
        updateStatue(file.getName());
        boolean endsWith = file.getObjectKey().endsWith(".mp4");
        if (endsWith) {
            MediaFileZipEntity nailEntity = this.fileUploadConvertToZipEntity((file));
            nailEntity.setWorkspaceId(workspaceId);
            nailEntity.setFileId(UUID.randomUUID().toString());
            zipMapper.insert(nailEntity);
        } 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("~")) {
            String name = TimerUtil.getDkbh(filename);
@@ -187,7 +216,7 @@
                }
            }
        } else {
           String names = "nail" + filename;
            String names = "nail" + filename;
            List<MediaFileNailEntity> entities = nailMapper.selectList(new LambdaQueryWrapper<MediaFileNailEntity>()
                    .eq(MediaFileNailEntity::getFileName, names));
            if (entities != null && !entities.isEmpty()) {
@@ -423,11 +452,11 @@
    public ResponseResult downloadImages(List<String> jobIds) {
        try {
            String bucketPath = "/data/software/minio-data/cloud-bucket/";
            // List<String> prefixes = getUniqueFilePaths(jobIds);
            List<String> prefixes = getUniqueFilePaths(jobIds);
            MinioFileDownloader downloader = new MinioFileDownloader(bucketPath);
            // 下载并压缩文件到本地目录
            String localSaveDir = "/data/software/minio-data/zip-bucket/"; // 修改为你想保存的本地目录
            downloader.downloadAndZipFolders(jobIds, localSaveDir);
            downloader.downloadAndZipFolders(prefixes, localSaveDir);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("下载失败" + e.getMessage());
@@ -531,6 +560,32 @@
    private MediaFileNailEntity fileUploadConvertToNailEntity(FileUploadDTO file) {
        MediaFileNailEntity.MediaFileNailEntityBuilder builder = MediaFileNailEntity.builder();
        if (file != null) {
            builder.fileName(file.getName())
                    .fingerprint(file.getFingerprint())
                    .objectKey(file.getObjectKey())
                    .subFileType(file.getSubFileType())
                    .isOriginal(file.getExt().getIsOriginal())
                    .filePath(file.getPath())
                    .jobId(file.getExt().getFlightId())
                    .drone(file.getExt().getSn()).metadata(file.getMetadata())
                    .tinnyFingerprint(file.getExt().getTinnyFingerprint());
            // domain-type-subType
            int[] payloadModel = Arrays.stream(file.getExt().getPayloadModelKey().split("-"))
                    .map(Integer::valueOf)
                    .mapToInt(Integer::intValue)
                    .toArray();
            Optional<DeviceDictionaryDTO> payloadDict = deviceDictionaryService
                    .getOneDictionaryInfoByTypeSubType(DeviceDomainEnum.PAYLOAD.getVal(), payloadModel[1], payloadModel[2]);
            payloadDict.ifPresent(payload -> builder.payload(payload.getDeviceName()));
        }
        return builder.build();
    }
    private MediaFileZipEntity fileUploadConvertToZipEntity(FileUploadDTO file) {
        MediaFileZipEntity.MediaFileZipEntityBuilder builder = MediaFileZipEntity.builder();
        if (file != null) {
            builder.fileName(file.getName())
@@ -706,9 +761,96 @@
        }
    }
    public void updateMediaZipFileNames(String jobId) {
        // 查询符合条件的数据
        List<MediaFileZipEntity> mediaFiles = zipMapper.selectList(new LambdaQueryWrapper<MediaFileZipEntity>()
                .eq(MediaFileZipEntity::getJobId, jobId));
        boolean allContainTilde = mediaFiles.stream().allMatch(file -> file.getFileName().contains("~"));
        boolean noneContainTilde = mediaFiles.stream().noneMatch(file -> file.getFileName().contains("~"));
        // 如果所有 fileName 都包含 '~' 或者都不包含 '~',将 is_add 字段改为 1
        if (allContainTilde || noneContainTilde) {
            return;
        }
        // 筛选出name字段不包含'~'的数据
        List<MediaFileZipEntity> filteredFiles = mediaFiles.stream()
                .filter(file -> !file.getFileName().contains("~"))
                .collect(Collectors.toList());
        for (MediaFileZipEntity currentFile : filteredFiles) {
            String currentName = currentFile.getFileName();
            Map<String, Object> currentMetadata = JSON.parseObject(JSON.toJSONString(currentFile.getMetadata()), Map.class);
            Long currentCreatedTime = (Long) currentMetadata.get("createdTime");
            // 找到metadata中的createdTime小于当前数据的createdTime且最接近的那条数据
            Optional<MediaFileZipEntity> closestFileOpt = mediaFiles.stream()
                    .filter(file -> {
                        Map<String, Object> metadata = JSON.parseObject(JSON.toJSONString(file.getMetadata()), Map.class);
                        Long createdTime = (Long) metadata.get("createdTime");
                        String filename = file.getFileName();
                        return createdTime < currentCreatedTime && filename.contains("~");
                    })
                    .min((file1, file2) -> {
                        Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class);
                        Map<String, Object> metadata2 = JSON.parseObject(JSON.toJSONString(file2.getMetadata()), Map.class);
                        Long time1 = (Long) metadata1.get("createdTime");
                        Long time2 = (Long) metadata2.get("createdTime");
                        return Long.compare(currentCreatedTime - time1, currentCreatedTime - time2);
                    });
            if (closestFileOpt.isEmpty()) {
                // 找不到小于的文件,尝试找大于且最接近的文件
                closestFileOpt = mediaFiles.stream()
                        .filter(file -> {
                            Map<String, Object> metadata = JSON.parseObject(JSON.toJSONString(file.getMetadata()), Map.class);
                            Long createdTime = (Long) metadata.get("createdTime");
                            String filename = file.getFileName();
                            return createdTime > currentCreatedTime && filename.contains("~");
                        })
                        .min((file1, file2) -> {
                            Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class);
                            Map<String, Object> metadata2 = JSON.parseObject(JSON.toJSONString(file2.getMetadata()), Map.class);
                            Long time1 = (Long) metadata1.get("createdTime");
                            Long time2 = (Long) metadata2.get("createdTime");
                            return Long.compare(time1 - currentCreatedTime, time2 - currentCreatedTime);
                        });
            }
            // 提取并替换name字段
            closestFileOpt.ifPresent(closestFile -> {
                String closestName = closestFile.getFileName();
                int startIndex = closestName.indexOf("V");
                if (startIndex == -1) startIndex = closestName.indexOf("W");
                if (startIndex == -1) startIndex = closestName.indexOf("Z");
                if (startIndex == -1) startIndex = closestName.indexOf("T");
                if (startIndex == -1) {
                    return;
                }
                int endIndex = closestName.indexOf(".", startIndex);
                if (endIndex == -1) {
                    return;
                }
                String replacement = closestName.substring(startIndex, endIndex);
                int currentStartIndex = currentName.indexOf("V");
                if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("W");
                if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("Z");
                if (currentStartIndex == -1) currentStartIndex = currentName.indexOf("T");
                if (currentStartIndex == -1) {
                    return;
                }
                int currentEndIndex = currentName.indexOf(".", currentStartIndex);
                if (currentEndIndex == -1) {
                    return;
                }
                String newName = currentName.substring(0, currentStartIndex)
                        + replacement
                        + currentName.substring(currentEndIndex);
                currentFile.setFileName(newName);
                updateZipById(currentFile.getId(), currentFile);
            });
        }
    }
    @Override
    public void updateMarkMediaFileNames(String jobId) {
    public void updateNailMediaFileNames(String jobId) {
        try {
            // 查询符合条件的数据
            List<MediaFileNailEntity> mediaFiles = nailMapper.selectList(new LambdaQueryWrapper<MediaFileNailEntity>()
@@ -916,6 +1058,13 @@
        nailMapper.update(entity, updateWrapper);
    }
    public void updateZipById(Integer id, MediaFileZipEntity entity) {
        UpdateWrapper<MediaFileZipEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        zipMapper.update(entity, updateWrapper);
    }
    public void updateMediaById(Integer id, MediaFileEntity entity) {
        UpdateWrapper<MediaFileEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
@@ -923,10 +1072,12 @@
    }
    public void getNoaddFile() {
        List<MediaFileNailEntity> markEntities = nailMapper.selectList(new LambdaQueryWrapper<MediaFileNailEntity>().eq(MediaFileNailEntity::getIsadd, 0));
        List<MediaFileNailEntity> markEntities = nailMapper.selectList(
                new LambdaQueryWrapper<MediaFileNailEntity>().eq(MediaFileNailEntity::getIsadd, 0));
        for (MediaFileNailEntity mark : markEntities) {
            updateMarkMediaFileNames(mark.getJobId());
            updateNailMediaFileNames(mark.getJobId());
            updateMediaFileNames(mark.getJobId());
            updateMediaZipFileNames(mark.getJobId());
        }
    }