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
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.util.AliyunOSSHelper;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.CommunityConstant;
import com.tanpu.community.api.enums.FileTypeEnum;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.dao.mapper.community.FileRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@Slf4j
@Service
public class OSSFileService {
@Value("${aliyun.oss.endpoint}")
public String endpoint;
@Value("${aliyun.oss.accessId}")
public String accessId;
@Value("${aliyun.oss.accessSK}")
public String accessSK;
@Value("${aliyun.oss.bucketName}")
public String bucketName;
private AliyunOSSHelper ossHelper;
@Resource
private FileRecordMapper fileRecordMapper;
@Autowired
private UuidGenHelper uuidGenHelper;
@PostConstruct
public void init() {
ossHelper = AliyunOSSHelper.build(endpoint, accessId, accessSK);
}
@Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName, String dirPrefix) {
String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
return uploadFile(data, fileName, fileSuffix, dirPrefix);
}
@Transactional
public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix, String dirPrefix) {
String[] arr = StringUtils.split(fileName, ".");
String suffix = arr[arr.length - 1];
//上传
String id = uuidGenHelper.getUuidStr();
String key = CommunityConstant.OSS_PREFIX_FOLDER + dirPrefix + id + "." + suffix;
ossHelper.writeFile(bucketName, key, data, fileSuffix);
//落库
FileRecordEntity record = new FileRecordEntity();
record.setFileId(uuidGenHelper.getUuidStr());
record.setDeleteTag(BizStatus.DeleteTag.tag_init);
record.setFileOssKey(key);
record.setFileName(fileName);
record.setFileId(id);
record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key));
record.setFileType(FileTypeEnum.IMAGE.getCode());
// todo 上传非文件类型
HashMap<String, Integer> attr = getStringIntegerHashMap(data, "webp".equals(suffix));
record.setExtInfo(JsonUtil.toJson(attr));
fileRecordMapper.insert(record);
return record;
}
public void uploadFileNoRecord(byte[] data, String fileName, String fileSuffix, String dirPrefix) {
String[] arr = StringUtils.split(fileName, ".");
String suffix = arr[arr.length - 1];
//上传
String key = CommunityConstant.OSS_PREFIX_FOLDER + dirPrefix + suffix;
ossHelper.writeFile(bucketName, key, data, fileSuffix);
}
private HashMap<String, Integer> getStringIntegerHashMap(byte[] data, boolean isWebp) {
ByteArrayInputStream bins = new ByteArrayInputStream(data);
BufferedImage bi = null;
try {
bi = ImageIO.read(bins);
} catch (Exception e) {
e.printStackTrace();
}
HashMap<String, Integer> attr = new HashMap<>();
if (bi != null) {
attr.put("width", bi.getWidth());
attr.put("height", bi.getHeight());
}
if (isWebp) {
// webp格式图片读取宽高
byte[] bytes = Arrays.copyOf(data, 30);
int width = ((int) bytes[27] & 0xff) << 8 | ((int) bytes[26] & 0xff);
int height = ((int) bytes[29] & 0xff) << 8 | ((int) bytes[28] & 0xff);
attr.put("width", width);
attr.put("height", height);
}
return attr;
}
public FileRecordEntity queryById(String fileId) {
return fileRecordMapper.selectOne(new LambdaQueryWrapper<FileRecordEntity>()
.eq(FileRecordEntity::getFileId, fileId));
}
public List<FileRecordEntity> queryByIds(List<String> fileIds) {
return fileRecordMapper.selectList(new LambdaQueryWrapper<FileRecordEntity>()
.in(FileRecordEntity::getFileId, fileIds));
}
public FileRecordEntity queryByOssKey(String fileKey) {
return fileRecordMapper.selectOne(new LambdaQueryWrapper<FileRecordEntity>().eq(FileRecordEntity::getFileOssKey, fileKey));
}
public void update(FileRecordEntity fileRecordEntity) {
fileRecordMapper.updateById(fileRecordEntity);
}
}