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
package cn.wisenergy.chnmuseum.party.web.controller;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ZipUtil;
import cn.wisenergy.chnmuseum.party.common.dfs.FastDFSUtils;
import cn.wisenergy.chnmuseum.party.common.util.RSAUtils;
import cn.wisenergy.chnmuseum.party.common.video.VideoEncryptUtil;
import cn.wisenergy.chnmuseum.party.common.vo.GenericPageParam;
import cn.wisenergy.chnmuseum.party.model.Asset;
import cn.wisenergy.chnmuseum.party.model.TBoxOperation;
import cn.wisenergy.chnmuseum.party.model.TUser;
import cn.wisenergy.chnmuseum.party.service.AssetService;
import cn.wisenergy.chnmuseum.party.service.TBoxOperationService;
import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
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.ServletOutputStream;
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.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* <pre>
* 文件资产 前端控制器
* </pre>
*
* @author Danny Lee
* @since 2021-04-06
*/
@Slf4j
@RestController
@RequestMapping("/asset")
@Api(tags = {"视频文件汇出接口"})
public class AssetController extends BaseController {
private static final String BASE_STRING = "1234567890abcdef";
@Resource
private AssetService assetService;
@Resource
private TBoxOperationService tBoxOperationService;
@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 = "获取视频汇出分页列表")
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<>();
// 对名称或编码模糊查询
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")
public Map<String, Object> getById(@PathVariable("id") String id) {
Asset asset = assetService.getById(id);
return getResult(asset);
}
@ApiOperation(value = "视频文件汇出", notes = "视频文件汇出")
@ApiImplicitParams({
@ApiImplicitParam(name = "idList", value = "视频文件标识ID集合", dataType = "String", paramType = "query")
})
@PostMapping("/download")
@RequiresAuthentication
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() + 1);
final List<Asset> assetList = assetService.listByIds(idList);
final String cipher = RandomUtil.randomString(BASE_STRING, 16);
for (final Asset asset : assetList) {
final String fileUrl = asset.getFileUrl();
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
FastDFSUtils.downloadFile(fileUrl, byteOutputStream);
final Set<MetaData> fileMetaData = FastDFSUtils.getFileMetaData(fileUrl);
String fileName = fileMetaData.stream().filter(x -> "MD5".equals(x.getName())).map(MetaData::getValue).findFirst().get() + ".chnmuseum";
map.put(fileName, VideoEncryptUtil.encrypt(new ByteArrayInputStream(byteOutputStream.toByteArray()), cipher));
}
final TUser user = getcurUser();
final TBoxOperation tBoxOperation = this.tBoxOperationService.getOne(Wrappers.<TBoxOperation>lambdaQuery().eq(TBoxOperation::getOrganId, user.getOrgId()));
map.put("cipher.txt", IoUtil.toStream(RSAUtils.encrypt(cipher, tBoxOperation.getPublicKey()), StandardCharsets.UTF_8));
ServletOutputStream outputStream = response.getOutputStream();
String[] paths = map.keySet().toArray(new String[0]);
InputStream[] ins = map.values().toArray(new InputStream[0]);
ZipUtil.zip(outputStream, paths, ins);
}
}