语音文件增加保存到本地逻辑(线下部署使用)

This commit is contained in:
joylink_zhangsai 2022-09-30 13:36:59 +08:00
parent 435758678c
commit b83f3eaee2
2 changed files with 32 additions and 1 deletions

View File

@ -8,6 +8,10 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class VoiceFileUtils { public class VoiceFileUtils {
@ -24,6 +28,9 @@ public class VoiceFileUtils {
private static final AtomicInteger SN = new AtomicInteger(0); private static final AtomicInteger SN = new AtomicInteger(0);
private static final String LOCAL_FILE_URI_PREFIX = File.separator + "usr" + File.separator + "local"
+ File.separator + "joylink" + File.separator + "oss" + File.separator + "joylink";
public static String saveFile(byte[] bytes) { public static String saveFile(byte[] bytes) {
try { try {
return upload(bytes); return upload(bytes);
@ -59,6 +66,31 @@ public class VoiceFileUtils {
CommonJsonResponse response = restTemplate.postForObject(url, map, CommonJsonResponse.class); CommonJsonResponse response = restTemplate.postForObject(url, map, CommonJsonResponse.class);
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertEquals(200, response.getCode()); BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertEquals(200, response.getCode());
return (String) response.getData(); return (String) response.getData();
// return localUpload(bytes);
}
static String localUpload(byte[] bytes) {
LocalDate date = LocalDate.now();
int index = SN.getAndIncrement();
String fileName = index + ".wav";
String url = File.separator + "AUDIO" + File.separator + date + File.separator + fileName;
File file = new File(LOCAL_FILE_URI_PREFIX + url);
while (file.exists()) {
fileName = index + "-" + new Random().nextInt(10000) + ".wav";
url = File.separator + "AUDIO" + File.separator + date + File.separator + fileName;
file = new File(LOCAL_FILE_URI_PREFIX + url);
}
File parentFile = file.getAbsoluteFile().getParentFile();
if (!parentFile.exists()) {
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertTrue(parentFile.mkdirs(), "创建文件夹失败");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(bytes);
} catch (Exception e) {
throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("文件写入失败", e);
}
return url;
} }
public static byte[] readFile(String filePath) { public static byte[] readFile(String filePath) {

View File

@ -2,7 +2,6 @@ package club.joylink.rtss.websocket;
import club.joylink.rtss.services.ISysUserService; import club.joylink.rtss.services.ISysUserService;
import club.joylink.rtss.services.LoginSessionManager; import club.joylink.rtss.services.LoginSessionManager;
import club.joylink.rtss.services.SysUserService;
import club.joylink.rtss.vo.AccountVO; import club.joylink.rtss.vo.AccountVO;
import club.joylink.rtss.vo.LoginUserInfoVO; import club.joylink.rtss.vo.LoginUserInfoVO;
import club.joylink.rtss.websocket.interceptor.PresenceChannelInterceptor; import club.joylink.rtss.websocket.interceptor.PresenceChannelInterceptor;