|
@@ -2,15 +2,32 @@ package com.example.unusualsounds.project.vox.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.example.unusualsounds.common.utils.FileToMultipartFile;
|
|
|
import com.example.unusualsounds.common.utils.RedisUtils;
|
|
|
import com.example.unusualsounds.common.utils.UUID;
|
|
|
+import com.example.unusualsounds.framework.minio.MinioUtil;
|
|
|
import com.example.unusualsounds.project.vox.entity.Alarms;
|
|
|
import com.example.unusualsounds.project.vox.service.AlarmsService;
|
|
|
import com.example.unusualsounds.project.vox.mapper.AlarmsMapper;
|
|
|
+import io.minio.GetObjectArgs;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.errors.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.file.FileAlreadyExistsException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
|
|
@@ -20,6 +37,7 @@ import java.util.HashMap;
|
|
|
* @createDate 2025-02-18 14:45:58
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class AlarmsServiceImpl extends ServiceImpl<AlarmsMapper, Alarms>
|
|
|
implements AlarmsService{
|
|
|
|
|
@@ -30,12 +48,37 @@ public class AlarmsServiceImpl extends ServiceImpl<AlarmsMapper, Alarms>
|
|
|
@Autowired
|
|
|
private EdgeOrganizationServiceImpl edgeOrganizationServiceImpl;
|
|
|
|
|
|
- @Value("${Alarms.imageUrl}")
|
|
|
+ @Value(" ${CENTER_CODE}")
|
|
|
+ private String centerCode;
|
|
|
+
|
|
|
+ @Value(" ${Alarms.imageUrl}")
|
|
|
private String imageUrl;
|
|
|
|
|
|
- @Value("${Alarms.thumbnailUrl}")
|
|
|
- private String thumbnailUrl;
|
|
|
+ @Value(" ${EDGE_CODE}")
|
|
|
+ private String edgeCode;
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${Center-minio.bucket}")
|
|
|
+ private String centerMinioBucket;
|
|
|
+
|
|
|
+ @Value("${Center-minio.imagePath}")
|
|
|
+ private String centerMinioImagePath;
|
|
|
+
|
|
|
+ @Value("${Alarms.dir}")
|
|
|
+ private String alarmsDir;
|
|
|
+
|
|
|
+ //边端图片封面
|
|
|
+ @Value("${minio.source-dir}")
|
|
|
+ private String alarmsUrl;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MinioUtil minioUtil;
|
|
|
+
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ public AlarmsServiceImpl(@Qualifier("minioClient2") MinioClient minioClient){
|
|
|
+ this.minioClient = minioClient;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -67,15 +110,95 @@ public class AlarmsServiceImpl extends ServiceImpl<AlarmsMapper, Alarms>
|
|
|
alarms.setUuid(alarmUUID);
|
|
|
alarms.setCreatedAt(new Date());
|
|
|
alarms.setUpdatedAt(new Date());
|
|
|
- alarms.setImageUrl(imageUrl);
|
|
|
- alarms.setThumbnailUrl(thumbnailUrl);
|
|
|
|
|
|
+// alarms.setImageUrl(imageUrl);
|
|
|
+// alarms.setThumbnailUrl(imageUrl);
|
|
|
+
|
|
|
+ // todo 每次上传文件都从中心端minio进行下载上传的方式
|
|
|
+ if(!StringUtils.equals(centerCode.trim(),edgeCode.trim())){
|
|
|
+ //边缘端
|
|
|
+ String string = this.downLoadCenterImg();
|
|
|
+ if(!"error".equals(string)){
|
|
|
+ this.upAlarmsImg(string);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ alarms.setImageUrl(imageUrl.trim());
|
|
|
+ alarms.setThumbnailUrl(imageUrl.trim());
|
|
|
|
|
|
boolean save = this.save(alarms);
|
|
|
boolean reportSave = alarmReportsService.saveAlarmReport(alarms);
|
|
|
return reportSave;
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void testAlarms() {
|
|
|
+ String string = this.downLoadCenterImg();
|
|
|
+ System.out.println("下载的文件地址:"+string);
|
|
|
+ String s = this.upAlarmsImg(string);
|
|
|
+ System.out.println("上传的文件地址:"+s);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传最新的封面图片
|
|
|
+ * 然后返回s3开头的url
|
|
|
+ * @param alarmsImgDir
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String upAlarmsImg(String alarmsImgDir){
|
|
|
+ File file = new File(alarmsImgDir);
|
|
|
+ FileToMultipartFile fileToMultipartFile = new FileToMultipartFile(file);
|
|
|
+ String path = alarmsUrl+"print/";
|
|
|
+ String url = minioUtil.uploadImageUrl(fileToMultipartFile, path,"image/jpeg");
|
|
|
+
|
|
|
+ String tmpStr = "s3://";
|
|
|
+ String replaced = url.replaceFirst("https?://[^/]+", "s3:/");
|
|
|
+ return replaced;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从中心端下载固定路径下的封面图片
|
|
|
+ * ,然后上传到当前边端站,每次预警都重新下载并上传
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String downLoadCenterImg() {
|
|
|
+ try {
|
|
|
+ // 使用minioClient2
|
|
|
+ String bucketName = centerMinioBucket;
|
|
|
+ String objectName = centerMinioImagePath;
|
|
|
+
|
|
|
+ Path destPath = Paths.get(alarmsDir);
|
|
|
+ // 如果文件已存在,则删除它
|
|
|
+ try {
|
|
|
+ if (Files.exists(destPath)) {
|
|
|
+ Files.delete(destPath); // 删除已有文件
|
|
|
+ log.warn("文件存在,删除{}", alarmsDir);
|
|
|
+ }
|
|
|
+ }catch (FileAlreadyExistsException e){
|
|
|
+ log.error("文件删除失败:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ // 获取对象输入流
|
|
|
+ InputStream stream = minioClient.getObject(
|
|
|
+ GetObjectArgs.builder()
|
|
|
+ .bucket(bucketName)
|
|
|
+ .object(objectName)
|
|
|
+ .build());
|
|
|
+
|
|
|
+ // 将输入流写入到文件
|
|
|
+ Files.copy(stream, Paths.get(alarmsDir));
|
|
|
+ log.info("当前minio:{}",minioClient);
|
|
|
+ log.info("文件下载成功");
|
|
|
+ return alarmsDir;
|
|
|
+ } catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException |
|
|
|
+ InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException |
|
|
|
+ IllegalArgumentException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|