rain
2024-08-14 ea73b6fb3ad0b34e4d856321afecae5ada1091fe
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.*;
@@ -115,72 +117,78 @@
    }
    @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.setIsadd(0);
        fileEntity.setFileId(UUID.randomUUID().toString());
        String url = pojo.getEndpoint() + "/" + pojo.getBucket() + file.getObjectKey();
        File downloadedFile = TbFjServiceImpl.downloadFile(url);
        Object data = ImgUtil.getInfo(downloadedFile);
        fileEntity.setDroneData(data);
        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("~")) {
@@ -292,6 +300,47 @@
                    mediaFile.setJobName(taskNameResult);
                })
                .filter(mediaFile -> uniqueFileMap.putIfAbsent(mediaFile.getFileName(), mediaFile) == null)
                .collect(Collectors.toList());
        // 计算分页信息
        int total = uniqueResults.size();
        int start = (page - 1) * pageSize;
        int end = Math.min(start + pageSize, total);
        // 获取当前页的结果
        List<MediaFileEntity> pageResults = uniqueResults.subList(start, end);
        // 创建临时的 Page 对象
        Page<MediaFileEntity> resultPage = new Page<>(page, pageSize);
        resultPage.setRecords(pageResults);
        resultPage.setTotal(total);
        // 返回分页数据
        return new PaginationData<>(pageResults, new Pagination(resultPage));
    }
    @Override
    public PaginationData<MediaFileEntity> mediaQuerys(Integer page, Integer pageSize, String workspaceId) {
        // 创建查询条件对象
        LambdaQueryWrapper<MediaFileEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(MediaFileEntity::getWorkspaceId, workspaceId);
        queryWrapper.last("ORDER BY JSON_EXTRACT(metadata, '$.createdTime') DESC");
        // 执行查询获取所有结果
        List<MediaFileEntity> allResults = mapper.selectList(queryWrapper);
        // 处理结果去重并设置任务名称
        Map<String, MediaFileEntity> uniqueFileMap = new LinkedHashMap<>();
        List<MediaFileEntity> uniqueResults = allResults.stream()
                .peek(mediaFile -> {
                    String taskNameResult = waylineJobService.getName(mediaFile.getJobId());
                    mediaFile.setJobName(taskNameResult);
                })
                .filter(mediaFile -> uniqueFileMap.putIfAbsent(mediaFile.getJobId(), mediaFile) == null)
                .collect(Collectors.toList());
        // 计算分页信息
@@ -666,6 +715,10 @@
        // 如果所有 fileName 都包含 '~' 或者都不包含 '~',将 is_add 字段改为 1
        if (allContainTilde || noneContainTilde) {
            mediaFiles.forEach(file -> {
                file.setIsadd(1);
                updateMediaById(file.getId(), file);
            });
            return;
        }
@@ -940,26 +993,43 @@
        // 分页查询
        Page<MediaFileEntity> page = new Page<>(pageNum, pageSize);
        // 使用 QueryWrapper 来执行 DISTINCT 查询,并且选择 payload 和 create_time 字段
        // 使用 QueryWrapper 执行查询
        QueryWrapper<MediaFileEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("DISTINCT job_id", "payload", "create_time")  // 选择 job_id, payload 和 create_time 字段
                .eq("workspace_id", workspaceId)
                .orderByDesc("create_time");
                .orderByDesc("create_time");  // 根据 create_time 降序排序
        // 执行分页查询
        Page<MediaFileEntity> resultPage = mapper.selectPage(page, queryWrapper);
        List<MediaFileEntity> result = resultPage.getRecords();
        // 根据 job_id 字段进行去重
        Map<String, MediaFileEntity> uniqueFilesMap = result.stream()
                .collect(Collectors.toMap(
                        MediaFileEntity::getJobId,
                        entity -> entity,
                        (existing, replacement) -> existing // 如果有重复的,保留已有的
                ));
        List<MediaFileEntity> uniqueFiles = new ArrayList<>(uniqueFilesMap.values());
        // 为每个 MediaFileEntity 设置 jobName
        for (MediaFileEntity mediaFile : result) {
        for (MediaFileEntity mediaFile : uniqueFiles) {
            String taskNameResult = waylineJobService.getName(mediaFile.getJobId());
            mediaFile.setJobName(taskNameResult);
        }
        // 使用分页对象构造 Pagination
        Pagination pagination = new Pagination(resultPage);
        return new PaginationData<>(result, pagination);
        Page<MediaFileEntity> newPage = new Page<>(pageNum, pageSize);
        newPage.setRecords(uniqueFiles);
        newPage.setTotal(resultPage.getTotal()); // 设置总记录数为原始查询的总记录数
        Pagination pagination = new Pagination(newPage);
        return new PaginationData<>(uniqueFiles, pagination);
    }
@@ -1015,7 +1085,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);
@@ -1029,18 +1099,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());
        }
    }