fix xf oss read bug

This commit is contained in:
ran.shang
2025-08-15 14:17:10 +08:00
parent 8f44d802db
commit bff0f19500
2 changed files with 68 additions and 23 deletions

View File

@@ -1,10 +1,13 @@
package com.newyetai.component.file.util; package com.newyetai.component.file.util;
import com.newyetai.component.file.config.FileProperties;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
@@ -13,6 +16,7 @@ import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import com.newyetai.common.exception.BusinessException; import com.newyetai.common.exception.BusinessException;
@@ -23,7 +27,6 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import ws.schild.jave.Encoder; import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject; import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes; import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes; import ws.schild.jave.encode.EncodingAttributes;
@@ -34,12 +37,39 @@ import ws.schild.jave.encode.EncodingAttributes;
* @author on 2019-12-10 * @author on 2019-12-10
*/ */
@Slf4j @Slf4j
@Component
public class FileUtils { public class FileUtils {
public static final double IMG_QUALITY = 0.6d; public static final double IMG_QUALITY = 0.6d;
private FileUtils() {} private static FileProperties fileProperties;
FileUtils(FileProperties fileProperties) {
FileUtils.fileProperties = fileProperties;
}
public static String getUploadDir(){
return fileProperties.getUploadDir();
}
public static File createTempFile(String suffix) throws IOException {
File tempDir = new File(FileUtils.getUploadDir() + File.separator + "temp" + File.separator);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
File temp = File.createTempFile("tmp_", suffix, tempDir);
return temp;
}
public static FileInputStream copyToTempFile(InputStream in, File temp) throws IOException {
temp.deleteOnExit();
try (OutputStream out = new FileOutputStream(temp)) {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) != -1) out.write(buf, 0, len);
}
return new FileInputStream(temp);
}
/** /**
* 得到字节大小字符串 * 得到字节大小字符串

View File

@@ -33,18 +33,18 @@ public class XfUtils {
private static XfProperties xfProperties; private static XfProperties xfProperties;
private static FileService fileService; private static FileService fileService;
private static FileRecordService fileRecordService; private static FileRecordService fileRecordService;
XfUtils(ConfigService configService, XfProperties xfProperties, FileService fileService, FileRecordService fileRecordService){ XfUtils(ConfigService configService, XfProperties xfProperties, FileService fileService, FileRecordService fileRecordService){
XfUtils.configService = configService; XfUtils.configService = configService;
XfUtils.xfProperties = xfProperties; XfUtils.xfProperties = xfProperties;
XfUtils.fileService = fileService; XfUtils.fileService = fileService;
XfUtils.fileRecordService = fileRecordService; XfUtils.fileRecordService = fileRecordService;
} }
/** /**
* 生成面试问题 * 生成面试问题
* @return InterviewContentDTO json * @return InterviewContentDTO json
* @author sr on 2024-12-10 * @author sr on 2024-12-10
*/ */
public static String generateInterviewQuestion(InterviewContentDTO content) { public static String generateInterviewQuestion(InterviewContentDTO content) {
ChatReq req = ChatReq.builder() ChatReq req = ChatReq.builder()
@@ -58,11 +58,11 @@ public class XfUtils {
Assert.isTrue(result.getSuccess(), result.getMsg()); Assert.isTrue(result.getSuccess(), result.getMsg());
return result.getData(); return result.getData();
} }
/** /**
* 评分面试 * 评分面试
* @return InterviewContentDTO json * @return InterviewContentDTO json
* @author sr on 2024-12-10 * @author sr on 2024-12-10
*/ */
public static String gradeInterview(InterviewContentDTO content) { public static String gradeInterview(InterviewContentDTO content) {
ChatReq req = ChatReq.builder() ChatReq req = ChatReq.builder()
@@ -76,11 +76,11 @@ public class XfUtils {
Assert.isTrue(result.getSuccess(), result.getMsg()); Assert.isTrue(result.getSuccess(), result.getMsg());
return result.getData(); return result.getData();
} }
/** /**
* 生成简历 * 生成简历
* @return * @return
* @author sr on 2024-12-10 * @author sr on 2024-12-10
*/ */
public static String generateResume(String content) { public static String generateResume(String content) {
String key01 = configService.getConfigValue("ai_jianli_key01", CommonConstants.PLATFORM_ORG_ID, true); String key01 = configService.getConfigValue("ai_jianli_key01", CommonConstants.PLATFORM_ORG_ID, true);
@@ -98,28 +98,36 @@ public class XfUtils {
Assert.isTrue(result.getSuccess(), result.getMsg()); Assert.isTrue(result.getSuccess(), result.getMsg());
return result.getData(); return result.getData();
} }
/** /**
* 语音听写 * 语音听写
* @param fileCode * @param fileCode
* @return * @return
* @author sr on 2025-01-09 * @author sr on 2025-01-09
*/ */
public static String iat(String fileCode) { public static String iat(String fileCode) {
try (InputStream is = fileRecordService.getFile(fileCode)){ try (InputStream fileis = fileRecordService.getFile(fileCode)){
AlitaResult<String> result = WebIATWS.execute(is, xfProperties); File file = FileUtils.createTempFile("pcm");
Assert.isTrue(result.getSuccess(), result.getMsg()); try (InputStream is = FileUtils.copyToTempFile(fileis, file)) {
return result.getData(); AlitaResult<String> result = WebIATWS.execute(is, xfProperties);
Assert.isTrue(result.getSuccess(), result.getMsg());
return result.getData();
} catch (Exception e) {
throw e;
} finally {
file.deleteOnExit();
}
} catch (Exception e) { } catch (Exception e) {
throw new BusinessException("语音听写异常:" + e.getMessage()); throw new BusinessException("语音听写异常:" + e.getMessage());
} }
} }
/** /**
* 语音合成 * 语音合成
* @return * @return
* @author sr on 2025-01-09 * @author sr on 2025-01-09
*/ */
public static String tts(String text) { public static String tts(String text) {
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){ try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
@@ -258,11 +266,18 @@ public class XfUtils {
* @return * @return
*/ */
public static String rtasr(RtasrReqDTO reqDTO) { public static String rtasr(RtasrReqDTO reqDTO) {
try (InputStream is = fileRecordService.getFile(reqDTO.getFileCode())){ try (InputStream fileis = fileRecordService.getFile(reqDTO.getFileCode())){
AlitaResult<String> result = RtasrClientApp.execute(is, reqDTO.getFrom(), xfProperties); File file = FileUtils.createTempFile("pcm");
Assert.isTrue(result.getSuccess(), result.getMsg()); try (InputStream is = FileUtils.copyToTempFile(fileis, file)) {
return result.getData(); AlitaResult<String> result = RtasrClientApp.execute(is, reqDTO.getFrom(), xfProperties);
} catch (Exception e) { Assert.isTrue(result.getSuccess(), result.getMsg());
return result.getData();
} catch (Exception e) {
throw e;
} finally {
file.deleteOnExit();
}
}catch (Exception e) {
throw new BusinessException("语音听写异常:" + e.getMessage()); throw new BusinessException("语音听写异常:" + e.getMessage());
} }