修改音视频文件样例配置功能,表单界面删除样例文件的上传,改由后台调用外部程序生成样例文件。

Signed-off-by: zhangwei <zhangwei@intranet.com>
This commit is contained in:
zhangwei
2018-07-11 14:20:01 +08:00
parent e989f78fe6
commit 84338393d9
10 changed files with 270 additions and 46 deletions

View File

@@ -27,6 +27,8 @@ public class AvFileSampleCfg extends BaseCfg<AvFileSampleCfg> {
private Integer compileId;
private String srcPath;
private String samplePath;
private Integer isSampleCreated;
private String resultPath;
public String getSrcPath() {
return srcPath;
@@ -76,4 +78,17 @@ public class AvFileSampleCfg extends BaseCfg<AvFileSampleCfg> {
public void setLevel(Integer level) {
this.level = level;
}
public Integer getIsSampleCreated() {
return isSampleCreated;
}
public void setIsSampleCreated(Integer isSampleCreated) {
this.isSampleCreated = isSampleCreated;
}
public String getResultPath() {
return resultPath;
}
public void setResultPath(String resultPath) {
this.resultPath = resultPath;
}
}

View File

@@ -434,11 +434,18 @@ public final class Constants {
public static final String AV_FILE_PATH = Configurations.getStringProperty("av_file_path", "/home/ysp/");
public static final String AV_SAMPLE_AUDIO_REGION = Configurations.getStringProperty("av_sample_audio_region", "av_sample_audio");
public static final String AV_SAMPLE_VEDIO_REGION = Configurations.getStringProperty("av_sample_vedio_region", "av_sample_vedio");
public static final String AV_SAMPLE_PITURE_REGION = Configurations.getStringProperty("av_sample_pitrue_region", "av_sample_pitrue");
public static final String AV_SAMPLE_PICTURE_REGION = Configurations.getStringProperty("av_sample_picture_region", "av_sample_picture");
public static final String AV_SAMPLE_VOIP_REGION = Configurations.getStringProperty("av_sample_voip_region", "av_sample_voip");
public static final String AUDIO_SAMPLE_CREATE_PROC = Configurations.getStringProperty("audio_sample_create_proc", "./audio_convert_proc");
public static final String VEDIO_SAMPLE_CREATE_PROC = Configurations.getStringProperty("vedio_sample_create_proc", "./vedio_convert_proc");
public static final String PICTURE_SAMPLE_CREATE_PROC = Configurations.getStringProperty("picture_sample_create_proc", "./picture_convert_proc");
public static final boolean AUDIO_SAMPLE_PROC_PARAM_IS_QUOTATION = Configurations.getBooleanProperty("audio_sample_proc_param_is_quotation", true);
public static final boolean VEDIO_SAMPLE_PROC_PARAM_IS_QUOTATION = Configurations.getBooleanProperty("vedio_sample_proc_param_is_quotation", true);
public static final boolean PICTURE_SAMPLE_PROC_PARAM_IS_QUOTATION = Configurations.getBooleanProperty("picture_sample_proc_param_is_quotation", true);
public static final boolean AUDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION = Configurations.getBooleanProperty("audio_sample_proc_param_is_translation", false);
public static final boolean VEDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION = Configurations.getBooleanProperty("vedio_sample_proc_param_is_translation", false);
public static final boolean PICTURE_SAMPLE_PROC_PARAM_IS_TRANSLATION = Configurations.getBooleanProperty("picture_sample_proc_param_is_translation", false);
//HTTP自定义域相关参数
public static String HTTP_HEADER_USER_REGION_KEY=Configurations.getStringProperty("http_header_user_region_key", "HTTP_HEADER");
public static String HTTP_HEADER_DICT_MODULE=Configurations.getStringProperty("http_header_dict_module", "REQUEST_HEADER_DISTRICT");

View File

@@ -1,6 +1,9 @@
package com.nis.web.controller.configuration.ntc;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -39,6 +42,7 @@ import com.nis.domain.maat.ToMaatResult;
import com.nis.domain.maat.ToMaatResult.ResponseData;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.ConfigServiceUtil;
import com.nis.util.Constants;
import com.nis.util.DictUtils;
import com.nis.util.FileUtils;
import com.nis.util.JsonMapper;
@@ -107,33 +111,45 @@ public class AvController extends BaseController {
public String saveFileSample(Model model,HttpServletRequest request,HttpServletResponse response, RedirectAttributes redirectAttributes,
String ids,AvFileSampleCfg entity,MultipartFile srcFile,MultipartFile sampleFile){
try{
if(srcFile!=null && sampleFile!=null &&
srcFile.getSize()>0 && sampleFile.getSize()>0){
// if(srcFile!=null && sampleFile!=null &&
// srcFile.getSize()>0 && sampleFile.getSize()>0){
if(srcFile!=null && srcFile.getSize()>0){
String sep = System.getProperty("file.separator");
String srcFilePath = request.getRealPath("/")+"srcFile";
String sampleFilePath = request.getRealPath("/")+"sampleFile";
String srcFilePath = Constants.AV_FILE_PATH+entity.getCfgType()+sep+"srcFile";//源文件保存路径
String sampleFilePath = Constants.AV_FILE_PATH+entity.getCfgType()+sep+"sampleFile";//样例文件保存路径
String resultFilePath = Constants.AV_FILE_PATH+entity.getCfgType()+sep+"resultFile";//结果文件保存路径
FileUtils.createDirectory(srcFilePath);
FileUtils.createDirectory(sampleFilePath);
String srcFileName = UUID.randomUUID()+FileUtils.getSuffix(srcFile.getOriginalFilename(), true);
String sampleFileName = UUID.randomUUID()+FileUtils.getSuffix(sampleFile.getOriginalFilename(), true);
File uploadSrcFile = new File(srcFilePath+sep+srcFileName);
File uploadSampleFile = new File(sampleFilePath+sep+sampleFileName);
FileCopyUtils.copy(srcFile.getBytes(), uploadSrcFile);
FileCopyUtils.copy(sampleFile.getBytes(),uploadSampleFile);
String fileName = UUID.randomUUID()+"";
String srcFileAllPath = srcFilePath+sep+fileName+FileUtils.getSuffix(srcFile.getOriginalFilename(), true);
String sampleFileAllPath = sampleFilePath+sep+fileName+".sample";
String resultFileAllPath = resultFilePath+sep+fileName+".result";
entity.setSrcPath(srcFileAllPath);
entity.setSamplePath(sampleFileAllPath);
entity.setResultPath(resultFileAllPath);
File uploadSrcFile = new File(srcFileAllPath);
File uploadSampleFile = new File(sampleFileAllPath);
FileCopyUtils.copy(srcFile.getBytes(), uploadSrcFile);//保存源文件
String host = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
String srcUrl = host+"/srcFile/"+uploadSrcFile.getName();
String sampleUrl = host+"/sampleFile/"+uploadSampleFile.getName();
String srcUrl = host+srcFilePath.substring(srcFilePath.indexOf(Constants.AV_FILE_PATH)+Constants.AV_FILE_PATH.length()-1)+sep+uploadSrcFile.getName();
String sampleUrl = host+sampleFilePath.substring(sampleFilePath.indexOf(Constants.AV_FILE_PATH)+Constants.AV_FILE_PATH.length()-1)+sep+uploadSampleFile.getName();
srcUrl = srcUrl.replace("\\", "/");
sampleUrl = sampleUrl.replace("\\", "/");
logger.info("srcUrl:"+srcUrl);
logger.info("sampleUrl:"+sampleUrl);
String srcMd5 = FileUtils.getFileMD5(uploadSrcFile);
String sampleMd5 = FileUtils.getFileMD5(uploadSampleFile);
entity.setSrcUrl(srcUrl);
entity.setSrcPath(uploadSrcFile.getPath());
entity.setSampleUrl(sampleUrl);
entity.setSamplePath(uploadSampleFile.getPath());
entity.setSrcMd5(srcMd5);
entity.setSampleMd5(sampleMd5);
// File uploadSrcFile = new File(srcFilePath);
// FileCopyUtils.copy(srcFile.getBytes(), uploadSrcFile);
// String srcMd5 = FileUtils.getFileMD5(uploadSrcFile);
// File uploadSampleFile = new File(sampleFilePath);
// String sampleMd5 = FileUtils.getFileMD5(uploadSampleFile);
// entity.setSrcMd5(srcMd5);
// entity.setSampleMd5(sampleMd5);
}
avCfgService.saveOrUpdateAvFileSample(entity);

View File

@@ -297,7 +297,8 @@
SRC_URL,SAMPLE_URL, SRC_MD5,SAMPLE_MD5,LEVEL,CFG_DESC,ACTION,IS_VALID,IS_AUDIT,
CREATOR_ID,CREATE_TIME,EDITOR_ID,EDIT_TIME,AUDITOR_ID,AUDIT_TIME,
SERVICE_ID,REQUEST_ID,COMPILE_ID,IS_AREA_EFFECTIVE,CLASSIFY,
ATTRIBUTE,LABLE,AREA_EFFECTIVE_IDS,function_id,cfg_type,cfg_region_code,src_path,sample_path
ATTRIBUTE,LABLE,AREA_EFFECTIVE_IDS,function_id,cfg_type,cfg_region_code,src_path,sample_path,
is_sample_created
)values (
#{srcUrl,jdbcType=VARCHAR},#{sampleUrl,jdbcType=VARCHAR},#{srcMd5,jdbcType=VARCHAR},
#{sampleMd5,jdbcType=VARCHAR},#{level,jdbcType=INTEGER},#{cfgDesc,jdbcType=VARCHAR},#{action,jdbcType=INTEGER},
@@ -307,7 +308,8 @@
#{requestId,jdbcType=INTEGER},#{compileId,jdbcType=INTEGER},#{isAreaEffective,jdbcType=INTEGER},
#{classify,jdbcType=VARCHAR},#{attribute,jdbcType=VARCHAR},#{lable,jdbcType=VARCHAR},
#{areaEffectiveIds,jdbcType=VARCHAR},#{functionId,jdbcType=INTEGER},
#{cfgType,jdbcType=VARCHAR},#{cfgRegionCode,jdbcType=INTEGER},#{srcPath,jdbcType=VARCHAR},#{samplePath,jdbcType=VARCHAR}
#{cfgType,jdbcType=VARCHAR},#{cfgRegionCode,jdbcType=INTEGER},#{srcPath,jdbcType=VARCHAR},#{samplePath,jdbcType=VARCHAR},
#{isSampleCreated,jdbcType=INTEGER}
)
</insert>
@@ -403,6 +405,9 @@
<if test="samplePath != null and samplePath != ''">
sample_path = #{samplePath,jdbcType=VARCHAR},
</if>
<if test="isSampleCreated != null" >
is_sample_created = #{isSampleCreated,jdbcType=INTEGER},
</if>
<if test="srcMd5 != null and srcMd5 != ''">
src_md5 = #{srcMd5,jdbcType=VARCHAR},
</if>

View File

@@ -2,15 +2,20 @@ package com.nis.web.service.configuration;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -23,6 +28,7 @@ import com.nis.domain.maat.ToMaatResult;
import com.nis.domain.maat.ToMaatResult.ResponseData;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.ConfigServiceUtil;
import com.nis.util.Constants;
import com.nis.util.FileUtils;
import com.nis.util.StringUtil;
import com.nis.web.dao.configuration.AvCfgDao;
@@ -86,7 +92,77 @@ public class AvCfgService extends BaseService{
}
if(compileId!=0){
entity.setCompileId(compileId);
entity.setIsSampleCreated(0);
File uploadSrcFile = new File(entity.getSrcPath());
String srcMd5 = FileUtils.getFileMD5(uploadSrcFile);
entity.setSrcMd5(srcMd5);
avCfgDao.insertAvFileSample(entity);
//调用外部脚本,生成样例文件
List list = new ArrayList();
Map<String,Object> map = new HashMap();
map.put("srcFile", entity.getSrcPath());
map.put("dstFile", entity.getSamplePath());
map.put("resultFile", entity.getResultPath());
map.put("fileId", entity.getCfgId());
list.add(map);
String param = gsonToJson(list);
String sampleCreatelProc = "";
if(entity.getCfgType().equals(Constants.AV_SAMPLE_AUDIO_REGION)||
entity.getCfgType().equals(Constants.AV_SAMPLE_VOIP_REGION)){//音频样例配置或VOIP样例配置
sampleCreatelProc = Constants.AUDIO_SAMPLE_CREATE_PROC;
if(Constants.AUDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION){
param = StringEscapeUtils.escapeJava(param);
}
if(Constants.AUDIO_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
param = "'"+param+"'";
}
}else if(entity.getCfgType().equals(Constants.AV_SAMPLE_VEDIO_REGION)){//视频样例配置
sampleCreatelProc = Constants.VEDIO_SAMPLE_CREATE_PROC;
if(Constants.VEDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION){
param = StringEscapeUtils.escapeJava(param);
}
if(Constants.VEDIO_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
param = "'"+param+"'";
}
}else if(entity.getCfgType().equals(Constants.AV_SAMPLE_PICTURE_REGION)){//图片样例配置
sampleCreatelProc = Constants.PICTURE_SAMPLE_CREATE_PROC;
if(Constants.PICTURE_SAMPLE_PROC_PARAM_IS_TRANSLATION){
param = StringEscapeUtils.escapeJava(param);
}
if(Constants.PICTURE_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
param = "'"+param+"'";
}
}
logger.info("调用外部程序输入参数:"+param);
Map resultMap = execShell(sampleCreatelProc,param);
logger.info("调用外部程序结果:"+resultMap);
if(resultMap.get("exitStatus").equals(0)){//调用外部程序成功
String out = resultMap.get("out").toString();//输出参数
JSONArray resArray =JSONArray.fromObject(out);
JSONObject resObject = resArray.getJSONObject(0);
logger.info("调用外部程序输出参数:"+resObject);
int state = resObject.getInt("state");
int fileSize = resObject.getInt("fileSize");
Integer fileId = resObject.getInt("fileId");
if(state==1 && fileSize>0){//成功
entity.setIsSampleCreated(1);//样例文件创建成功
}else{
entity.setIsSampleCreated(-1);//样例文件创建失败
}
if(fileId.equals(entity.getCfgId().intValue())){
File uploadSampleFile = new File(entity.getSamplePath());
String sampleMd5 = FileUtils.getFileMD5(uploadSampleFile);
entity.setSampleMd5(sampleMd5);
avCfgDao.updateAvFileSample(entity);
}else{
logger.info("调用外部程序返回fileId错误,输入的fileId为"+entity.getCfgId()+",输出的fileId为"+fileId);
}
}else{
throw new MaatConvertException(resultMap.get("error")!=null?resultMap.get("error").toString():resultMap.get("message").toString());
}
}else{
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
@@ -212,4 +288,73 @@ public class AvCfgService extends BaseService{
}
}
/**
* 调用shell脚本 返回运行结果
*
* @param shellName
* @param params
* @return
*/
public Map<String, Object> execShell(String shellName,
String... params) {
Map<String, Object> result = new HashMap<String, Object>();
StringBuilder sb = new StringBuilder();
sb.append(shellName);
for (String temp : params) {
sb.append(" " + temp);
}
String os = System.getProperty("os.name").toLowerCase();
String cmd1 = "";
String cmd2 = "";
if(os.contains("windows")){
cmd1 = "cmd.exe";
cmd2 = "/c";
}else{
cmd1 = "/bin/sh";
cmd2 = "-c";
}
logger.info("调用脚本信息,cmd1:"+cmd1+",cmd2:"+cmd2);
String cmdarray[] = new String[] {cmd1, cmd2 ,sb.toString() };
BufferedReader br = null;
BufferedReader bre = null;
try {
Process exec = Runtime.getRuntime().exec(cmdarray);
exec.getInputStream();
br = new BufferedReader(
new InputStreamReader(exec.getInputStream()));
bre = new BufferedReader(new InputStreamReader(
exec.getErrorStream()));
String s = null;
StringBuilder out = new StringBuilder();
while ((s = br.readLine()) != null) {
out.append(s);
}
result.put("out", out.toString());//输出参数
out.setLength(0);//清空
while ((s = bre.readLine()) != null) {
out.append(s);
}
result.put("error", out.toString());//错误信息
int waitFor = exec.waitFor();
logger.info("调用脚本:"+shellName+",执行返回状态值:"+waitFor);
result.put("exitStatus", waitFor);//执行状态
} catch (Exception e) {
e.printStackTrace();
logger.error("调用 " + shellName + " 脚本异常", e);
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bre != null)
try {
bre.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}