package com.qxueyou.scc.base;
|
|
import java.io.BufferedReader;
|
import java.io.ByteArrayOutputStream;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.qxueyou.scc.base.dao.CommonDAO;
|
import com.qxueyou.scc.base.model.Result;
|
import com.qxueyou.scc.media.model.MediaVideo;
|
import com.qxueyou.scc.sys.service.IOssService;
|
|
/**
|
* aliyun资源临时授权
|
* @author cyq
|
*
|
*/
|
@Controller
|
@RequestMapping(value = "/sts")
|
public class StsCtrl {
|
@SuppressWarnings("unused")
|
private static final String M3U8 = "m3u8";
|
|
private static final String MP4 = "mp4";
|
|
@SuppressWarnings("unused")
|
private static final String AES_VIDEO_KEY = "u9WH6ldpMd29cJ0Z";
|
|
private final Logger log = LogManager.getLogger(StsCtrl.class);
|
|
@Autowired
|
IOssService ossService;
|
|
/**
|
* 公共数据访问对象
|
*/
|
@Autowired
|
private CommonDAO commonDAO;
|
|
/**
|
* 获取视频的m3u8访问权限,用获取临时访问权限的key路径作为解密密钥,临时生成m3u8文件,并上传到阿里云,并返回路径
|
* @param videoId
|
* @param definition
|
* @return
|
*/
|
@RequestMapping(value="video/m3u8", method=RequestMethod.GET, produces = "application/json;charset=utf-8")
|
public @ResponseBody Result videoM3u8(@RequestParam("videoId") String videoId, String definition) {
|
if (StringUtils.isEmpty(videoId)) {
|
return new Result(false, "参数错误");
|
}
|
|
// json.put("result", "success");
|
// // 检查缓存中是否有可用的url,如果有,则直接返回
|
// String urlCache = cacheService.get("sts_url_" + resId, String.class);
|
// if (StringUtils.isNotEmpty(urlCache)) {
|
// json.put("url", urlCache);
|
|
|
BufferedReader bf = null;
|
ByteArrayOutputStream byteArrOS =null;
|
try {
|
String def = definition;
|
MediaVideo video = commonDAO.read(MediaVideo.class, videoId);
|
|
if (video == null) {
|
return new Result(false, "资源不存在");
|
}
|
|
String resId = "";
|
String resUrl = null;
|
JSONObject json = null;
|
|
//判断是否是mp3等音乐格式文件,如果是则直接返回original地址
|
if(checkIsVoiceMedia(video)){
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
|
json.put("isConver",false);
|
return new Result(true, "", json);
|
}
|
|
resUrl = this.getM3u8ResUrl(def, video);
|
|
//判断是否是加密视频,未加密则直接返回视频地址
|
if(StringUtils.isEmpty(video.getSecretKey())){
|
//如果未转码加密,且m3u8视频不存在,则尝试读取MP4视频。
|
if (StringUtils.isEmpty(resUrl)) {
|
resUrl = StringUtils.isNotEmpty(video.getAndroidHD()) ? video.getAndroidHD() : StringUtils.isNotEmpty(video.getAndroidSD()) ? video.getAndroidSD() : video.getAndroidLD();
|
|
if (StringUtils.isEmpty(resUrl)) {
|
return new Result(false, "资源不存在");
|
}
|
}
|
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
|
json.put("isConver",false);
|
}else{
|
//如果转码加密后,依然不能读取到M3U8视频或读取到的是MP4直接返回,提示视屏不存在
|
if (StringUtils.isEmpty(resUrl)||resUrl.endsWith(MP4)) {
|
return new Result(false, "资源不存在");
|
}
|
|
resId = resUrl.substring(resUrl.indexOf("/video/") + 1);
|
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", resId);
|
json.put("isConver",true);
|
|
|
// String resDir = resId.substring(0, resId.lastIndexOf('/'));
|
|
//获取原视频VideoId
|
// String keyAddr = video.getOriginVideoId();
|
// if(StringUtils.isBlank(keyAddr)){
|
// keyAddr = video.getVideoId();
|
// }
|
|
// // 获取解密key的临时访问权限
|
// if(StringUtils.isNotEmpty(video.getM3u8KeyPath())){
|
// json.put("url", video.getM3u8KeyPath().substring(video.getM3u8KeyPath().indexOf("/video/") + 1));
|
// }else{
|
// String m3u8KeyPath = resDir + "/" + keyAddr + ".key";
|
// }
|
|
|
|
// String m3u8ResKey = resDir + "/" + uuid + ".m3u8";
|
|
// InputStream in = ossService.getObject(resId);
|
// String temp=null;
|
// byteArrOS=new ByteArrayOutputStream();
|
// bf = new BufferedReader(new InputStreamReader(in));
|
//
|
// while ((temp = bf.readLine()) != null) {
|
// if (temp.startsWith("#EXT-X-KEY")) {
|
// temp = temp.replace("_x.key", ".key");
|
// }
|
// temp= temp.concat("\n");
|
// byteArrOS.write(temp.getBytes());
|
// }
|
|
|
}
|
return new Result(true, "", json);
|
} catch (Exception e) {
|
log.error(e, e);
|
return new Result(false);
|
}finally{
|
IOUtils.closeQuietly(bf);
|
IOUtils.closeQuietly(byteArrOS);
|
}
|
}
|
|
|
/**
|
* 获取视频的m3u8访问权限,用获取临时访问权限的key路径作为解密密钥,临时生成m3u8文件,并上传到阿里云,并返回路径
|
* @param videoId
|
* @param definition
|
* @return
|
*/
|
@SuppressWarnings("unused")
|
@RequestMapping(value="video/mp4", method=RequestMethod.GET, produces = "application/json;charset=utf-8")
|
public @ResponseBody Result videoMp4(@RequestParam("videoId") String videoId, String definition) {
|
if (StringUtils.isEmpty(videoId)) {
|
return new Result(false, "参数错误");
|
}
|
|
BufferedReader bf = null;
|
ByteArrayOutputStream byteArrOS =null;
|
try {
|
String def = definition;
|
MediaVideo video = commonDAO.read(MediaVideo.class, videoId);
|
|
if (video == null) {
|
return new Result(false, "资源不存在");
|
}
|
|
String resId = "";
|
String resUrl = null;
|
JSONObject json = null;
|
|
//判断是否是mp3等音乐格式文件,如果是则直接返回original地址
|
if(checkIsVoiceMedia(video)){
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
|
json.put("isConver",false);
|
return new Result(true, "", json);
|
}
|
|
resUrl = this.getMp4ResUrl(def, video);
|
|
//如果转码地址不存在,并且源码地址是mp4则直接使用原视频地址播放
|
if(StringUtils.isEmpty(resUrl) && video.getOrigUrl().endsWith(MP4)){
|
resUrl = video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1);
|
}
|
|
//查看原视频是否是MP4
|
if (StringUtils.isEmpty(resUrl)) {
|
return new Result(false, "资源不存在");
|
}
|
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", resUrl.substring(resUrl.indexOf("/video/") + 1));
|
json.put("isConver",false);
|
|
return new Result(true, "", json);
|
} catch (Exception e) {
|
log.error(e, e);
|
return new Result(false);
|
}finally{
|
IOUtils.closeQuietly(bf);
|
IOUtils.closeQuietly(byteArrOS);
|
}
|
}
|
|
|
/**
|
* 获取resurl
|
*
|
* @param def
|
* @param video
|
* @return
|
*/
|
private String getM3u8ResUrl(String def, MediaVideo video){
|
return StringUtils.isNotEmpty(video.getIosHD()) ? video.getIosHD() : StringUtils.isNotEmpty(video.getIosSD()) ? video.getIosSD() : video.getIosLD();
|
}
|
|
/**
|
* 获取resurl
|
*
|
* @param def
|
* @param video
|
* @return
|
*/
|
private String getMp4ResUrl(String def, MediaVideo video){
|
return StringUtils.isNotEmpty(video.getAndroidHD()) ? video.getAndroidHD() : StringUtils.isNotEmpty(video.getAndroidSD()) ? video.getAndroidSD(): video.getAndroidLD();
|
}
|
|
/**
|
* 判断是否是音频media,精确判断需要枚举音频格式
|
* @return
|
*/
|
private boolean checkIsVoiceMedia(MediaVideo media){
|
if(media==null || StringUtils.isEmpty(media.getOrigUrl())){
|
return false;
|
}
|
|
return !(StringUtils.isNotEmpty(media.getAndroidHD())
|
||StringUtils.isNotEmpty(media.getAndroidSD())
|
||StringUtils.isNotEmpty(media.getAndroidLD())
|
||StringUtils.isNotEmpty(media.getIosHD())
|
||StringUtils.isNotEmpty(media.getIosSD())
|
||StringUtils.isNotEmpty(media.getIosLD()));
|
}
|
|
/**
|
* 测试接口专用
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value="test", method=RequestMethod.GET)
|
public @ResponseBody String test(HttpServletRequest request, HttpServletResponse response) {
|
return null;
|
}
|
}
|