//package com.backendsys.controller.Upload;
//
//import com.aliyun.oss.OSS;
//import com.aliyun.oss.OSSClientBuilder;
//import com.aliyun.oss.OSSException;
//import com.aliyun.oss.model.PutObjectRequest;
//import com.aliyun.oss.model.PutObjectResult;
//import com.aliyuncs.exceptions.ClientException;
//import com.backendsys.utils.CommonUtil;
//import com.backendsys.utils.response.Result;
//import com.backendsys.utils.response.ResultEnum;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
//@RestController
//public class UploadAliOSSController {
//
//    /**
//     * 文档：图片处理(水印)(大小)
//     * https://help.aliyun.com/zh/oss/developer-reference/img-2?spm=a2c4g.11186623.0.0.28dc3af2212Yhe
//     *
//     * 注意：需要绑定自定义域名，才能获得图片浏览器访问的效果
//     * https://help.aliyun.com/zh/oss/user-guide/how-to-ensure-an-object-is-previewed-when-you-access-the-object?spm=a2c4g.11186623.0.0.60c2431eiMgwZB
//     * 图片地址示例：
//     * https://backendsys.oss-cn-shenzhen.aliyuncs.com/20230812/20230812132756.png
//     */
//
//    @Value("${aliyun.oss.max-size}")
//    private long maxSize;
//    @Value("${aliyun.oss.access-key-id}")
//    private String accessKeyId;
//    @Value("${aliyun.oss.access-key-secret}")
//    private String accessKeySecret;
//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//    @Value("${aliyun.oss.bucket-name}")
//    private String bucketName;
//    @Value("${aliyun.oss.accessible-domain}")
//    private String accessibleDomain;
//
//    /**
//     * 阿里云 OSS
//     */
//    @PreAuthorize("@sr.hasPermission(1.1)")
//    @PostMapping("/api/ali/ossUpload")
//    public Result upload(@RequestParam("file") MultipartFile file) throws ClientException {
//
//        // 检查上传的文件是否为空
//        if (file.isEmpty()) {
//            return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能为空");
//        }
//        // 判断文件大小是否超过
//        if (file.getSize() > maxSize) {
//            return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能大于 " + maxSize/1024/1024 + " MB");
//        }
//
//        //System.out.println("accessKeyId = " + accessKeyId);
//        //System.out.println("accessKeySecret = " + accessKeySecret);
//        //System.out.println("endpoint = " + endpoint);
//        //System.out.println("bucketName = " + bucketName);
//
//        // 填写Object完整路径，例如exampledir/exampleobject.txt。
//        // 按日期作为文件目录
//        String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date()) + File.separator;
//
//        // 获得后缀名
//        String originalFileName = file.getOriginalFilename();
//        String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
//
//        // 生成新的文件名
//        String newFileName = CommonUtil.generateFilename(suffix);
//
//        //
//        String objectName = dateDir + newFileName;
//
//        // 创建OSSClient实例
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
//        // 简单上传文件
//        try {
//            // 获取文件内容
//            InputStream inputStream = file.getInputStream();
//            // 创建PutObjectRequest对象。
//            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
//            // 创建PutObject请求。
//            PutObjectResult result = ossClient.putObject(putObjectRequest);
//
//        } 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 (IOException ce) {
//            System.out.println(ce);
//        } finally {
//            if (ossClient != null) {
//                ossClient.shutdown();
//            }
//        }
//
//        // 获取临时访问权限
//        //GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
//        //Date expiration = new Date(new Date().getTime() + 3600 * 1000);
//        //request.setExpiration(expiration);
//        //////方法一： 直接覆盖请求头
//        ////ResponseHeaderOverrides Headers=new ResponseHeaderOverrides();
//        ////Headers.setContentDisposition(String.format("attachment;filename=%s", fileName));
//        ////request.setResponseHeaders(Headers);
//        ////方法二： 设置setQueryParameter();其实方法一源码也是这样设置的
//        // Map<String, String> queryParams = new LinkedHashMap<>(8);
//        // queryParams.put("response-content-disposition", String.format("attachment;filename=%s", newFileName));
//        // request.setQueryParameter(queryParams);
//        // URL url = ossClient.generatePresignedUrl(request);
//
//        Map<String, Object> map = new LinkedHashMap<>();
//        map.put("filename", newFileName);
//        map.put("url", accessibleDomain + File.separator + objectName);
//        map.put("type", suffix.replace(".", ""));
//        map.put("size", file.getSize());
//
//        return Result.success(map);
//    }
//
//
//}
