Commit aed730b4 by 苗权威

modify

parent c6880ae2
...@@ -2285,7 +2285,7 @@ public class TTService { ...@@ -2285,7 +2285,7 @@ public class TTService {
log.info("检测到重复加密存在:"+path); log.info("检测到重复加密存在:"+path);
path = URLDecoder.decode(path, "UTF-8"); path = URLDecoder.decode(path, "UTF-8");
} catch (Exception e) { } catch (Exception e) {
path = URLDecoder.decode(path); path = path.replaceAll("%2f", "/");
} }
} }
Path pt = Paths.get(path); Path pt = Paths.get(path);
......
...@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; ...@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -138,6 +139,7 @@ public class TtsLsService { ...@@ -138,6 +139,7 @@ public class TtsLsService {
String encodePth; String encodePth;
try { try {
encodePth = URLEncoder.encode(path, "UTF-8"); encodePth = URLEncoder.encode(path, "UTF-8");
encodePth = encodePth.replaceAll("%2F", "/");
} catch (Exception e) { } catch (Exception e) {
encodePth = path; encodePth = path;
} }
...@@ -166,13 +168,13 @@ public class TtsLsService { ...@@ -166,13 +168,13 @@ public class TtsLsService {
boolean xiaoai; boolean xiaoai;
if ((xiaoai="xiaoai".equals(platform)) || !StringUtils.endsWith(_type, "64")) { if ((xiaoai="xiaoai".equals(platform)) || !StringUtils.endsWith(_type, "64")) {
if (!xiaoai) {
// 异步合成b64文件 // 异步合成b64文件
POOL.submit(() -> getFromFutureAndGenerateB64File(futures, response, file_ls)); final String finalBotName = botName;
} POOL.submit(() -> getFromFutureAndGenerateB64File(futures, response, file_ls, xiaoai, finalBotName));
dealMp3UrlRequest(response, file_ls, botName, _type, xiaoai); dealMp3UrlRequest(response, file_ls, botName, _type, xiaoai);
} else { } else {
dealHasB64FileRequest(futures, response, file_ls, botName, _type); if (!getFromFutureAndGenerateB64File(futures, response, file_ls, false, botName)) return;
dealHasB64FileRequest(response, file_ls, botName, _type);
} }
} else { } else {
TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(HttpServletResponse.SC_BAD_REQUEST, "text_info_ls字段不能为空"), null, HttpServletResponse.SC_BAD_REQUEST); TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(HttpServletResponse.SC_BAD_REQUEST, "text_info_ls字段不能为空"), null, HttpServletResponse.SC_BAD_REQUEST);
...@@ -180,11 +182,11 @@ public class TtsLsService { ...@@ -180,11 +182,11 @@ public class TtsLsService {
} }
private boolean getFromFutureAndGenerateB64File(List<Future<ResponseCodeWithData>> futures, HttpServletResponse response, private boolean getFromFutureAndGenerateB64File(List<Future<ResponseCodeWithData>> futures, HttpServletResponse response,
List<String> file_ls) { List<String> file_ls, boolean xiaoai, String botName) {
// 阻塞等待mp3文件生成成功 // 阻塞等待mp3文件生成成功
for (Future<ResponseCodeWithData> future : futures) { for (Future<ResponseCodeWithData> future : futures) {
try { try {
ResponseCodeWithData data = future.get(); ResponseCodeWithData data = future.get(6, TimeUnit.MINUTES);
if (data.getCode() != HttpServletResponse.SC_OK) { if (data.getCode() != HttpServletResponse.SC_OK) {
if (data.getCode() == 8) { if (data.getCode() == 8) {
log.info("tts_ls:出现竞争情况->"+data.getRes()+",时间:"+new Date(System.currentTimeMillis())); log.info("tts_ls:出现竞争情况->"+data.getRes()+",时间:"+new Date(System.currentTimeMillis()));
...@@ -215,6 +217,28 @@ public class TtsLsService { ...@@ -215,6 +217,28 @@ public class TtsLsService {
} }
} }
if (xiaoai) {
String tmp_name = TTSUtils.pythonlistToStr(file_ls);
String tmp_audio_name = StringUtils.join(new String[]{Initialize.getAudioDir(), botName, "xiaoai", tmp_name}, File.separator);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
if (Boolean.TRUE.equals(stringRedisTemplate.opsForValue()
.setIfAbsent(tmp_audio_name, "1", 20, TimeUnit.SECONDS))) {
try {
Files.createDirectories(Paths.get(tmp_audio_name).getParent());
TTSUtils.combineMp3File(file_ls, tmp_audio_name);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
log.error("小爱平台的请求,合并mp3文件失败");
}
} catch (Exception e) {
log.error("ffmpeg 合并音频出错:"+e);
} finally {
stringRedisTemplate.delete(tmp_audio_name);
}
} else {
log.warn("tts_ls xiaoai 合成竞争->"+tmp_audio_name+",时间:"+new Date(System.currentTimeMillis()));
}
}
} else {
// 非xiaoai平台的请求,首先将.mp3文件的数据存储在对应的.mp3.b64文件内 // 非xiaoai平台的请求,首先将.mp3文件的数据存储在对应的.mp3.b64文件内
// 将.mp3文件的数据存储在对应的.mp3.b64文件内 // 将.mp3文件的数据存储在对应的.mp3.b64文件内
for (String filename : file_ls) { for (String filename : file_ls) {
...@@ -233,13 +257,12 @@ public class TtsLsService { ...@@ -233,13 +257,12 @@ public class TtsLsService {
} }
} }
} }
}
return true; return true;
} }
private void dealHasB64FileRequest(List<Future<ResponseCodeWithData>> futures, HttpServletResponse response, private void dealHasB64FileRequest(HttpServletResponse response,
List<String> file_ls, String botName, String _type) { List<String> file_ls, String botName, String _type) {
if (!getFromFutureAndGenerateB64File(futures, response, file_ls)) return;
// 若请求字段_type为filename,则返回.b64文件的路径列表,负责返回所有.b64文件的内容数据 // 若请求字段_type为filename,则返回.b64文件的路径列表,负责返回所有.b64文件的内容数据
TtsLsResponse ttsLsResponse = new TtsLsResponse(); TtsLsResponse ttsLsResponse = new TtsLsResponse();
try { try {
...@@ -272,25 +295,8 @@ public class TtsLsService { ...@@ -272,25 +295,8 @@ public class TtsLsService {
if (xiaoai) { if (xiaoai) {
String tmp_name = TTSUtils.pythonlistToStr(file_ls); String tmp_name = TTSUtils.pythonlistToStr(file_ls);
String tmp_audio_name = StringUtils.join(new String[]{Initialize.getAudioDir(), botName, "xiaoai", tmp_name}, File.separator); String tmp_audio_name = StringUtils.join(new String[]{Initialize.getAudioDir(), botName, "xiaoai", tmp_name}, File.separator);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
if (Boolean.TRUE.equals(stringRedisTemplate.opsForValue()
.setIfAbsent(tmp_audio_name, "1", 20, TimeUnit.SECONDS))) {
POOL.submit(() -> {
try {
Files.createDirectories(Paths.get(tmp_audio_name).getParent());
TTSUtils.combineMp3File(file_ls, tmp_audio_name);
} catch (Exception e) {
log.error("ffmpeg 合并音频出错:"+e);
}
});
String audio = Initialize.getServerUrlCdn()+"/api/audio/cache/"+botName+"/?filename="+tmp_audio_name+"&exist=1";
TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(audio,HttpServletResponse.SC_OK), null, HttpServletResponse.SC_OK);
} else {
log.warn("tts_ls xiaoai 合成竞争->"+tmp_audio_name+",时间:"+new Date(System.currentTimeMillis()));
String audio = Initialize.getServerUrlCdn()+"/api/audio/cache/"+botName+"/?filename="+tmp_audio_name+"&exist=1"; String audio = Initialize.getServerUrlCdn()+"/api/audio/cache/"+botName+"/?filename="+tmp_audio_name+"&exist=1";
TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(audio,HttpServletResponse.SC_OK), null, HttpServletResponse.SC_OK); TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(audio,HttpServletResponse.SC_OK), null, HttpServletResponse.SC_OK);
}
}
} else { } else {
TtsLsResponse ttsLsResponse = new TtsLsResponse(); TtsLsResponse ttsLsResponse = new TtsLsResponse();
try { try {
...@@ -418,4 +424,12 @@ public class TtsLsService { ...@@ -418,4 +424,12 @@ public class TtsLsService {
ttsLsResponse.getAudio().add(audio); ttsLsResponse.getAudio().add(audio);
} }
} }
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "/root/TTS-service/audio/46128715/A26_offline/被调用/e33d5713d5c9a4d4b22bd2917ecdab6b.mp3";
String ss;
System.out.println((ss=URLEncoder.encode(s, "UTF-8")));
System.out.println(URLEncoder.encode(ss, "UTF-8"));
System.out.println(ss.replaceAll("%2F", "/"));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment