派生自 projectDept/qhighschool

EricsHu
2023-06-19 bc3b37b1622091def1f6ee4c3eb3ff79499b3466
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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;
    }    
}