WeMaterialServiceImpl.java
8.93 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.http.ForestResponse;
import com.linkwechat.common.config.CosConfig;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.config.ServerConfig;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.enums.WeErrorCodeEnum;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.file.FileUploadUtils;
import com.linkwechat.common.utils.file.FileUtils;
import com.linkwechat.wecom.client.WeMediaClient;
import com.linkwechat.wecom.domain.WeMaterial;
import com.linkwechat.wecom.domain.dto.WeMediaDto;
import com.linkwechat.wecom.domain.dto.WeResultDto;
import com.linkwechat.wecom.domain.vo.WeMaterialFileVO;
import com.linkwechat.wecom.mapper.WeMaterialMapper;
import com.linkwechat.wecom.service.IWeMaterialService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* 素材service
*
* @author KEWEN
* @date 2020-10-08
*/
@Slf4j
@Service
public class WeMaterialServiceImpl implements IWeMaterialService {
@Autowired
private WeMaterialMapper weMaterialMapper;
@Autowired
private WeMediaClient weMediaClient;
@Autowired
private RuoYiConfig ruoYiConfig;
@Override
public WeMaterialFileVO uploadWeMaterialFile(MultipartFile file, String type) {
if (null == file) {
throw new WeComException("文件为空!");
}
try {
// //上传临时素材
Optional<com.linkwechat.common.enums.MediaType > mediaType = com.linkwechat.common.enums.MediaType .of(type);
if (!mediaType.isPresent()) {
throw new WeComException("媒体类型出错!");
}
//构造返回结果
return WeMaterialFileVO.builder().materialUrl(ruoYiConfig.getFile().getCos().getCosImgUrlPrefix()).materialName(FileUploadUtils.upload2Cos(file, ruoYiConfig.getFile().getCos())).build();
} catch (Exception e) {
throw new WeComException(e.getMessage());
}
}
@Override
public int insertWeMaterial(WeMaterial weMaterial) {
weMaterial.setCreateTime(DateUtil.date());
weMaterial.setId(SnowFlakeUtil.nextId());
return weMaterialMapper.insertWeMaterial(weMaterial);
}
@Override
public int deleteWeMaterialById(Long id) {
return weMaterialMapper.deleteWeMaterialById(id);
}
@Override
public void deleteWeMaterialByIds(Long[] ids) {
weMaterialMapper.deleteWeMaterialByIds(ids);
}
@Override
public int updateWeMaterial(WeMaterial weMaterial) {
weMaterial.setCreateTime(DateUtil.date());
return weMaterialMapper.updateWeMaterial(weMaterial);
}
@Override
public WeMaterial findWeMaterialById(Long id) {
return weMaterialMapper.findWeMaterialById(id);
}
@Override
public List<WeMaterial> findWeMaterials(String categoryId, String search, String mediaType) {
return weMaterialMapper.findWeMaterials(categoryId, search, mediaType);
}
@Override
public void resetCategory(String categoryId, String materials) {
List<String> materialList = Arrays.asList(StringUtils.splitByWholeSeparatorPreserveAllTokens(materials, WeConstans.COMMA));
if (CollectionUtil.isNotEmpty(materialList)) {
materialList.forEach(s -> {
weMaterialMapper.resetCategory(categoryId, s);
});
}
}
@Override
public WeMediaDto uploadTemporaryMaterial(String url, String type, String name) {
HttpURLConnection conn = null;
try{
URL materialUrl = new URL(url);
conn = (HttpURLConnection) materialUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
InputStream inputStream = conn.getInputStream();
return weMediaClient.upload(inputStream, name, type);
}catch (Exception e){
e.printStackTrace();
log.error("上传临时文件失败......url:{},type:{},name:{},ex:{},st:{}",url,type,name,e.getMessage(),e.getStackTrace());
}finally {
if (conn !=null){
conn = null;
}
}
return null;
}
/**
* 获取附件mediaId
* @param url
* @param type
* @param attachmentType
* @param name
* @return
*/
@Override
public WeMediaDto uploadAttachmentMaterial(String url, String type,Integer attachmentType, String name) {
HttpURLConnection conn = null;
try{
URL materialUrl = new URL(url);
conn = (HttpURLConnection) materialUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
InputStream inputStream = conn.getInputStream();
return weMediaClient.uploadAttachment(inputStream, name, type,attachmentType);
}catch (Exception e){
e.printStackTrace();
log.error("上传临时文件失败......url:{},type:{},name:{},ex:{},st:{}",url,type,name,e.getMessage(),e.getStackTrace());
}finally {
if (conn !=null){
conn = null;
}
}
return null;
}
@Override
public WeMediaDto uploadImg(MultipartFile file) {
return weMediaClient.uploadimg(file);
}
@Override
public WeMediaDto getMediaToResponse(String mediaId) {
WeMediaDto weMediaDto = new WeMediaDto();
weMediaDto.setErrcode(0);
ForestResponse forestResponse = weMediaClient.getMediaToResponse(mediaId);
if (forestResponse != null) {
try {
String fixedName =new String(forestResponse.getFilename().getBytes("ISO8859-1"),"UTF-8");
fixedName = fixedName.replace("\"", "");
weMediaDto.setFileName(fixedName);
if(ruoYiConfig.getFile().isStartCosUpload()) {
String fileFullName = FileUploadUtils.upload2Cos(forestResponse.getInputStream(), FileUtil.getSuffix(fixedName), ruoYiConfig.getFile().getCos());
String imgUrlPrefix = ruoYiConfig.getFile().getCos().getCosImgUrlPrefix();
weMediaDto.setUrl(imgUrlPrefix + fileFullName);
}else {
String filePath = FileUploadUtils.upload(forestResponse.getInputStream(), fixedName);
weMediaDto.setUrl(filePath);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
weMediaDto.setErrcode(-1);
} catch (Exception e) {
e.printStackTrace();
throw new WeComException("文件下载异常");
}
}
return weMediaDto;
}
/**
* 下载服务器素材
* @param media_id
* @param fileType
* @return
*/
@Override
public String mediaGet(String media_id, String fileType,String extentType) {
InputStream file = weMediaClient.mediaGet(media_id);
MultipartFile file1=new MultipartFile() {
@Override
public String getName() {
return SnowFlakeUtil.nextId()+"";
}
@Override
public String getOriginalFilename() {
return SnowFlakeUtil.nextId()+"."+ extentType;
}
@Override
public String getContentType() {
return extentType;
}
@Override
public boolean isEmpty() {
return false;
}
@SneakyThrows
@Override
public long getSize() {
return file.available();
}
@Override
public byte[] getBytes() throws IOException {
return new byte[0];
}
@Override
public InputStream getInputStream() throws IOException {
return file;
}
@Override
public void transferTo(File file) throws IOException, IllegalStateException {
}
};
try {
WeMaterialFileVO weMaterialFileVO = this.uploadWeMaterialFile(file1, fileType);
return weMaterialFileVO.getMaterialUrl()+weMaterialFileVO.getMaterialName();
}catch (Exception e){
log.error("朋友圈资源获取失败:"+e.getMessage());
}
return null;
}
}