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

图片格式支持webp

parent 3bbbcdb0
...@@ -21,6 +21,7 @@ import javax.annotation.Resource; ...@@ -21,6 +21,7 @@ import javax.annotation.Resource;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -79,7 +80,8 @@ public class OSSFileService { ...@@ -79,7 +80,8 @@ public class OSSFileService {
record.setFileId(id); record.setFileId(id);
record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key)); record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key));
record.setFileType(FileTypeEnum.IMAGE.getCode()); 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)); record.setExtInfo(JsonUtil.toJson(attr));
fileRecordMapper.insert(record); fileRecordMapper.insert(record);
...@@ -95,7 +97,9 @@ public class OSSFileService { ...@@ -95,7 +97,9 @@ public class OSSFileService {
ossHelper.writeFile(bucketName, key, data, fileSuffix); 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); ByteArrayInputStream bins = new ByteArrayInputStream(data);
BufferedImage bi = null; BufferedImage bi = null;
try { try {
...@@ -104,26 +108,35 @@ public class OSSFileService { ...@@ -104,26 +108,35 @@ public class OSSFileService {
e.printStackTrace(); e.printStackTrace();
} }
HashMap<String, Integer> attr = new HashMap<>(); HashMap<String, Integer> attr = new HashMap<>();
attr.put("width",bi.getWidth()); if (bi != null) {
attr.put("height",bi.getHeight()); 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; return attr;
} }
public FileRecordEntity queryById(String fileId) { public FileRecordEntity queryById(String fileId) {
return fileRecordMapper.selectOne(new LambdaQueryWrapper<FileRecordEntity>() return fileRecordMapper.selectOne(new LambdaQueryWrapper<FileRecordEntity>()
.eq(FileRecordEntity::getFileId,fileId)); .eq(FileRecordEntity::getFileId, fileId));
} }
public List<FileRecordEntity> queryByIds(List<String> fileIds) { public List<FileRecordEntity> queryByIds(List<String> fileIds) {
return fileRecordMapper.selectList(new LambdaQueryWrapper<FileRecordEntity>() return fileRecordMapper.selectList(new LambdaQueryWrapper<FileRecordEntity>()
.in(FileRecordEntity::getFileId,fileIds)); .in(FileRecordEntity::getFileId, fileIds));
} }
public FileRecordEntity queryByOssKey(String fileKey) { 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