1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package io.github.lyr2000.dissertation.service.impl;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.oss.OSS;
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 io.github.lyr2000.common.dto.Result;
import io.github.lyr2000.dissertation.components.AliYunOssPropertis;
import io.github.lyr2000.dissertation.pojo.dto.OssCallbackParam;
import io.github.lyr2000.dissertation.pojo.dto.ResultBean;
import io.github.lyr2000.dissertation.service.FileService;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lyr
* @description 阿里OSS 文件
* @create 2021-11-19 15:39
*/
@Slf4j
@Service
@ToString
@Data
public class AliyunOssFileServiceImpl implements FileService {
// @Value("${aliyun.oss.accessKey}")
// private String accessKey;
//
// @Value("${aliyun.oss.accessKeySecret}")
// private String accessKeySecret;
@Resource
private AliYunOssPropertis propertis;
@Override
public Result<?> uploadFile(MultipartFile file, String originLocalFileName, String uploadUID,
int partCnt, int totalCnt) {
return null;
}
@Override
public Result<?> uploadVideo(MultipartFile file) {
return null;
}
@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;
@PostConstruct
@Profile({"test","tests"})
void printProfiles() {
log.info("aliyun profiles = {}",this.toString());
}
/**
* // * 生成前端直传 token
* // * @return
* //
*/
// @Override
// public String generateUploadToken() {
// return null;
// }
@Resource
private OSSClient ossClient;
@Override
public ResultBean getPolicy() {
JSONObject resultBean = new JSONObject();
// 存储目录
String dir = ALIYUN_OSS_DIR_PREFIX + "/";
// 签名有效期
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/x-www-form-urlencoded");
// 提交节点
String action = "https://"+propertis.getEndpoint();
/*"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.put("accessid", ossClient.getCredentialsProvider().getCredentials().getAccessKeyId());
resultBean.put("policy", policy);
resultBean.put("signature", signature);
resultBean.put("dir", dir);
resultBean.put("expire", String.valueOf(expireEndTime / 1000));
resultBean.put("callback", callbackData);
resultBean.put("host", action);
// 返回结果
} catch (Exception e) {
log.error("签名生成失败", e);
return new ResultBean(ResultBean.RESCODE_SIGNATION_FAILED, ResultBean.RESMSG_SIGNATION_FAILED);
}
return new ResultBean(ResultBean.RESCODE_SUCCESS, ResultBean.RESMSG_SUCCESS, resultBean);
}
@Override
public ResultBean callback(HttpServletRequest request) {
JSONObject resultBean = new JSONObject();
String filename = request.getParameter("filename");
filename = "https://".concat(ALIYUN_OSS_ENDPOINT).concat("/").concat(filename);
resultBean.put("filename", filename);
resultBean.put("size", request.getParameter("size"));
resultBean.put("mimeType", request.getParameter("mimeType"));
resultBean.put("width", request.getParameter("width"));
resultBean.put("height", request.getParameter("height"));
return new ResultBean(ResultBean.RESCODE_SUCCESS, ResultBean.RESMSG_SUCCESS, resultBean);
}
// @Override
// public Result<?> uploadFile(MultipartFile file, int sliceIndex, int totalCnt, String originName) {
// return null;
// }
}
|