package com.sf.file.utils;
//package com.sf.file.utils;
//
//import java.io.File;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.LinkedHashMap;
//import java.util.Map;
//import java.util.UUID;
//
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.multipart.MultipartFile;
//
//import com.aliyun.oss.OSS;
//import com.aliyun.oss.OSSClientBuilder;
//import com.aliyun.oss.common.utils.BinaryUtil;
//import com.aliyun.oss.model.MatchMode;
//import com.aliyun.oss.model.PolicyConditions;
//import com.sf.common.exception.ServiceException;
//import com.sf.common.utils.file.FileUploadUtils;
//
//@Configuration
//public class AliOssUtil {
//	private static String END_POINT;
//
//	private static String ACCESS_KEY_ID;
//
//	private static String ACCESS_KEY_SECRET;
//
//	private static String BUCKET_NAME;
//
//	private static String COMMON_FILE_PATH;
//
//	public static int POLICY_EXPIRETIME;
//
//	@Value("${aliyun.oss.file.endpoint}")
//	public void setEndpoint(String endpoint) {
//		END_POINT = endpoint;
//	}
//
//	@Value("${aliyun.oss.file.keyId}")
//	public void setAccessKeyId(String accessKeyId) {
//		ACCESS_KEY_ID = accessKeyId;
//	}
//
//	@Value("${aliyun.oss.file.secret}")
//	public void setAccessKeySecret(String accessKeySecret) {
//		ACCESS_KEY_SECRET = accessKeySecret;
//	}
//
//	@Value("${aliyun.oss.file.bucketName}")
//	public void setBucketName(String bucketName) {
//		BUCKET_NAME = bucketName;
//	}
//
//	@Value("${aliyun.oss.file.path}")
//	public void setCommonFilePath(String commonFilePath) {
//		COMMON_FILE_PATH = commonFilePath;
//	}
//
//	@Value("${aliyun.oss.file.policyExpireTime}")
//	public void setPolicyExpireTime(int policyExpireTime) {
//		POLICY_EXPIRETIME = policyExpireTime;
//	}
//
//	public static Map<String, String> policy() {
//		OSS ossClient = null;
//		try {
//			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//			String dir = COMMON_FILE_PATH + "/" + formatter.format(new Date()) + "/";
//			String host = "https://" + BUCKET_NAME + "." + END_POINT; // host的格式为 bucketname.endpoint
//			ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
//			long expireEndTime = System.currentTimeMillis() + POLICY_EXPIRETIME * 1000;
//			Date expiration = new Date(expireEndTime);
//			PolicyConditions policyConds = new PolicyConditions();
//			policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
//			policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
//
//			String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
//			byte[] binaryData = postPolicy.getBytes("utf-8");
//			String encodedPolicy = BinaryUtil.toBase64String(binaryData);
//			String postSignature = ossClient.calculatePostSignature(postPolicy);
//
//			Map<String, String> respMap = new LinkedHashMap<String, String>();
//			respMap.put("accessid", ACCESS_KEY_ID);
//			respMap.put("policy", encodedPolicy);
//			respMap.put("signature", postSignature);
//			respMap.put("dir", dir);
//			respMap.put("host", host);
//			respMap.put("expire", String.valueOf(expireEndTime / 1000));
//			// respMap.put("expire", formatISO8601Date(expiration));
//
//			// 回调地址
////			JSONObject jasonCallback = new JSONObject();
////			jasonCallback.put("callbackUrl", callbackUrl);
////			jasonCallback.put("callbackBody",
////					"filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
////			jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded");
////			String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
////			respMap.put("callback", base64CallbackBody);
//
////			JSONObject ja1 = JSONObject.pfromObject(respMap);
////			// System.out.println(ja1.toString());
////			response.setHeader("Access-Control-Allow-Origin", "*");
////			response.setHeader("Access-Control-Allow-Methods", "GET, POST");
////			response(request, response, ja1.toString());
//			return respMap;
//		} catch (IOException e) {
//			throw new ServiceException("oss获取policy失败" + e.getMessage());
//		} finally {
//			// 关闭OSSClient。
//			if (ossClient != null) {
//				ossClient.shutdown();
//			}
//		}
//	}
//
//	public static HashMap<String, Object> localFileUpload(File file, String path, String fileType) {
//		HashMap<String, Object> resultMap = new HashMap<>();
//		if (file.exists() && file.isFile()) {
//			OSS ossClient = null;
//			// 创建OSSClient实例。
//			ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
//			// 上传文件流。
//			// 保存路径，注意：这里不要以 / 或 \ 开头
//			resultMap = FileUploadUtils.originalFilenameFile(file);
//			// uuid重命名文件
//			String newFileName = UUID.randomUUID() + fileType;
//			String fileName = COMMON_FILE_PATH + path + "/" + newFileName;
//			resultMap.put("path", "https://" + BUCKET_NAME + "." + END_POINT + "/" + fileName);
//			ossClient.putObject(BUCKET_NAME, fileName, file);
//		} else {
//			throw new ServiceException("上传文件不能为空");
//		}
//		return resultMap;
//	}
//
//	public static HashMap<String, Object> commonUpload(MultipartFile file, String path, String fileType) {
//		if (file.isEmpty()) {
//			throw new ServiceException("上传文件不能为空");
//		}
//		HashMap<String, Object> resultMap = new HashMap<>();
//		OSS ossClient = null;
//		// 创建OSSClient实例。
//		try {
//			ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
//			// 上传文件流。
//			// 保存路径，注意：这里不要以 / 或 \ 开头
//			resultMap = FileUploadUtils.originalFilenameMul(file);
//			// uuid重命名文件
//			String newFileName = UUID.randomUUID() + fileType;
//			String fileName = COMMON_FILE_PATH + path + "/" + newFileName;
//			resultMap.put("path", "https://" + BUCKET_NAME + "." + END_POINT + "/" + fileName);
//			ossClient.putObject(BUCKET_NAME, fileName, file.getInputStream());
//		} catch (IOException e) {
//			throw new ServiceException("上传失败" + e.getMessage());
//		} finally {
//			// 关闭OSSClient。
//			if (ossClient != null) {
//				ossClient.shutdown();
//			}
//		}
//		return resultMap;
//	}
//}
