hby100j功能语音管理

This commit is contained in:
2026-02-03 16:29:46 +08:00
parent ee445dc0d2
commit 696bbb4aa4
11 changed files with 506 additions and 6 deletions

View File

@ -1,9 +1,14 @@
package com.fuyuanshen.app.service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fuyuanshen.app.domain.AppBusinessFile;
import com.fuyuanshen.app.domain.bo.AppBusinessFileBo;
import com.fuyuanshen.app.domain.dto.AppAudioFileDto;
import com.fuyuanshen.app.domain.dto.AppFileRenameDto;
import com.fuyuanshen.app.domain.vo.AppFileVo;
import com.fuyuanshen.app.http.HttpTtsClient;
import com.fuyuanshen.app.mapper.AppBusinessFileMapper;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
import com.fuyuanshen.equipment.utils.AlibabaTTSUtil;
import com.fuyuanshen.equipment.utils.AudioProcessUtil;
@ -55,6 +60,7 @@ public class AudioProcessService {
private final FileHashUtil fileHashUtil;
private final ISysOssService ossService;
private final IAppBusinessFileService appBusinessFileService;
private final AppBusinessFileMapper appBusinessFileMapper;
/**
* 处理上传的音频文件
@ -192,6 +198,28 @@ public class AudioProcessService {
return file.getAbsolutePath();
}
private String saveByteArrayToFile(InputStream inputStream, String filename) throws IOException {
// 确定保存路径(可以是临时目录或指定目录)
String directory = System.getProperty("java.io.tmpdir"); // 使用系统临时目录
File dir = new File(directory);
if (!dir.exists()) {
dir.mkdirs();
}
// 创建完整文件路径
File file = new File(dir, filename);
// 从输入流读取数据并写入文件
try (FileOutputStream fos = new FileOutputStream(file);
InputStream is = inputStream) {
byte[] buffer = new byte[8192]; // 8KB缓冲区
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
return file.getAbsolutePath();
}
/**
@ -328,7 +356,8 @@ public class AudioProcessService {
validateAudioFileForRestful(file);
// 上传文件
// SysOssVo upload = sysOssService.upload(file);
String filename = file.getOriginalFilename();
String savedPath = null;
try {
String fileHash = fileHashUtil.hash(file);
SysOssVo upload = ossService.updateHash(file, fileHash);
@ -336,19 +365,30 @@ public class AudioProcessService {
if (upload.getUrl() != null && upload.getUrl().startsWith("http://")) {
upload.setUrl(upload.getUrl().replaceFirst("^http://", "https://"));
}
String fileSuffix = filename.substring(filename.lastIndexOf('.')).toLowerCase();
AppBusinessFileBo appBusinessFileBo = new AppBusinessFileBo();
appBusinessFileBo.setFileId(upload.getOssId());
appBusinessFileBo.setBusinessId(bo.getDeviceId());
appBusinessFileBo.setFileType(3L);
appBusinessFileBo.setCreateBy(AppLoginHelper.getUserId());
savedPath = saveByteArrayToFile(file.getInputStream(), generateRandomFileName(fileSuffix));
if (savedPath != null) {
log.info("MP3文件已保存: {}", savedPath);
Integer mp3Duration = Mp3Duration.getMp3Duration(savedPath);
log.info("MP3文件时长: {} 秒", mp3Duration);
appBusinessFileBo.setDuration(mp3Duration);
}
appBusinessFileService.insertByBo(appBusinessFileBo);
if (upload != null) {
return upload.getUrl();
}
} catch (Exception e){
log.error("上传音频文件失败", e);
}finally {
log.info("删除临时文件: {}", savedPath);
if(savedPath != null){
deleteTempFile(new File(savedPath));
}
}
return null;
@ -366,9 +406,13 @@ public class AudioProcessService {
if (originalFilename == null) {
throw new IllegalArgumentException("文件名不能为空");
}
List<String> SUPPORTED_FORMATS = Arrays.asList(
".wav", ".mp3", ".pcm"
);
String ext = originalFilename.substring(originalFilename.lastIndexOf('.')).toLowerCase();
// 检查文件扩展名
if (!isSupportedFormat(originalFilename)) {
if (!SUPPORTED_FORMATS.contains(ext)) {
throw new IllegalArgumentException("只允许上传MP3、WAV、PCM格式的音频文件");
}
@ -476,4 +520,21 @@ public class AudioProcessService {
List<AppFileVo> appFileVos = appBusinessFileService.queryAppFileList(bo);
return appFileVos;
}
public R<Void> deleteAudioFile(Long fileId,Long deviceId) {
UpdateWrapper<AppBusinessFile> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("file_id",fileId);
updateWrapper.eq("business_id",deviceId);
appBusinessFileMapper.delete(updateWrapper);
return R.ok();
}
public R<Void> renameAudioFile(AppFileRenameDto bo) {
UpdateWrapper<AppBusinessFile> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("file_id",bo.getFileId());
updateWrapper.eq("business_id",bo.getDeviceId());
updateWrapper.set("re_name",bo.getFileName());
appBusinessFileMapper.update(updateWrapper);
return R.ok();
}
}