调度定时把文件上传到minio , 播放基于minio
This commit is contained in:
@@ -1,13 +1,23 @@
|
||||
package com.rj.scheduler;
|
||||
|
||||
import com.rj.config.AppConfig;
|
||||
import com.rj.entity.AudioManagementSegments;
|
||||
import com.rj.service.IAudioManagementSegmentsService;
|
||||
import com.rj.service.IAudioManagementStatisticsService;
|
||||
import com.rj.service.MinIOService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 门店录音统计定时任务
|
||||
@@ -25,6 +35,67 @@ public class AudioStatisticsScheduler {
|
||||
@Autowired
|
||||
private AppConfig appConfig;
|
||||
|
||||
@Autowired
|
||||
private IAudioManagementSegmentsService audioManagementSegmentsService;
|
||||
|
||||
@Autowired
|
||||
private MinIOService minIOService;
|
||||
|
||||
/**
|
||||
* 每 5 秒:将仅有本地路径、尚未写入 MinIO URL 的录音分段补传到 Minio 并回写 audio_file_url。
|
||||
*/
|
||||
@Scheduled(fixedRate = 50000)
|
||||
public void uploadLocalSegmentFilesToMinio() {
|
||||
if (!appConfig.getScheduler().isStart()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
List<AudioManagementSegments> pending = audioManagementSegmentsService.listSegmentsNeedingMinioUpload(50);
|
||||
if (pending.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (AudioManagementSegments segment : pending) {
|
||||
uploadOneSegmentFileToMinio(segment);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("录音分段补传 MinIO 定时任务执行失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void uploadOneSegmentFileToMinio(AudioManagementSegments segment) {
|
||||
String pathStr = segment.getAudioFilePath();
|
||||
Path path = Paths.get(pathStr);
|
||||
if (!Files.isRegularFile(path)) {
|
||||
log.warn("分段本地文件不存在,跳过: id={}, path={}", segment.getId(), pathStr);
|
||||
return;
|
||||
}
|
||||
String originalName = segment.getAudioFileOriginalName();
|
||||
if (!StringUtils.hasText(originalName)) {
|
||||
originalName = path.getFileName().toString();
|
||||
}
|
||||
String objectName = UUID.randomUUID().toString().replace("-", "") + originalName;
|
||||
String contentType;
|
||||
try {
|
||||
contentType = Files.probeContentType(path);
|
||||
} catch (Exception e) {
|
||||
contentType = null;
|
||||
}
|
||||
if (!StringUtils.hasText(contentType)) {
|
||||
contentType = "application/octet-stream";
|
||||
}
|
||||
try (FileInputStream in = new FileInputStream(path.toFile())) {
|
||||
String minioUrl = minIOService.uploadFile(in, objectName, contentType);
|
||||
boolean ok = audioManagementSegmentsService.updateAudioFileUrlByIdIgnoreTenant(segment.getId(), minioUrl);
|
||||
if (!ok) {
|
||||
log.warn("补传成功但更新 audio_file_url 未生效: id={}", segment.getId());
|
||||
} else {
|
||||
log.info("分段已补传 MinIO: id={}", segment.getId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("分段补传 MinIO 失败: id={}, path={}", segment.getId(), pathStr, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 每天凌晨2点执行门店录音统计
|
||||
* 统计昨天的录音数据
|
||||
@@ -65,6 +136,7 @@ public class AudioStatisticsScheduler {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user