fix xf oss read bug
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到字节大小字符串
|
||||
|
||||
@@ -106,16 +106,24 @@ public class XfUtils {
|
||||
* @author sr on 2025-01-09
|
||||
*/
|
||||
public static String iat(String fileCode) {
|
||||
try (InputStream is = fileRecordService.getFile(fileCode)){
|
||||
try (InputStream fileis = fileRecordService.getFile(fileCode)){
|
||||
File file = FileUtils.createTempFile("pcm");
|
||||
try (InputStream is = FileUtils.copyToTempFile(fileis, file)) {
|
||||
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) {
|
||||
throw new BusinessException("语音听写异常:" + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 语音合成
|
||||
* @return
|
||||
@@ -258,11 +266,18 @@ public class XfUtils {
|
||||
* @return
|
||||
*/
|
||||
public static String rtasr(RtasrReqDTO reqDTO) {
|
||||
try (InputStream is = fileRecordService.getFile(reqDTO.getFileCode())){
|
||||
try (InputStream fileis = fileRecordService.getFile(reqDTO.getFileCode())){
|
||||
File file = FileUtils.createTempFile("pcm");
|
||||
try (InputStream is = FileUtils.copyToTempFile(fileis, file)) {
|
||||
AlitaResult<String> 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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user