package com.tuoheng.miniprogram.controller;

import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.miniprogram.service.IAliyunOssService;
import com.tuoheng.miniprogram.service.impl.AliyunOssServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.List;

/**
 * 阿里云对象存储OSS 前端控制器
 *
 * @author WangHaoran
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/aliyunOss")
public class AliyunOssController {

    @Autowired
    IAliyunOssService aliyunOssService;

    /**
     * 生成单个以GET方法访问的签名URL
     *
     * @param objectName 填写Object完整路径，例如exampleobject.txt。Object完整路径中不能包含Bucket名称。
     * @return
     * @throws Throwable
     */
    @GetMapping("/generatePresignedUrl")
    public URL generatePresignedUrl(String objectName) throws Throwable {
        return aliyunOssService.generatePresignedUrl(objectName);
    }

    /**
     * 生成多个以GET方法访问的签名URL
     *
     * @param objectNameList 填写Object完整路径，例如exampleobject.txt。Object完整路径中不能包含Bucket名称。
     *                       此处请填写多个Object完整路径，用于一次性获取多个Object的签名URL。
     * @return
     * @throws Throwable
     */
    @GetMapping("/generatePresignedUrls")
    public List<URL> generatePresignedUrls(String[] objectNameList) throws Throwable {
        return aliyunOssService.generatePresignedUrls(objectNameList);
    }

    /**
     * 获取securityToken
     *
     * @return
     */
    @GetMapping("/getSecurityToken")
    public JsonResult getSecurityToken() {
        AssumeRoleResponse.Credentials credentials = AliyunOssServiceImpl.getSecurityToken("SessionTest");
        return JsonResult.success(credentials);
    }
}
