package com.yonge.cooleshow.admin.controller;


import com.ksyun.ks3.dto.PostObjectFormFields;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.mall.common.api.CommonResult;
import com.yonge.cooleshow.admin.dto.OssCallbackResult;
import com.yonge.cooleshow.admin.dto.OssPolicyResult;
import com.yonge.cooleshow.admin.service.OssService;
import com.yonge.toolset.thirdparty.entity.UploadSign;
import com.yonge.toolset.thirdparty.storage.StoragePluginContext;
import com.yonge.toolset.thirdparty.storage.provider.KS3StoragePlugin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Oss对象存储管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "OssController", description = "Oss管理")
@RequestMapping("/aliyun/oss")
public class OssController {
    @Autowired
    private OssService ossService;
    @Autowired
    private StoragePluginContext storagePluginContext;

    @ApiOperation(value = "Oss上传签名生成")
    @RequestMapping(value = "/policy", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OssPolicyResult> policy() {
        OssPolicyResult result = ossService.policy();
        return CommonResult.success(result);
    }

    @ApiOperation(value = "Oss上传成功回调")
    @RequestMapping(value = "callback", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OssCallbackResult> callback(HttpServletRequest request) {
        OssCallbackResult ossCallbackResult = ossService.callback(request);
        return CommonResult.success(ossCallbackResult);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", dataType = "String", value = "为空时默认使用daya"),
            @ApiImplicitParam(name = "fileName", dataType = "String", value = "要上传的文件名称,不包含路径信息"),
            @ApiImplicitParam(name = "postData", dataType = "Map", value = "1.如果使用js sdk上传的时候设置了ACL请设置,例\"acl\":\"public-read\"值要与SDK中一致,没有则删除该项</br>" +
                    "2.提供js sdk中的key值,例\"key\":\"20150115/中文/${filename}\""),
            @ApiImplicitParam(name = "unknowValueField", dataType = "List", value = "对于用户无法确定表单值的放在unknownValueField中(比如有的上传控件会添加一些表单项,但表单项的值可能是随机的)"),
    })
    @ApiOperation(value = "获取上传文件签名", notes = "{\n" +
            "    \"bucketName\":\"\",\n" +
            "    \"filename\":\"test.png\",\n" +
            "    \"postData\":{\n" +
            "        \"acl\":\"public-read\",\n" +
            "        \"key\":\"20150115/中文/${filename}\"\n" +
            "    },\n" +
            "    \"unknowValueField\":[\"test\"]\n" +
            "}")
    @PostMapping("/getUploadSign")
    public CommonResult<PostObjectFormFields> getUploadSign(@RequestBody UploadSign uploadSign) {
        return CommonResult.success(storagePluginContext.getUploadSign(KS3StoragePlugin.PLUGIN_NAME, uploadSign));
    }
}
