package cn.reghao.autodop.dmaster.sys.controller;

import cn.reghao.autodop.dmaster.app.model.po.config.build.LocalBuildDir;
import cn.reghao.autodop.dmaster.util.UploadDownload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author reghao
 * @date 2021-09-24 11:19:05
 */
@Api(tags = "系统接口")
@RestController
@RequestMapping("/api/sys")
public class SysController {
    private final UploadDownload uploadDownload;

    public SysController(UploadDownload uploadDownload) {
        this.uploadDownload = uploadDownload;
    }

    @ApiOperation(value = "下载构建的应用")
    @GetMapping("/package/{filename}")
    public void downloadPackage(@PathVariable("filename") String filename, HttpServletResponse response)
            throws IOException {
        String filePath = LocalBuildDir.packDir + "/" + filename;
        FileInputStream fileInputStream = new FileInputStream(filePath);
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);
        uploadDownload.download(filename, bis, response);
    }
}
