Commit 4a84cf03 authored by 刘基明's avatar 刘基明

图片格式支持webp

parent 3bbbcdb0
......@@ -21,6 +21,7 @@ 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;
......@@ -79,7 +80,8 @@ public class OSSFileService {
record.setFileId(id);
record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key));
record.setFileType(FileTypeEnum.IMAGE.getCode());
HashMap<String, Integer> attr = getStringIntegerHashMap(data);
// todo 上传非文件类型
HashMap<String, Integer> attr = getStringIntegerHashMap(data, "webp".equals(suffix));
record.setExtInfo(JsonUtil.toJson(attr));
fileRecordMapper.insert(record);
......@@ -95,7 +97,9 @@ public class OSSFileService {
ossHelper.writeFile(bucketName, key, data, fileSuffix);
}
private HashMap<String, Integer> getStringIntegerHashMap(byte[] data) {
private HashMap<String, Integer> getStringIntegerHashMap(byte[] data, boolean isWebp) {
ByteArrayInputStream bins = new ByteArrayInputStream(data);
BufferedImage bi = null;
try {
......@@ -104,26 +108,35 @@ public class OSSFileService {
e.printStackTrace();
}
HashMap<String, Integer> attr = new HashMap<>();
attr.put("width",bi.getWidth());
attr.put("height",bi.getHeight());
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));
.eq(FileRecordEntity::getFileId, fileId));
}
public List<FileRecordEntity> queryByIds(List<String> fileIds) {
return fileRecordMapper.selectList(new LambdaQueryWrapper<FileRecordEntity>()
.in(FileRecordEntity::getFileId,fileIds));
.in(FileRecordEntity::getFileId, fileIds));
}
public FileRecordEntity queryByOssKey(String fileKey) {
return fileRecordMapper.selectOne(new LambdaQueryWrapper<FileRecordEntity>().eq(FileRecordEntity::getFileOssKey,fileKey));
return fileRecordMapper.selectOne(new LambdaQueryWrapper<FileRecordEntity>().eq(FileRecordEntity::getFileOssKey, fileKey));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment