package com.zhongzheng.controller.alioss;


import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.modules.alioss.bo.OssRequest;
import com.zhongzheng.modules.alioss.service.OssService;
import com.zhongzheng.modules.alioss.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.util.Map;

/**
 * 阿里OSS
 *
 * @author change
 * @date 2021-05-17
 */
@Api(value = "阿里OSS上传文件控制器", tags = {"阿里OSS上传文件"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/aliyun/oss")
public class OssController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(OssController.class);

    private final OssService ossService;

    /**
     * policy获得
     */
    @ApiOperation("policy获得")
    @GetMapping("/policy")
    public AjaxResult<ResultBean> policy(@Validated OssRequest ossRequest) {
        ResultBean result = ossService.getPolicy(ossRequest);
        log.info("服务端生成签名:{}",result);
        return AjaxResult.success(result);
    }

    /**
     * 阿里回调OSS返回参数到前端
     */
    @ApiOperation("回调")
    @PostMapping("/callback")
    public ResultBean callback(HttpServletRequest request) {
        ResultBean ossCallbackResult = ossService.callback(request);
        log.info("oss成功的回调:{}",ossCallbackResult);
        return ossCallbackResult;
    }


    /**
     * upload上传文件
     */
    @ApiOperation("上传file图片")
    @PostMapping("/upload")
    public AjaxResult<String> upload(OssRequest file) throws Exception {
        String result = ossService.upload(file);
        log.info("服务端生成签名:{}",result);
        return AjaxResult.success("返回图片路径",result);
    }

}
