package com.koobietech.eas.controller;

import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.service.EasArchiveFileDownloadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lc
 */
@Tag(name = "下载文件模块")
@RestController
@RequestMapping("/file")
public class EasArcTlsFileDownloadController {

    @Resource
    EasArchiveFileDownloadService easArchiveFileDownloadService;

    @GetMapping("/getTokenById")
    @Operation(summary = "获取下载token", description = "前端传入archiveId，后端返回下载token")
    public JsonResult getFile(Integer archiveId) {

        String archiveToken = easArchiveFileDownloadService.getArchiveToken(archiveId);

        return JsonResult.data(archiveToken);
    }

    @GetMapping("/getFileByToken")
    @Operation(summary = "获取文件流", description = "前端把token传入请求头，后端返回文件流")
    public void getFileByToken(@RequestParam String archiveToken, HttpServletResponse response) {

        //加if判断，如果返回的是false，说明token已经过期，返回null
        easArchiveFileDownloadService.downloadFileByToken(archiveToken, response);
    }




}
