diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java index a689eb6..ec07cc2 100644 --- a/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java +++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java @@ -1,28 +1,5 @@ package ai.qiwu.com.tts_service.controller; -import ai.qiwu.com.tts_service.common.*; -import ai.qiwu.com.tts_service.pojo.TTSResponse; -import ai.qiwu.com.tts_service.pojo.deploy.MoveBotAudio; -import ai.qiwu.com.tts_service.pojo.deploy.Result; -import ai.qiwu.com.tts_service.pojo.upload.UploadOneSenData; -import ai.qiwu.com.tts_service.pojo.upload.UploadPlotImgResponse; -import ai.qiwu.com.tts_service.service.Initialize; -import ai.qiwu.com.tts_service.service.TTService; -import com.alibaba.fastjson.JSON; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.catalina.connector.ClientAbortException; -import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.CollectionUtils; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.multipart.MultipartHttpServletRequest; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; @@ -35,11 +12,49 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Semaphore; import java.util.stream.Collectors; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.catalina.connector.ClientAbortException; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +import ai.qiwu.com.tts_service.common.ApachHttpUtils; +import ai.qiwu.com.tts_service.common.FileUtil; +import ai.qiwu.com.tts_service.common.ResponseCode; +import ai.qiwu.com.tts_service.common.ServerResponse; +import ai.qiwu.com.tts_service.common.TTSUtils; +import ai.qiwu.com.tts_service.pojo.deploy.MoveBotAudio; +import ai.qiwu.com.tts_service.pojo.deploy.Result; +import ai.qiwu.com.tts_service.service.Initialize; +import ai.qiwu.com.tts_service.service.TTService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + /** * painter * 20-6-11 下午7:57 @@ -64,21 +79,60 @@ public class DeployController { } return ServerResponse.createByError(); } - + @PostMapping("/background/audio/certain") + public ServerResponse moveTempToCertain2(@RequestBody JSONObject requestBody) { + String tmpFileRelativePathStr = requestBody.getString("tmpFileRelativePath"); + String targetBotAccount = requestBody.getString("targetBotAccount"); + String tmpFileFullPathStr = Initialize.AUDIO_DIR+tmpFileRelativePathStr; + Path tmpFileFullPath = Paths.get(tmpFileFullPathStr); + if(Files.exists(tmpFileFullPath) + && !Files.isDirectory(tmpFileFullPath) + && StringUtils.endsWith(tmpFileRelativePathStr,".mp3") + && tmpFileRelativePathStr.startsWith("/game_editor_tmp")) { + try { + String tmpFileRelativePathStr2 = tmpFileRelativePathStr.substring("/game_editor_tmp".length()); + if(tmpFileRelativePathStr2.startsWith(File.separator+targetBotAccount)) { + tmpFileRelativePathStr2 = tmpFileRelativePathStr2.substring((File.separator+targetBotAccount).length()); + } + String targetFileRelativePathStr = File.separator+targetBotAccount+tmpFileRelativePathStr2; + String targetFileFullPathStr = Initialize.AUDIO_DIR+targetFileRelativePathStr; + Path targetFileFullPath = Paths.get(targetFileFullPathStr); + File fileDistFile = new File(targetFileFullPathStr); + if(!fileDistFile.getParentFile().exists()) { + fileDistFile.mkdirs(); + } + Files.move(tmpFileFullPath, targetFileFullPath, + StandardCopyOption.REPLACE_EXISTING); + FileUtil.deleteFile(new File(tmpFileFullPathStr)); + return ServerResponse.createBySuccess(targetFileRelativePathStr); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + return ServerResponse.createByError(); + } + @GetMapping("/background/audio") public ServerResponse moveTempToCertain(String path, @RequestParam(required = false) String oldPath) { Path pt = Paths.get(Initialize.AUDIO_DIR+path); if (Files.exists(pt) && !Files.isDirectory(pt) && StringUtils.endsWith(path,".mp3") && StringUtils.contains(path, "/background/tmp/")) { String certain = path.replace("tmp/", ""); + Path distPath = Paths.get(Initialize.AUDIO_DIR+certain); + File fileDistFile = new File(Initialize.AUDIO_DIR+certain); + if(!fileDistFile.getParentFile().exists()) { + fileDistFile.mkdirs(); + } try { - Files.move(pt, Paths.get(Initialize.AUDIO_DIR+certain), + Files.move(pt, distPath, StandardCopyOption.REPLACE_EXISTING); FileUtil.deleteFile(new File(Initialize.AUDIO_DIR+path.substring(0, path.indexOf("/tmp/")+4))); if (StringUtils.isNotBlank(oldPath)) { delAudio(oldPath); } - return ServerResponse.createBySuccess(); + return ServerResponse.createBySuccess(certain); } catch (Exception e) { log.error("moveTempToCertain error:"+e); } @@ -94,25 +148,47 @@ public class DeployController { String type = mulRequest.getParameter("type"); // default, category, plot String sampleRatStr = mulRequest.getParameter("sampleRate"); // default, category, plot String bitRateStr = mulRequest.getParameter("bitRate"); // default, category, plot - - if (file == null || file.isEmpty() || StringUtils.isAnyBlank(type, auth, botAccount) || groupId == null) { - return ServerResponse.createByErrorEnum(ResponseCode.DATA_IS_WRONG); + if(file == null || file.isEmpty()) { + log.error("文件为空"); + return ServerResponse.createByErrorEnum(ResponseCode.DATA_IS_WRONG); + } + if(type.equals("default")) { + if (StringUtils.isAnyBlank(type, auth) || groupId == null) { + log.error("type:{}, auth:{}, groupId:{}", type, auth, groupId); + return ServerResponse.createByErrorEnum(ResponseCode.DATA_IS_WRONG); + } + if(botAccount==null) { + botAccount = ""; + } + botAccount = botAccount.trim(); + }else { + if (StringUtils.isAnyBlank(type, botAccount, auth) || groupId == null) { + log.error("type:{}, auth:{}, botAccount, groupId:{}", type, auth, botAccount, groupId); + return ServerResponse.createByErrorEnum(ResponseCode.DATA_IS_WRONG); + } } // 校验 + String url = Initialize.ZUUL_HOST+"/audio/auth"; + Map headers = new HashMap<>(); + headers.put("Authorization", auth); + headers.put("botAccount", botAccount); + headers.put("groupId", String.valueOf(groupId)); + String result = null; try { - Map headers = new HashMap<>(); - headers.put("Authorization", auth); - headers.put("botAccount", botAccount); - headers.put("groupId", String.valueOf(groupId)); - String result = ApachHttpUtils.get(Initialize.ZUUL_HOST+"/audio/auth", headers, null); - ServerResponse response = JSON.parseObject(result, ServerResponse.class); - if (ResponseCode.SUCCESS.getCode().equals(response.getCode())) { - String path = File.separator+botAccount+"/background/tmp/"; + result = ApachHttpUtils.get(url, headers, null); + JSONObject resultJSONObject = JSON.parseObject(result); + if (ResponseCode.SUCCESS.getCode().equals(resultJSONObject.getIntValue("code"))) { + String path = ""; + if(botAccount.equals("")) { + path = "/game_editor_tmp/background/"; + }else { + path = "/game_editor_tmp"+File.separator+botAccount+"/background/"; + } switch (type) { case "category": path += "category/"+RandomStringUtils.randomAlphabetic(5)+System.currentTimeMillis()+".mp3";break; case "plot" : path += "plot/"+RandomStringUtils.randomAlphabetic(5)+System.currentTimeMillis()+".mp3";break; - default : path += "default.mp3"; + default : path += RandomStringUtils.randomAlphabetic(5)+System.currentTimeMillis()+".mp3"; } Path pt; Files.createDirectories((pt=Paths.get(Initialize.AUDIO_DIR+path)).getParent()); @@ -146,9 +222,19 @@ public class DeployController { } return ServerResponse.createBySuccess(Initialize.SERVER_URL+"/api/audio/tts?filename="+path); + }else { + log.error("url:{}{}", Initialize.ZUUL_HOST, "/audio/auth"); + log.error("headers:{}", headers); + log.error("result:{}", result); + log.error("ServerResponse:{}", resultJSONObject); + return ServerResponse.createByErrorEnum(ResponseCode.USER_AUTHENTICATION_FAIL); } } catch (Exception e) { - log.error("uploadBackgroundAudio error:"+ ExceptionUtils.getStackTrace(e)); + log.error("url:{}{}", Initialize.ZUUL_HOST, "/audio/auth"); + log.error("headers:{}", headers); + log.error("result:{}", result); + log.error("uploadBackgroundAudio error:"+ ExceptionUtils.getStackTrace(e)); + } return ServerResponse.createByErrorEnum(ResponseCode.ERROR); } @@ -603,6 +689,9 @@ public class DeployController { } public static void main(String[] args) { - System.out.println(TTSUtils.buildMP3Filename("这是一段测试文本,请听下音效,选择你认为合适的一套声音")); + String resultStr = "{\"code\":1,\"msg\":\"成功\",\"data\":null}"; + ServerResponse serverResponse = JSON.parseObject(resultStr, ServerResponse.class); + System.out.println("serverResponse:"+serverResponse); + // System.out.println(TTSUtils.buildMP3Filename("这是一段测试文本,请听下音效,选择你认为合适的一套声音")); } } diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/filter/CorsFilter.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/filter/CorsFilter.java new file mode 100644 index 0000000..0f1b527 --- /dev/null +++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/filter/CorsFilter.java @@ -0,0 +1,37 @@ +package ai.qiwu.com.tts_service.filter; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.stereotype.Component; + + +@Component +public class CorsFilter implements Filter { + + final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class); + + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) + throws IOException, ServletException { + HttpServletResponse response = (HttpServletResponse) res; + response.setHeader("Access-Control-Allow-Origin", "*"); + response.setHeader("Access-Control-Allow-Credentials", "true"); + response.setHeader("Access-Control-Allow-Methods", "*"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Headers", "*"); + chain.doFilter(req, res); + } + + public void init(FilterConfig filterConfig) { + } + + public void destroy() { + } +} \ No newline at end of file