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
package cn.chnmuseum.party.web.controller;
import cn.chnmuseum.party.common.dfs.FastDFSUtils;
import cn.chnmuseum.party.common.log.MethodLog;
import cn.chnmuseum.party.common.log.OperModule;
import cn.chnmuseum.party.common.log.OperType;
import cn.chnmuseum.party.common.util.RSAUtils;
import cn.chnmuseum.party.common.util.TimeUtils;
import cn.chnmuseum.party.common.video.VideoEncryptUtil;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.Asset;
import cn.chnmuseum.party.model.TBoxOperation;
import cn.chnmuseum.party.service.AssetService;
import cn.chnmuseum.party.web.controller.base.BaseController;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* <pre>
* 文件资产 前端控制器
* </pre>
*
* @author Danny Lee
* @since 2021-04-06
*/
@Slf4j
@RestController
@RequestMapping("/asset")
@Api(tags = {"视频文件汇出接口"})
public class AssetController extends BaseController {
@Resource
private AssetService assetService;
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "nameOrCode", value = "视频文件名", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "videoContentCatId", value = "视频内容分类ID", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "videoContentCopyrightOwnerId", value = "视频内容版权方ID", paramType = "query", dataType = "String")
})
@PostMapping("/getPageList")
@RequiresAuthentication //@RequiresPermissions("asset:page")
@ApiOperation(value = "获取视频汇出分页列表", notes = "获取视频汇出分页列表")
@MethodLog(operModule = OperModule.VIDEOREMIT, operType = OperType.SELECT)
public Map<String, Object> getAssetPageList(GenericPageParam genericPageParam,
@RequestParam(value = "videoContentCatId", required = false) String videoContentCatId,
@RequestParam(value = "videoContentCopyrightOwnerId", required = false) String videoContentCopyrightOwnerId) {
LambdaQueryWrapper<Asset> ew = new LambdaQueryWrapper<>();
ew.eq(Asset::getPublished, true);
// 对名称或编码模糊查询
if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
ew.like(Asset::getFileName, genericPageParam.getNameOrCode());
}
// 设置排序规则
ew.orderByDesc(Asset::getCreateTime);
Page<Asset> page = this.assetService.pageByConditions(getPage(), videoContentCatId, videoContentCopyrightOwnerId);
return getResult(page);
}
@ApiOperation(value = "获取视频文件详情", notes = "获取视频文件详情")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "标识ID", dataType = "String", paramType = "path")
})
@GetMapping("/get/{id}")
@RequiresAuthentication //@RequiresPermissions("asset:get:id")
@MethodLog(operModule = OperModule.VIDEOREMIT, operType = OperType.SELECT)
public Map<String, Object> getById(@PathVariable("id") String id) {
Asset asset = assetService.getById(id);
return getResult(asset);
}
@ApiOperation(value = "视频文件汇出POST", notes = "视频文件汇出POST")
@ApiImplicitParams({
@ApiImplicitParam(name = "idList", value = "视频文件标识ID集合", dataType = "String", paramType = "query")
})
@PostMapping("/download")
@RequiresAuthentication
@MethodLog(operModule = OperModule.VIDEOREMIT, operType = OperType.VIDEO_EXPORT)
public void download(@RequestParam("idList") List<String> idList, HttpServletResponse response) throws IOException {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode("video.zip", "UTF-8"));
final Map<String, InputStream> map = new LinkedHashMap<>(idList.size());
final List<Asset> assetList = assetService.listByIds(idList);
for (Asset asset : assetList) {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
FastDFSUtils.downloadFile(asset.getFileUrlCrypto(), byteOutputStream);
map.put(asset.getMd5() + ".chnmuseum", VideoEncryptUtil.encrypt(new ByteArrayInputStream(byteOutputStream.toByteArray()), VideoEncryptUtil.cipher));
}
String[] paths = map.keySet().toArray(new String[0]);
InputStream[] ins = map.values().toArray(new InputStream[0]);
ZipUtil.zip(response.getOutputStream(), paths, ins);
}
@ApiOperation(value = "视频文件汇出GET", notes = "视频文件汇出GET")
@ApiImplicitParams({
@ApiImplicitParam(name = "idList", value = "视频文件标识ID集合", dataType = "String", paramType = "query")
})
@GetMapping("/download")
@RequiresAuthentication
@MethodLog(operModule = OperModule.VIDEOREMIT, operType = OperType.VIDEO_EXPORT)
public void downloadByGet(@RequestParam("idList") List<String> idList, HttpServletResponse response) throws IOException {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
final String fileName = TimeUtils.format(LocalDateTime.now(), TimeUtils.FORMAT_ONE) + "视频集.zip";
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
final Map<String, InputStream> map = new LinkedHashMap<>(idList.size());
final List<Asset> assetList = assetService.listByIds(idList);
for (Asset asset : assetList) {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
FastDFSUtils.downloadFile(asset.getFileUrlCrypto(), byteOutputStream);
map.put(asset.getMd5() + ".chnmuseum", VideoEncryptUtil.encrypt(new ByteArrayInputStream(byteOutputStream.toByteArray()), VideoEncryptUtil.cipher));
}
ZipUtil.zip(response.getOutputStream(), map.keySet().toArray(new String[0]), map.values().toArray(new InputStream[0]));
}
@ApiOperation(value = "导出所有机顶盒的文件加密密钥", notes = "导出所有机顶盒的文件加密密钥")
@GetMapping("/downloadCipher")
@RequiresAuthentication
@MethodLog(operModule = OperModule.VIDEOREMIT, operType = OperType.VIDEO_EXPORT)
public void downloadCipher(HttpServletResponse response) throws IOException {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode("cipher.zip", "UTF-8"));
final List<TBoxOperation> tBoxOperationList = assetService.listBoxByOrgan();
final Map<String, InputStream> map = new LinkedHashMap<>(tBoxOperationList.size());
for (TBoxOperation tBoxOperation : tBoxOperationList) {
map.put(tBoxOperation.getOrganName() + "-" + tBoxOperation.getMac().replaceAll(":|-", "_") + ".cipher", IoUtil.toStream(RSAUtils.encrypt(VideoEncryptUtil.cipher, tBoxOperation.getPublicKey()), StandardCharsets.UTF_8));
}
ZipUtil.zip(response.getOutputStream(), map.keySet().toArray(new String[0]), map.values().toArray(new InputStream[0]));
}
}