|
|
@@ -9,17 +9,15 @@ import cn.reghao.dfs.store.service.PutObjectService;
|
|
|
import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.dfs.store.model.po.ImageFile;
|
|
|
import cn.reghao.jutil.media.ImageOps;
|
|
|
+import cn.reghao.oss.api.constant.SupportedMedia;
|
|
|
+import cn.reghao.oss.api.constant.UploadChannel;
|
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
@@ -33,7 +31,6 @@ public class ImageFileProcessor {
|
|
|
private final FileStoreService fileStoreService;
|
|
|
private final ObjectNameService objectNameService;
|
|
|
private final PutObjectService putObjectService;
|
|
|
- private final Set<String> imageFormats = Set.of("jpg", "jpeg", "webp", "gif", "png");
|
|
|
|
|
|
public ImageFileProcessor(ImageRepository imageRepository, FileStoreService fileStoreService,
|
|
|
ObjectNameService objectNameService, PutObjectService putObjectService) {
|
|
|
@@ -43,125 +40,142 @@ public class ImageFileProcessor {
|
|
|
this.putObjectService = putObjectService;
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet processImage(ObjectResult objectResult) {
|
|
|
+ public UploadFileRet processImage(ObjectResult objectResult, int channelId) {
|
|
|
String objectName = objectResult.getObjectName();
|
|
|
String objectId = objectResult.getObjectId();
|
|
|
String imageFileId = objectId;
|
|
|
String objectUrl = objectNameService.getObjectUrl(objectName);
|
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
|
if (duplicate) {
|
|
|
- String dupObjectId = objectResult.getDupObjectId();
|
|
|
- ImageFile imageFile = imageRepository.getImageFile(dupObjectId);
|
|
|
- ImageFile imageFile1 = new ImageFile(imageFileId, objectId, objectUrl, imageFile);
|
|
|
- imageRepository.saveImageFiles(List.of(imageFile1));
|
|
|
- return new UploadFileRet(objectId, null);
|
|
|
+ return processDuplicate(objectResult,channelId);
|
|
|
}
|
|
|
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
- String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
+ File file = new File(absolutePath);
|
|
|
+ String format = ImageOps.getFormat(file);
|
|
|
+ if (!SupportedMedia.imageFormats.contains(format)) {
|
|
|
+ // 在 ChannelValidateService#validateImage 中已做过判断, 理论上不会执行这行代码
|
|
|
+ log.error("不支持 {} 图片格式", format);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
- if (imageFormats.contains(format)) {
|
|
|
- ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
- int width = size.getWidth();
|
|
|
- int height = size.getHeight();
|
|
|
-
|
|
|
- ImageFile imageFile = new ImageFile(imageFileId, objectId, format, objectUrl, width, height);
|
|
|
- imageRepository.saveImageFiles(List.of(imageFile));
|
|
|
- return new UploadFileRet(objectId, null);
|
|
|
- } else {
|
|
|
- log.info("不支持 {} 格式的文件", format);
|
|
|
+ ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
+ int width = size.getWidth();
|
|
|
+ int height = size.getHeight();
|
|
|
+ List<ImageFile> list = new ArrayList<>();
|
|
|
+ list.add(new ImageFile(imageFileId, objectId, format, objectUrl, width, height));
|
|
|
+ if (channelId == UploadChannel.photo.getCode()) {
|
|
|
+ ImageFile imageFile = getConvertedImageFile(objectResult, "webp", width, height);
|
|
|
+ if (imageFile != null) {
|
|
|
+ list.add(imageFile);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ imageRepository.saveImageFiles(list);
|
|
|
+ return new UploadFileRet(objectId, null);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet processPhoto(ObjectResult objectResult) {
|
|
|
+ private UploadFileRet processDuplicate(ObjectResult objectResult, int channelId) {
|
|
|
String objectName = objectResult.getObjectName();
|
|
|
String objectId = objectResult.getObjectId();
|
|
|
String imageFileId = objectId;
|
|
|
String objectUrl = objectNameService.getObjectUrl(objectName);
|
|
|
- boolean duplicate = objectResult.isDuplicate();
|
|
|
- if (duplicate) {
|
|
|
- String dupObjectId = objectResult.getDupObjectId();
|
|
|
- ImageFile imageFile = imageRepository.getImageFile(dupObjectId);
|
|
|
- ImageFile imageFile1 = new ImageFile(imageFileId, objectId, objectUrl, imageFile);
|
|
|
- imageRepository.saveImageFiles(List.of(imageFile1));
|
|
|
- return new UploadFileRet(objectId, null);
|
|
|
+ String dupObjectId = objectResult.getDupObjectId();
|
|
|
+ List<ImageFile> imageFiles = imageRepository.findImageFiles(dupObjectId);
|
|
|
+ ImageFile imageFile = imageFiles.get(0);
|
|
|
+ ImageFile imageFile1 = new ImageFile(imageFileId, objectId, objectUrl, imageFile);
|
|
|
+ List<ImageFile> list = new ArrayList<>();
|
|
|
+ list.add(imageFile1);
|
|
|
+
|
|
|
+ if (imageFiles.size() > 1) {
|
|
|
+ for (int i = 1; i < imageFiles.size(); i++) {
|
|
|
+ ImageFile imageFile2 = imageFiles.get(i);
|
|
|
+ ObjectResult objectResult1 = putObjectService.copyFromObjectId(imageFile2.getObjectId());
|
|
|
+ String objectId1 = objectResult1.getObjectId();
|
|
|
+ String objectUrl1 = objectNameService.getObjectUrl(objectResult1.getObjectName());
|
|
|
+ list.add(new ImageFile(imageFileId, objectId1, objectUrl1, imageFile2));
|
|
|
+ }
|
|
|
+ } else if (imageFiles.size() == 1 && channelId == UploadChannel.photo.getCode()) {
|
|
|
+ int width = imageFile.getWidth();
|
|
|
+ int height = imageFile.getHeight();
|
|
|
+ ImageFile imageFile2 = getConvertedImageFile(objectResult, "webp", width, height);
|
|
|
+ if (imageFile2 != null) {
|
|
|
+ list.add(imageFile2);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ imageRepository.saveImageFiles(list);
|
|
|
+ return new UploadFileRet(objectId, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ImageFile getConvertedImageFile(ObjectResult objectResult, String format, int width, int height) {
|
|
|
+ String imageFileId = objectResult.getObjectId();
|
|
|
+ String originalObjectName = objectResult.getObjectName();
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
- String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
- try {
|
|
|
- if (imageFormats.contains(format)) {
|
|
|
- ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
- int width = size.getWidth();
|
|
|
- int height = size.getHeight();
|
|
|
-
|
|
|
- List<ImageFile> imageFiles = new ArrayList<>();
|
|
|
- imageFiles.add(new ImageFile(imageFileId, objectId, format, objectUrl, width, height));
|
|
|
- if (!"webp".equals(format)) {
|
|
|
- ObjectResult webpResult = getWebpObject(objectResult);
|
|
|
- String webpObjectName = webpResult.getObjectName();
|
|
|
- String webpObjectId = webpResult.getObjectId();
|
|
|
- String webpUrl = objectNameService.getObjectUrl(webpObjectName);
|
|
|
- imageFiles.add(new ImageFile(imageFileId, webpObjectId, "webp", webpUrl, width, height));
|
|
|
- }
|
|
|
+ File srcFile = new File(absolutePath);
|
|
|
|
|
|
- imageRepository.saveImageFiles(imageFiles);
|
|
|
- return new UploadFileRet(objectId, null);
|
|
|
+ String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ String destPath = fileStoreService.genFilePath(contentId, srcFile.length(), "."+format);
|
|
|
+ File destFile = new File(destPath);
|
|
|
+ try {
|
|
|
+ if ("jpeg".equals(format)) {
|
|
|
+ ImageOps.convert2jpeg(srcFile, destFile);
|
|
|
+ } else if ("webp".equals(format)) {
|
|
|
+ ImageOps.convert2webp(srcFile, destFile);
|
|
|
} else {
|
|
|
- log.info("不支持 {} 格式的文件", format);
|
|
|
+ ImageOps.convert2thumbnail(srcFile, destFile, width, height);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (destFile.exists()) {
|
|
|
+ ObjectResult objectResult1 = saveImage(originalObjectName, contentId, "."+format, destFile);
|
|
|
+ String objectName1 = objectResult1.getObjectName();
|
|
|
+ String objectId1 = objectResult1.getObjectId();
|
|
|
+ String objectUrl1 = objectNameService.getObjectUrl(objectName1);
|
|
|
+ return new ImageFile(imageFileId, objectId1, format, objectUrl1, width, height);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private ObjectResult getJpegObject(ObjectResult objectResult, String format) throws Exception {
|
|
|
+ private ImageFile getThumbnailFile(ObjectResult objectResult, int width, int height) {
|
|
|
+ String imageFileId = objectResult.getObjectId();
|
|
|
String originalObjectName = objectResult.getObjectName();
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
- ObjectResult objectResult1;
|
|
|
- if (imageFormats.contains(format)) {
|
|
|
- String jpegObjectName = objectResult.getObjectName();
|
|
|
- objectResult1 = putObjectService.copyObject(jpegObjectName);
|
|
|
- } else {
|
|
|
- byte[] bytes = ImageOps.convert2jpg(new File(absolutePath));
|
|
|
- objectResult1 = saveImage(originalObjectName, bytes, ".jpeg");
|
|
|
- }
|
|
|
-
|
|
|
- return objectResult1;
|
|
|
- }
|
|
|
-
|
|
|
- private ObjectResult getWebpObject(ObjectResult objectResult) throws Exception {
|
|
|
- String objectName = objectResult.getObjectName();
|
|
|
- String absolutePath = objectResult.getAbsolutePath();
|
|
|
- BufferedImage bi = ImageIO.read(new File(absolutePath));
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(bi, "webp", baos);
|
|
|
- return saveImage(objectName, baos.toByteArray(), ".webp");
|
|
|
- }
|
|
|
+ File srcFile = new File(absolutePath);
|
|
|
|
|
|
- private ObjectResult getThumbnailObject(ObjectResult objectResult) throws Exception {
|
|
|
- String originalObjectName = objectResult.getObjectName();
|
|
|
- String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ //int width = 480;
|
|
|
+ //int height = 360;
|
|
|
+ String format = "jpeg";
|
|
|
+ String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ String destPath = fileStoreService.genFilePath(contentId, srcFile.length(), "."+format);
|
|
|
+ File destFile = new File(destPath);
|
|
|
+ try {
|
|
|
+ ImageOps.convert2thumbnail(srcFile, destFile, width, height);
|
|
|
+ if (destFile.exists()) {
|
|
|
+ ObjectResult objectResult1 = saveImage(originalObjectName, contentId, "."+format, destFile);
|
|
|
+ String objectName1 = objectResult1.getObjectName();
|
|
|
+ String objectId1 = objectResult1.getObjectId();
|
|
|
+ String objectUrl1 = objectNameService.getObjectUrl(objectName1);
|
|
|
+ return new ImageFile(imageFileId, objectId1, format, objectUrl1, width, height);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
|
|
|
- int width = 480;
|
|
|
- int height = 360;
|
|
|
- BufferedImage bi = Thumbnails.of(absolutePath)
|
|
|
- .size(width, height)
|
|
|
- .asBufferedImage();
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(bi, "jpeg", baos);
|
|
|
- return saveImage(originalObjectName, baos.toByteArray(), ".jpeg");
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- private ObjectResult saveImage(String objectName, byte[] bytes, String suffix) throws Exception {
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(objectName, suffix);
|
|
|
- String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
- File savedFile = fileStoreService.saveFile(bytes, contentId);
|
|
|
+ private ObjectResult saveImage(String originalObjectName, String contentId, String suffix, File savedFile)
|
|
|
+ throws Exception {
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(originalObjectName, suffix);
|
|
|
String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
|
|
|
return putObjectService.putObject(objectProp, contentId, savedFile, "", sha256sum);
|
|
|
}
|