AssetController.java 8.36 KB
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]));
    }

}