From bff0f1950073a49b3a091eb18cada7283797f846 Mon Sep 17 00:00:00 2001 From: "ran.shang" <> Date: Fri, 15 Aug 2025 14:17:10 +0800 Subject: [PATCH] fix xf oss read bug --- .../component/file/util/FileUtils.java | 34 ++++++++++- .../com/newyetai/sdk/xf/util/XfUtils.java | 57 ++++++++++++------- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/newyetai/component/file/util/FileUtils.java b/src/main/java/com/newyetai/component/file/util/FileUtils.java index 25eff80..a275f4f 100644 --- a/src/main/java/com/newyetai/component/file/util/FileUtils.java +++ b/src/main/java/com/newyetai/component/file/util/FileUtils.java @@ -1,10 +1,13 @@ package com.newyetai.component.file.util; +import com.newyetai.component.file.config.FileProperties; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Base64; import java.util.List; @@ -13,6 +16,7 @@ import java.util.zip.ZipInputStream; import javax.imageio.ImageIO; +import org.springframework.stereotype.Component; import org.springframework.util.FileCopyUtils; import com.newyetai.common.exception.BusinessException; @@ -23,7 +27,6 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IORuntimeException; import lombok.extern.slf4j.Slf4j; import ws.schild.jave.Encoder; -import ws.schild.jave.EncoderException; import ws.schild.jave.MultimediaObject; import ws.schild.jave.encode.AudioAttributes; import ws.schild.jave.encode.EncodingAttributes; @@ -34,12 +37,39 @@ import ws.schild.jave.encode.EncodingAttributes; * @author on 2019-12-10 */ @Slf4j +@Component public class FileUtils { 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); + } /** * 得到字节大小字符串 diff --git a/src/main/java/com/newyetai/sdk/xf/util/XfUtils.java b/src/main/java/com/newyetai/sdk/xf/util/XfUtils.java index 75f3360..0ad7a2d 100644 --- a/src/main/java/com/newyetai/sdk/xf/util/XfUtils.java +++ b/src/main/java/com/newyetai/sdk/xf/util/XfUtils.java @@ -33,18 +33,18 @@ public class XfUtils { private static XfProperties xfProperties; private static FileService fileService; private static FileRecordService fileRecordService; - + XfUtils(ConfigService configService, XfProperties xfProperties, FileService fileService, FileRecordService fileRecordService){ XfUtils.configService = configService; XfUtils.xfProperties = xfProperties; XfUtils.fileService = fileService; XfUtils.fileRecordService = fileRecordService; } - + /** * 生成面试问题 * @return InterviewContentDTO json - * @author sr on 2024-12-10 + * @author sr on 2024-12-10 */ public static String generateInterviewQuestion(InterviewContentDTO content) { ChatReq req = ChatReq.builder() @@ -58,11 +58,11 @@ public class XfUtils { Assert.isTrue(result.getSuccess(), result.getMsg()); return result.getData(); } - + /** * 评分面试 * @return InterviewContentDTO json - * @author sr on 2024-12-10 + * @author sr on 2024-12-10 */ public static String gradeInterview(InterviewContentDTO content) { ChatReq req = ChatReq.builder() @@ -76,11 +76,11 @@ public class XfUtils { Assert.isTrue(result.getSuccess(), result.getMsg()); return result.getData(); } - + /** * 生成简历 * @return - * @author sr on 2024-12-10 + * @author sr on 2024-12-10 */ public static String generateResume(String content) { 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()); return result.getData(); } - + /** * 语音听写 * @param fileCode * @return - * @author sr on 2025-01-09 + * @author sr on 2025-01-09 */ public static String iat(String fileCode) { - try (InputStream is = fileRecordService.getFile(fileCode)){ - AlitaResult result = WebIATWS.execute(is, xfProperties); - Assert.isTrue(result.getSuccess(), result.getMsg()); - return result.getData(); + try (InputStream fileis = fileRecordService.getFile(fileCode)){ + File file = FileUtils.createTempFile("pcm"); + try (InputStream is = FileUtils.copyToTempFile(fileis, file)) { + AlitaResult 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) { throw new BusinessException("语音听写异常:" + e.getMessage()); } - } - + + + /** * 语音合成 * @return - * @author sr on 2025-01-09 + * @author sr on 2025-01-09 */ public static String tts(String text) { try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){ @@ -258,11 +266,18 @@ public class XfUtils { * @return */ public static String rtasr(RtasrReqDTO reqDTO) { - try (InputStream is = fileRecordService.getFile(reqDTO.getFileCode())){ - AlitaResult result = RtasrClientApp.execute(is, reqDTO.getFrom(), xfProperties); - Assert.isTrue(result.getSuccess(), result.getMsg()); - return result.getData(); - } catch (Exception e) { + try (InputStream fileis = fileRecordService.getFile(reqDTO.getFileCode())){ + File file = FileUtils.createTempFile("pcm"); + try (InputStream is = FileUtils.copyToTempFile(fileis, file)) { + AlitaResult result = RtasrClientApp.execute(is, reqDTO.getFrom(), xfProperties); + 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()); }