package com.xhpc.general.controller;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.*;
import com.xhpc.common.core.domain.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class XhpcFileController {

    public static final String accessKeyId = "LTAI5tHxc8hrG6cT5nDtEkiV";
    public static final String accessKeySecret = "5kFEaIFE7eeODxKQwD5mvTRF48MK6w";

    @PostMapping(value = "/addFile")
    public R addFile(@RequestParam MultipartFile file){
        //获取文件名称
        String originalFilename = file.getOriginalFilename();
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "xhpc-bucket1";
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String format = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");
        String objectName = "vinFile/"+format+"/"+originalFilename;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        try {

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file.getInputStream());
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            System.out.println(result.toString());
            Map<String,Object> map =new HashMap<>();
            map.put("url","https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"+objectName);
            return R.ok(map);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        }catch (Exception e){

        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return R.ok();
    }
}
