From aed730b494c0dc4bc7edda778f98a2d636299faa Mon Sep 17 00:00:00 2001 From: 苗权威 <1160292058@qq.com> Date: Sat, 6 Jun 2020 20:28:35 +0800 Subject: [PATCH] modify --- .idea/workspace.xml | 327 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java | 2 +- tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------- 3 files changed, 152 insertions(+), 277 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3a9ff56..fb39658 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,129 +2,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -170,44 +50,37 @@ - + - - + + - - - - - + + - - + + - - + - + - - - - - + + @@ -224,18 +97,8 @@ - + - - - - - - - - - - @@ -243,7 +106,7 @@ - + @@ -261,7 +124,7 @@ - + @@ -321,12 +184,12 @@ @@ -380,6 +243,7 @@ + @@ -476,14 +340,13 @@ - - + @@ -499,7 +362,7 @@ - + + + - - - - + + - + @@ -771,7 +649,7 @@ - + @@ -1054,20 +932,6 @@ - - - - - - - - - - - - - - @@ -1075,23 +939,6 @@ - - - - - - - - - - - - - - - - - @@ -1106,83 +953,97 @@ - + - - + + - + - - + + - + - - - - - + + - + - - - - - + + - + - - + + - + - - + + - + - - + + - + - - + + - - + + + + + + + + - - + + - - - + + + + + + + + + + + + + + + + + diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java index f0823be..c21e926 100755 --- a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java +++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java @@ -2285,7 +2285,7 @@ public class TTService { log.info("检测到重复加密存在:"+path); path = URLDecoder.decode(path, "UTF-8"); } catch (Exception e) { - path = URLDecoder.decode(path); + path = path.replaceAll("%2f", "/"); } } Path pt = Paths.get(path); diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java index 76e838a..d51b001 100644 --- a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java +++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Paths; @@ -138,6 +139,7 @@ public class TtsLsService { String encodePth; try { encodePth = URLEncoder.encode(path, "UTF-8"); + encodePth = encodePth.replaceAll("%2F", "/"); } catch (Exception e) { encodePth = path; } @@ -166,13 +168,13 @@ public class TtsLsService { boolean xiaoai; if ((xiaoai="xiaoai".equals(platform)) || !StringUtils.endsWith(_type, "64")) { - if (!xiaoai) { - // 异步合成b64文件 - POOL.submit(() -> getFromFutureAndGenerateB64File(futures, response, file_ls)); - } + // 异步合成b64文件 + final String finalBotName = botName; + POOL.submit(() -> getFromFutureAndGenerateB64File(futures, response, file_ls, xiaoai, finalBotName)); dealMp3UrlRequest(response, file_ls, botName, _type, xiaoai); } else { - dealHasB64FileRequest(futures, response, file_ls, botName, _type); + if (!getFromFutureAndGenerateB64File(futures, response, file_ls, false, botName)) return; + dealHasB64FileRequest(response, file_ls, botName, _type); } } else { 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 { } private boolean getFromFutureAndGenerateB64File(List> futures, HttpServletResponse response, - List file_ls) { + List file_ls, boolean xiaoai, String botName) { // 阻塞等待mp3文件生成成功 for (Future future : futures) { try { - ResponseCodeWithData data = future.get(); + ResponseCodeWithData data = future.get(6, TimeUnit.MINUTES); if (data.getCode() != HttpServletResponse.SC_OK) { if (data.getCode() == 8) { log.info("tts_ls:出现竞争情况->"+data.getRes()+",时间:"+new Date(System.currentTimeMillis())); @@ -215,31 +217,52 @@ public class TtsLsService { } } - // 非xiaoai平台的请求,首先将.mp3文件的数据存储在对应的.mp3.b64文件内 - // 将.mp3文件的数据存储在对应的.mp3.b64文件内 - for (String filename : file_ls) { - if (filename.equals(Initialize.getThirdParty())) { - continue; + 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())); + } } - boolean endWithB64; - String filename_64 = (endWithB64=StringUtils.endsWith(filename, ".b64")) ? filename : filename+".b64"; - if (!TTSUtils.doesFileExist(filename_64)) { - if (endWithB64) filename = filename_64.substring(0, filename_64.length()-4); - try { - Files.copy(Paths.get(filename), Paths.get(filename_64), StandardCopyOption.ATOMIC_MOVE); - } catch (Exception e) { - log.error("拷贝.b64文件出错:"+e); - new File(filename_64).delete(); + } else { + // 非xiaoai平台的请求,首先将.mp3文件的数据存储在对应的.mp3.b64文件内 + // 将.mp3文件的数据存储在对应的.mp3.b64文件内 + for (String filename : file_ls) { + if (filename.equals(Initialize.getThirdParty())) { + continue; + } + boolean endWithB64; + String filename_64 = (endWithB64=StringUtils.endsWith(filename, ".b64")) ? filename : filename+".b64"; + if (!TTSUtils.doesFileExist(filename_64)) { + if (endWithB64) filename = filename_64.substring(0, filename_64.length()-4); + try { + Files.copy(Paths.get(filename), Paths.get(filename_64), StandardCopyOption.ATOMIC_MOVE); + } catch (Exception e) { + log.error("拷贝.b64文件出错:"+e); + new File(filename_64).delete(); + } } } } return true; } - private void dealHasB64FileRequest(List> futures, HttpServletResponse response, + private void dealHasB64FileRequest(HttpServletResponse response, List file_ls, String botName, String _type) { - if (!getFromFutureAndGenerateB64File(futures, response, file_ls)) return; - // 若请求字段_type为filename,则返回.b64文件的路径列表,负责返回所有.b64文件的内容数据 TtsLsResponse ttsLsResponse = new TtsLsResponse(); try { @@ -272,25 +295,8 @@ 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))) { - 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"; - TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(audio,HttpServletResponse.SC_OK), null, HttpServletResponse.SC_OK); - } - } + 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 { TtsLsResponse ttsLsResponse = new TtsLsResponse(); try { @@ -418,4 +424,12 @@ public class TtsLsService { 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", "/")); + } } -- libgit2 0.27.1