|
|
@@ -0,0 +1,114 @@
|
|
|
+package com.zhongzheng.modules.alioss.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.common.utils.BinaryUtil;
|
|
|
+import com.aliyun.oss.model.MatchMode;
|
|
|
+import com.aliyun.oss.model.PolicyConditions;
|
|
|
+import com.zhongzheng.common.constant.Constants;
|
|
|
+import com.zhongzheng.common.core.domain.AjaxResult;
|
|
|
+import com.zhongzheng.common.exception.CustomException;
|
|
|
+import com.zhongzheng.modules.alioss.bo.OssCallbackParam;
|
|
|
+import com.zhongzheng.modules.alioss.service.OssService;
|
|
|
+import com.zhongzheng.modules.alioss.vo.ResultBean;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class OssServiceImpl implements OssService {
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(OssServiceImpl.class);
|
|
|
+ @Value("${aliyun.oss.policy.expire}")
|
|
|
+ private int ALIYUN_OSS_EXPIRE;
|
|
|
+ @Value("${aliyun.oss.maxSize}")
|
|
|
+ private int ALIYUN_OSS_MAX_SIZE;
|
|
|
+ @Value("${aliyun.oss.callback}")
|
|
|
+ private String ALIYUN_OSS_CALLBACK;
|
|
|
+ @Value("${aliyun.oss.bucketName}")
|
|
|
+ private String ALIYUN_OSS_BUCKET_NAME;
|
|
|
+ @Value("${aliyun.oss.endpoint}")
|
|
|
+ private String ALIYUN_OSS_ENDPOINT;
|
|
|
+ @Value("${aliyun.oss.dir.prefix}")
|
|
|
+ private String ALIYUN_OSS_DIR_PREFIX;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OSSClient ossClient;
|
|
|
+ @Override
|
|
|
+ public ResultBean getPolicy() {
|
|
|
+ JSONObject resultBean = new JSONObject();
|
|
|
+ // 存储目录
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String dir = ALIYUN_OSS_DIR_PREFIX+sdf.format(new Date())+"/"+ this.generateRandomFilename();
|
|
|
+ // 签名有效期
|
|
|
+ long expireEndTime = System.currentTimeMillis() + ALIYUN_OSS_EXPIRE * 1000;
|
|
|
+ Date expiration = new Date(expireEndTime);
|
|
|
+ // 文件大小
|
|
|
+ long maxSize = ALIYUN_OSS_MAX_SIZE * 1024 * 1024;
|
|
|
+ // 回调
|
|
|
+ OssCallbackParam callback = new OssCallbackParam();
|
|
|
+ callback.setCallbackUrl(ALIYUN_OSS_CALLBACK);
|
|
|
+ callback.setCallbackBody("{\"filename\":${object},\"size\":${size},\"mimeType\":${mimeType},\"height\":${imageInfo.height},\"width\":${imageInfo.width}}");
|
|
|
+ callback.setCallbackBodyType("application/json");
|
|
|
+ // 提交节点
|
|
|
+ String action = "https://" + ALIYUN_OSS_ENDPOINT;
|
|
|
+ try {
|
|
|
+ PolicyConditions policyConds = new PolicyConditions();
|
|
|
+ policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, maxSize);
|
|
|
+ policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
|
|
|
+ String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
|
|
|
+ byte[] binaryData = postPolicy.getBytes("utf-8");
|
|
|
+ String policy = BinaryUtil.toBase64String(binaryData);
|
|
|
+ String signature = ossClient.calculatePostSignature(postPolicy);
|
|
|
+ String callbackData = BinaryUtil.toBase64String(JSONUtil.parse(callback).toString().getBytes("utf-8"));
|
|
|
+ resultBean.set("accessid",ossClient.getCredentialsProvider().getCredentials().getAccessKeyId()).set("policy",policy).set("signature",signature)
|
|
|
+ .set("dir",dir).set("expire",String.valueOf(expireEndTime / 1000)).set("callback",callbackData).set("host",action);
|
|
|
+ // 返回结果
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("签名生成失败", e);
|
|
|
+ throw new CustomException("签名生成失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ return new ResultBean(resultBean);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String generateRandomFilename() {
|
|
|
+ String RandomFilename = "";
|
|
|
+ //生成随机数
|
|
|
+ Random rand = new Random();
|
|
|
+ int random = rand.nextInt();
|
|
|
+
|
|
|
+ Calendar calCurrent = Calendar.getInstance();
|
|
|
+ int intDay = calCurrent.get(Calendar.DATE);
|
|
|
+ int intMonth = calCurrent.get(Calendar.MONTH) + 1;
|
|
|
+ int intYear = calCurrent.get(Calendar.YEAR);
|
|
|
+ String now = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" +
|
|
|
+ String.valueOf(intDay) + "_";
|
|
|
+
|
|
|
+ RandomFilename = now + String.valueOf(random > 0 ? random : (-1) * random);
|
|
|
+
|
|
|
+ return RandomFilename;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultBean callback(Map<String, Object> map) {
|
|
|
+ JSONObject resultBean = new JSONObject();
|
|
|
+ String filename = map.get("filename").toString();
|
|
|
+ filename = "https://".concat(ALIYUN_OSS_ENDPOINT).concat("/").concat(filename);
|
|
|
+ resultBean.set("filename",filename).set("size",map.get("size").toString()).set("mimeType",map.get("mimeType").toString())
|
|
|
+ .set("width",map.get("width").toString()).set("height",map.get("height").toString());
|
|
|
+ return new ResultBean(resultBean);
|
|
|
+ }
|
|
|
+}
|