|
|
@@ -9,12 +9,13 @@ 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.UploadChannel;
|
|
|
+import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
+import java.util.Set;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
@@ -27,6 +28,7 @@ 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(MediaRepository mediaRepository, FileStoreService fileStoreService,
|
|
|
ObjectNameService objectNameService, PutObjectService putObjectService) {
|
|
|
@@ -36,8 +38,8 @@ public class ImageFileProcessor {
|
|
|
this.putObjectService = putObjectService;
|
|
|
}
|
|
|
|
|
|
- public void process(ObjectResult objectResult) {
|
|
|
- String objectId = objectResult.getObjectId();
|
|
|
+ public UploadFileRet process(ObjectResult objectResult) {
|
|
|
+ String originalObjectId = objectResult.getObjectId();
|
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
|
if (duplicate) {
|
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
|
@@ -45,71 +47,76 @@ public class ImageFileProcessor {
|
|
|
|
|
|
String jpegUrl = imageFile.getJpegUrl();
|
|
|
String jpegObjectName = objectNameService.getObjectNameFromUrl(jpegUrl);
|
|
|
- ObjectResult objectResult2 = putObjectService.copyObject(jpegObjectName);
|
|
|
- String jpegUrl1 = objectResult2.getUploadFileRet().getUrl();
|
|
|
+ ObjectResult objectResult2 = putObjectService.copyObject(jpegObjectName, ".jpeg");
|
|
|
+ String jpegUrl1 = objectNameService.getObjectUrl(objectResult2.getObjectName());
|
|
|
|
|
|
String webpUrl = imageFile.getWebpUrl();
|
|
|
String webpObjectName = objectNameService.getObjectNameFromUrl(webpUrl);
|
|
|
- ObjectResult objectResult3 = putObjectService.copyObject(webpObjectName);
|
|
|
- String webpUrl1 = objectResult3.getUploadFileRet().getUrl();
|
|
|
+ ObjectResult objectResult3 = putObjectService.copyObject(webpObjectName, ".webp");
|
|
|
+ String webpUrl1 = objectNameService.getObjectUrl(objectResult3.getObjectName());
|
|
|
|
|
|
int width = imageFile.getWidth();
|
|
|
int height = imageFile.getHeight();
|
|
|
boolean horizontal = imageFile.getHorizontal();
|
|
|
|
|
|
- ImageFile imageFile1 = new ImageFile(objectId, width, height, horizontal, jpegUrl1, webpUrl1);
|
|
|
+ ImageFile imageFile1 = new ImageFile(originalObjectId, width, height, horizontal, jpegUrl1, webpUrl1);
|
|
|
mediaRepository.saveImageFile(imageFile1);
|
|
|
- return;
|
|
|
+ return new UploadFileRet(objectResult2.getObjectId(), jpegUrl1);
|
|
|
}
|
|
|
|
|
|
- String objectName = objectResult.getObjectName();
|
|
|
+ String originalObjectName = objectResult.getObjectName();
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
try {
|
|
|
- String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
+ ObjectResult objectResult1;
|
|
|
String jpegUrl;
|
|
|
String webpUrl;
|
|
|
- if (format == null) {
|
|
|
- jpegUrl = getJpegUrl(absolutePath);
|
|
|
- webpUrl = getWebpUrl(absolutePath);
|
|
|
- } else if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
|
|
|
- jpegUrl = objectNameService.getObjectUrl(objectName);
|
|
|
- webpUrl = getWebpUrl(absolutePath);
|
|
|
- } else if (format.equalsIgnoreCase("webp")) {
|
|
|
- jpegUrl = getJpegUrl(absolutePath);
|
|
|
- webpUrl = objectNameService.getObjectUrl(objectName);
|
|
|
+ String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
+ if (imageFormats.contains(format)) {
|
|
|
+ objectResult1 = putObjectService.copyObject(originalObjectName, "."+format);
|
|
|
+ jpegUrl = objectNameService.getObjectUrl(objectResult1.getObjectName());
|
|
|
} else {
|
|
|
- jpegUrl = getJpegUrl(absolutePath);
|
|
|
- webpUrl = getWebpUrl(absolutePath);
|
|
|
+ jpegUrl = getJpegUrl(originalObjectName, absolutePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (format.equalsIgnoreCase("webp")) {
|
|
|
+ objectResult1 = putObjectService.copyObject(originalObjectName, "."+format);
|
|
|
+ webpUrl = objectNameService.getObjectUrl(objectResult1.getObjectName());
|
|
|
+ } else {
|
|
|
+ webpUrl = getWebpUrl(originalObjectName, absolutePath);
|
|
|
}
|
|
|
|
|
|
ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
boolean horizontal = size.getWidth() > size.getHeight();
|
|
|
ImageFile imageFile =
|
|
|
- new ImageFile(objectId, size.getWidth(), size.getHeight(), horizontal, jpegUrl, webpUrl);
|
|
|
+ new ImageFile(originalObjectId, size.getWidth(), size.getHeight(), horizontal, jpegUrl, webpUrl);
|
|
|
mediaRepository.saveImageFile(imageFile);
|
|
|
+
|
|
|
+ return new UploadFileRet("objectResult1.getObjectId()", jpegUrl);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
+
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- private String getJpegUrl(String originalPath) throws Exception {
|
|
|
+ private String getJpegUrl(String originalObjectName, String originalPath) throws Exception {
|
|
|
byte[] bytes = ImageOps.convert2jpg(new File(originalPath));
|
|
|
- return saveImage(bytes, ".jpeg");
|
|
|
+ return saveImage(originalObjectName, bytes, ".jpeg");
|
|
|
}
|
|
|
|
|
|
- private String getWebpUrl(String originalPath) throws Exception {
|
|
|
+ private String getWebpUrl(String originalObjectName, String originalPath) throws Exception {
|
|
|
BufferedImage bi = ImageIO.read(new File(originalPath));
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
ImageIO.write(bi, "webp", baos);
|
|
|
- return saveImage(baos.toByteArray(), ".webp");
|
|
|
+ return saveImage(originalObjectName, baos.toByteArray(), ".webp");
|
|
|
}
|
|
|
|
|
|
- private String saveImage(byte[] bytes, String suffix) throws Exception {
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(UploadChannel.cover.getCode());
|
|
|
+ private String saveImage(String originalObjectName, byte[] bytes, String suffix) throws Exception {
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(originalObjectName, suffix);
|
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
File savedFile = fileStoreService.saveFile(bytes, contentId);
|
|
|
String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
|
|
|
ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, "", sha256sum);
|
|
|
- return objectResult.getUploadFileRet().getUrl();
|
|
|
+ return objectNameService.getObjectUrl(objectResult.getObjectName());
|
|
|
}
|
|
|
}
|