| | |
| | | import com.dji.sample.territory.service.impl.TbFjServiceImpl; |
| | | import com.dji.sample.territory.utils.ImgZipUtil; |
| | | import com.dji.sample.territory.utils.WaterMarkUtil; |
| | | import com.dji.sample.wayline.dao.IWaylineFileMapper; |
| | | import com.dji.sample.wayline.dao.IWaylineJobMapper; |
| | | import com.dji.sample.wayline.model.entity.WaylineFileEntity; |
| | | import com.dji.sample.wayline.model.entity.WaylineJobEntity; |
| | | import com.dji.sample.wayline.service.IWaylineFileService; |
| | | import com.dji.sample.wayline.service.IWaylineJobService; |
| | | import com.drew.imaging.ImageProcessingException; |
| | |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.dji.sample.media.util.ImageDownloaderAndCompressor.*; |
| | | import static com.dji.sample.patches.utils.TimerUtil.getDkbh; |
| | | |
| | | /** |
| | | * @author sean |
| | |
| | | private MinioPojo pojo; |
| | | |
| | | private ObjectMapper objectMapper = new ObjectMapper(); |
| | | @Autowired |
| | | private IWaylineJobMapper jobMapper; |
| | | @Autowired |
| | | private IWaylineFileMapper fileMapper; |
| | | |
| | | |
| | | private Optional<MediaFileEntity> getMediaByFingerprint(String workspaceId, String fingerprint) { |
| | |
| | | return Optional.ofNullable(fileEntity); |
| | | } |
| | | |
| | | public void saveMarkFile(String workspaceId, FileUploadDTO files, File file1,String dkbh) { |
| | | try { |
| | | MediaFileMarkEntity file=fileUploadConvertToMarkEntity(files); |
| | | 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()); |
| | | long timestamp = convertToTimestamp(files.getMetadata().getCreatedTime()); |
| | | int type=getLotInfo(dkbh); |
| | | File markFile = new File(WaterMarkUtil.addWatermark(file1, timestamp, files.getMetadata().getShootPosition().getLat(), |
| | | files.getMetadata().getShootPosition().getLng(), files.getMetadata().getGimbalYawDegree(),type).toURI()); |
| | | uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", mediaFileMarkEntity.getObjectKey(), markFile, "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()); |
| | | int isPlane=checkWaylineType(file.getExt().getFlightId()); |
| | | //避免对视频处理引发报错 |
| | | if (file.getObjectKey().endsWith("jpeg")&&isPlane!=2) { |
| | | 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()); |
| | | saveFiles(workspaceId, file, downloadedFile); |
| | | if (isPlane==1){ |
| | | fileEntity.setDkbh(getDkbh(fileEntity.getFileName())); |
| | | saveMarkFile(workspaceId, file, downloadedFile, fileEntity.getDkbh()); |
| | | } |
| | | } |
| | | return mapper.insert(fileEntity); |
| | | } |
| | | public void saveFiles(String workspaceId, FileUploadDTO file) { |
| | | |
| | | public void saveFiles(String workspaceId, FileUploadDTO file, File mediaFile) { |
| | | // 更新文件状态 |
| | | updateStatue(file.getName()); |
| | | // updateStatue(file.getName()); |
| | | |
| | | // 使用 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(); |
| | | // 处理异步任务的中断和异常 |
| | | // 处理图片文件 |
| | | File nailFile = new File(ImgZipUtil.compressImage(mediaFile, 50).toURI()); |
| | | MediaFileNailEntity nailEntity = this.fileUploadConvertToNailEntity(file); |
| | | Object data = ImgUtil.getInfo(mediaFile); |
| | | // 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", nailName, nailFile, "image/jpeg"); |
| | | nailMapper.insert(nailEntity); |
| | | } catch (IOException | ImageProcessingException e) { |
| | | // 处理异常 |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public void updateStatue(String filename) { |
| | | if (filename.contains("~")) { |
| | | String name = TimerUtil.getDkbh(filename); |
| | | String name = getDkbh(filename); |
| | | List<LotInfo> lotInfos = patchesMapper.selectList(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getDkbh, name)); |
| | | if (!lotInfos.isEmpty()) { |
| | | LambdaUpdateWrapper<LotInfo> updateWrapper = new LambdaUpdateWrapper<>(); |
| | |
| | | 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 && !entity.isEmpty()) { |
| | | for (MediaFileMarkEntity markEntity : entity) { |
| | | if (markEntity.getDronedata() != null) { |
| | | return markEntity.getDronedata(); |
| | | } |
| | | } |
| | | } else { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PaginationData<MediaFileEntity> mediaQuery(Integer page, Integer pageSize, Long updateStart, Long updateEnd, Long photoStart, Long photoEnd, String jobName, String workspaceId, String type,String jobId) { |
| | | public PaginationData<MediaFileEntity> mediaQuery(Integer page, Integer pageSize, Long updateStart, Long updateEnd, Long photoStart, Long photoEnd, String jobName, String workspaceId, String type, String jobId) { |
| | | // 创建查询条件对象 |
| | | LambdaQueryWrapper<MediaFileEntity> queryWrapper = new LambdaQueryWrapper<>(); |
| | | |
| | |
| | | queryWrapper.in(MediaFileEntity::getJobId, jobIds); |
| | | } |
| | | if (jobId != null && !jobId.isEmpty()) { |
| | | queryWrapper.eq(MediaFileEntity::getJobId,jobId); |
| | | queryWrapper.eq(MediaFileEntity::getJobId, jobId); |
| | | } |
| | | |
| | | if (type != null && !type.isEmpty()) { |
| | |
| | | @Override |
| | | public ResponseResult downloadImages(List<String> jobIds) { |
| | | String localSaveDir = "/home/drone/web/zip"; |
| | | String downPath="https://wrj.shuixiongit.com/downloadZip/"; |
| | | String filepath=TimerUtil.getNowDay()+".zip"; |
| | | String downPath = "https://wrj.shuixiongit.com/downloadZip/"; |
| | | String filepath = TimerUtil.getNowDay() + ".zip"; |
| | | try { |
| | | String bucketPath = "/data/software/minio-data/cloud-bucket"; |
| | | List<String> prefixes = getUniqueFilePaths(jobIds); |
| | | MinioFileDownloader downloader = new MinioFileDownloader(bucketPath); |
| | | downloader.downloadAndZipFolders(prefixes, localSaveDir,filepath); |
| | | downloader.downloadAndZipFolders(prefixes, localSaveDir, filepath); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return ResponseResult.error("下载失败" + e.getMessage()); |
| | | } |
| | | return ResponseResult.success(downPath+filepath); |
| | | return ResponseResult.success(downPath + filepath); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return builder.build(); |
| | | } |
| | | |
| | | private MediaFileZipEntity fileUploadConvertToZipEntity(FileUploadDTO file) { |
| | | MediaFileZipEntity.MediaFileZipEntityBuilder builder = MediaFileZipEntity.builder(); |
| | | private MediaFileMarkEntity fileUploadConvertToMarkEntity(FileUploadDTO file) { |
| | | MediaFileMarkEntity.MediaFileMarkEntityBuilder builder = MediaFileMarkEntity.builder(); |
| | | |
| | | if (file != null) { |
| | | builder.fileName(file.getName()) |
| | |
| | | .eq(MediaFileEntity::getJobId, jobId)); |
| | | } |
| | | |
| | | public int getLotInfo(String dkbh){ |
| | | LotInfo lotInfo=patchesMapper.selectOne(new LambdaQueryWrapper<LotInfo>().eq(LotInfo::getDkbh, dkbh)); |
| | | return lotInfo.getType(); |
| | | } |
| | | public int checkWaylineType(String jobId) { |
| | | WaylineJobEntity jobEntity = jobMapper.selectOne(new LambdaQueryWrapper<WaylineJobEntity>().eq(WaylineJobEntity::getJobId, jobId)); |
| | | WaylineFileEntity fileEntity=fileMapper.selectOne(new LambdaQueryWrapper<WaylineFileEntity>().eq(WaylineFileEntity::getWaylineId, jobEntity.getFileId())); |
| | | return fileEntity.getWaylineType(); |
| | | } |
| | | |
| | | public void updateMediaFileNames(String jobId) { |
| | | // 查询符合条件的数据 |
| | | List<MediaFileEntity> mediaFiles = mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>() |
| | | .eq(MediaFileEntity::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) { |
| | | // 判断所有的dkbh字段是否都不为空或者都为空 |
| | | boolean allHaveDkbh = mediaFiles.stream().allMatch(file -> file.getDkbh() != null && !file.getDkbh().isEmpty()); |
| | | boolean noneHaveDkbh = mediaFiles.stream().noneMatch(file -> file.getDkbh() != null && !file.getDkbh().isEmpty()); |
| | | |
| | | // 如果所有dkbh字段都不为空或者都为空,将is_add字段改为1 |
| | | if (allHaveDkbh || noneHaveDkbh) { |
| | | mediaFiles.forEach(file -> { |
| | | file.setIsadd(1); |
| | | updateMediaById(file.getId(), file); |
| | | }); |
| | | return; |
| | | } |
| | | |
| | | // 筛选出name字段不包含'~'的数据 |
| | | // 筛选出dkbh字段为空的数据 |
| | | List<MediaFileEntity> filteredFiles = mediaFiles.stream() |
| | | .filter(file -> !file.getFileName().contains("~")) |
| | | .filter(file -> file.getDkbh() == null || file.getDkbh().isEmpty()) |
| | | .collect(Collectors.toList()); |
| | | |
| | | for (MediaFileEntity 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<MediaFileEntity> 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("~"); |
| | | return createdTime < currentCreatedTime && file.getDkbh() != null && !file.getDkbh().isEmpty(); |
| | | }) |
| | | .min((file1, file2) -> { |
| | | Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class); |
| | |
| | | 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("~"); |
| | | return createdTime > currentCreatedTime && file.getDkbh() != null && !file.getDkbh().isEmpty(); |
| | | }) |
| | | .min((file1, file2) -> { |
| | | Map<String, Object> metadata1 = JSON.parseObject(JSON.toJSONString(file1.getMetadata()), Map.class); |
| | |
| | | return Long.compare(time1 - currentCreatedTime, time2 - currentCreatedTime); |
| | | }); |
| | | } |
| | | // 提取并替换name字段 |
| | | |
| | | // 将最接近的文件的dkbh值赋给当前文件 |
| | | 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); |
| | | currentFile.setDkbh(closestFile.getDkbh()); |
| | | currentFile.setIsadd(1); |
| | | updateMediaById(currentFile.getId(), currentFile); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | |
| | | public void updateMediaZipFileNames(String jobId) { |
| | | // 查询符合条件的数据 |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public PaginationData<MediaFileEntity> getPhotoByJobId(int pageNum, int pageSize, String workspaceId, String jobId) { |
| | | // 分页查询 |
| | |
| | | |
| | | // 添加查询条件 |
| | | queryWrapper.eq(MediaFileEntity::getWorkspaceId, workspaceId); |
| | | queryWrapper.eq(MediaFileEntity::getJobId,jobId); |
| | | queryWrapper.eq(MediaFileEntity::getJobId, jobId); |
| | | queryWrapper.last("ORDER BY JSON_EXTRACT(metadata, '$.createdTime') DESC"); |
| | | Page<MediaFileEntity> resultPage = mapper.selectPage(page, queryWrapper); |
| | | List<MediaFileEntity> result = resultPage.getRecords(); |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public List<MediaFileEntity> getMedia(String jobId) { |
| | | return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>() |
| | | .eq(MediaFileEntity::getJobId, jobId) |
| | |
| | | } |
| | | |
| | | 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); |
| | |
| | | |
| | | |
| | | 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()); |
| | | } |
| | | } |
| | | |