package cn.reghao.devops.web.mgr.app.controller;

import cn.reghao.devops.web.mgr.app.service.bd.GetApp;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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.ResponseBody;

/**
 * @author reghao
 * @date 2023-04-21 23:57:00
 */
@Slf4j
@Controller
@RequestMapping("/api/app/bd")
public class GetAppController {
    private final GetApp getApp;

    public GetAppController(GetApp getApp) {
        this.getApp = getApp;
    }

    @ApiOperation(value = "构建包下载接口")
    @GetMapping(value = "/packageurl/{buildLogId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String packageUrl(@PathVariable("buildLogId") String buildLogId) throws Exception {
        String packageUrl = getApp.packageUrl(buildLogId);
        return WebResult.successWithMsg(packageUrl);
    }

    @ApiOperation(value = "构建包下载接口")
    @GetMapping("/dl/{filename}")
    public String dlPackage(@PathVariable("filename") String filename) {
        try {
            getApp.dlPackage(filename);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return "redirect:/app/bd/build";
    }
}
