|
|
@@ -11,6 +11,7 @@ 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.rest.UploadFileRet;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
@@ -39,64 +40,128 @@ public class ImageFileProcessor {
|
|
|
this.putObjectService = putObjectService;
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet process(ObjectResult objectResult) {
|
|
|
+ public UploadFileRet processCover(ObjectResult objectResult) {
|
|
|
String originalObjectId = objectResult.getObjectId();
|
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
|
if (duplicate) {
|
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
|
ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
|
|
|
|
|
|
- String jpegObjectName = objectNameService.getObjectNameFromUrl(imageFile.getJpegUrl());
|
|
|
- String format = StringUtil.getSuffix(jpegObjectName);
|
|
|
- ObjectResult objectResult2 = putObjectService.copyObject(jpegObjectName, format);
|
|
|
- String jpegObjectId = objectResult2.getObjectId();
|
|
|
- String jpegUrl = objectNameService.getObjectUrl(objectResult2.getObjectName());
|
|
|
+ ObjectResult jpegResult = getCopiedObject(imageFile.getJpegUrl());
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
|
|
|
- String webpObjectName = objectNameService.getObjectNameFromUrl(imageFile.getWebpUrl());
|
|
|
- ObjectResult objectResult3 = putObjectService.copyObject(webpObjectName, ".webp");
|
|
|
- String webpObjectId = objectResult3.getObjectId();
|
|
|
- String webpUrl = objectNameService.getObjectUrl(objectResult3.getObjectName());
|
|
|
+ int width = imageFile.getWidth();
|
|
|
+ int height = imageFile.getHeight();
|
|
|
+ boolean horizontal = imageFile.getHorizontal();
|
|
|
+ ImageFile imageFile1 = new ImageFile(originalObjectId, width, height, horizontal, jpegObjectId, jpegUrl);
|
|
|
+ mediaRepository.saveImageFile(imageFile1);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
+ try {
|
|
|
+ ObjectResult jpegResult = getJpegObject(objectResult, format);
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
+ int width = size.getWidth();
|
|
|
+ int height = size.getHeight();
|
|
|
+ boolean horizontal = width > height;
|
|
|
+ ImageFile imageFile = new ImageFile(originalObjectId, width, height, horizontal, jpegObjectId, jpegUrl);
|
|
|
+ mediaRepository.saveImageFile(imageFile);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UploadFileRet processAvatar(ObjectResult objectResult) {
|
|
|
+ String originalObjectId = objectResult.getObjectId();
|
|
|
+ boolean duplicate = objectResult.isDuplicate();
|
|
|
+ if (duplicate) {
|
|
|
+ String dupObjectId = objectResult.getDupObjectId();
|
|
|
+ ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
|
|
|
+
|
|
|
+ ObjectResult jpegResult = getCopiedObject(imageFile.getJpegUrl());
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ ObjectResult thumbnailResult = getCopiedObject(imageFile.getThumbnailUrl());
|
|
|
+ String thumbnailObjectId = thumbnailResult.getObjectId();
|
|
|
+ String thumbnailUrl = objectNameService.getObjectUrl(thumbnailResult.getObjectName());
|
|
|
|
|
|
int width = imageFile.getWidth();
|
|
|
int height = imageFile.getHeight();
|
|
|
boolean horizontal = imageFile.getHorizontal();
|
|
|
+ ImageFile imageFile1 = new ImageFile(originalObjectId, jpegObjectId, jpegUrl, width, height, horizontal,
|
|
|
+ thumbnailObjectId, thumbnailUrl);
|
|
|
+ mediaRepository.saveImageFile(imageFile1);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
+ try {
|
|
|
+ ObjectResult jpegResult = getJpegObject(objectResult, format);
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ ObjectResult thumbnailResult = getThumbnailObject(objectResult);
|
|
|
+ String thumbnailObjectId = thumbnailResult.getObjectId();
|
|
|
+ String thumbnailUrl = objectNameService.getObjectUrl(thumbnailResult.getObjectName());
|
|
|
+
|
|
|
+ ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
+ int width = size.getWidth();
|
|
|
+ int height = size.getHeight();
|
|
|
+ boolean horizontal = width > height;
|
|
|
+ ImageFile imageFile = new ImageFile(originalObjectId, jpegObjectId, jpegUrl, width, height, horizontal,
|
|
|
+ thumbnailObjectId, thumbnailUrl);
|
|
|
+ mediaRepository.saveImageFile(imageFile);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UploadFileRet processPhoto(ObjectResult objectResult) {
|
|
|
+ String originalObjectId = objectResult.getObjectId();
|
|
|
+ boolean duplicate = objectResult.isDuplicate();
|
|
|
+ if (duplicate) {
|
|
|
+ String dupObjectId = objectResult.getDupObjectId();
|
|
|
+ ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
|
|
|
|
|
|
+ ObjectResult jpegResult = getCopiedObject(imageFile.getJpegUrl());
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ ObjectResult webpResult = getCopiedObject(imageFile.getWebpUrl());
|
|
|
+ String webpObjectId = webpResult.getObjectId();
|
|
|
+ String webpUrl = objectNameService.getObjectUrl(webpResult.getObjectName());
|
|
|
+
|
|
|
+ int width = imageFile.getWidth();
|
|
|
+ int height = imageFile.getHeight();
|
|
|
+ boolean horizontal = imageFile.getHorizontal();
|
|
|
ImageFile imageFile1 = new ImageFile(originalObjectId, width, height, horizontal,
|
|
|
jpegObjectId, jpegUrl, webpObjectId, webpUrl);
|
|
|
mediaRepository.saveImageFile(imageFile1);
|
|
|
- return new UploadFileRet(jpegObjectId, jpegUrl);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
}
|
|
|
|
|
|
- String originalObjectName = objectResult.getObjectName();
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
try {
|
|
|
- ObjectResult objectResult1;
|
|
|
- String jpegObjectId;
|
|
|
- String jpegUrl;
|
|
|
- String webpObjectId;
|
|
|
- String webpUrl;
|
|
|
- String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
- if (imageFormats.contains(format)) {
|
|
|
- String jpegObjectName = objectResult.getObjectName();
|
|
|
- objectResult1 = putObjectService.copyObject(jpegObjectName, "."+format);
|
|
|
- jpegObjectId = objectResult1.getObjectId();
|
|
|
- jpegUrl = objectNameService.getObjectUrl(objectResult1.getObjectName());
|
|
|
- } else {
|
|
|
- ObjectResult objectResult2 = getJpegObject(originalObjectName, absolutePath);
|
|
|
- jpegObjectId = objectResult2.getObjectId();
|
|
|
- jpegUrl = objectNameService.getObjectUrl(objectResult2.getObjectName());
|
|
|
- }
|
|
|
-
|
|
|
- if (format.equals("webp")) {
|
|
|
- String webpObjectName = objectResult.getObjectName();
|
|
|
- ObjectResult objectResult3 = putObjectService.copyObject(webpObjectName, ".webp");
|
|
|
- webpObjectId = objectResult3.getObjectId();
|
|
|
- webpUrl = objectNameService.getObjectUrl(objectResult3.getObjectName());
|
|
|
- } else {
|
|
|
- ObjectResult objectResult3 = getWebpObject(originalObjectName, absolutePath);
|
|
|
- webpObjectId = objectResult3.getObjectId();
|
|
|
- webpUrl = objectNameService.getObjectUrl(objectResult3.getObjectName());
|
|
|
- }
|
|
|
+ ObjectResult jpegResult = getJpegObject(objectResult, format);
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ ObjectResult webpResult = getWebpObject(objectResult, format);
|
|
|
+ String webpObjectId = webpResult.getObjectId();
|
|
|
+ String webpUrl = objectNameService.getObjectUrl(webpResult.getObjectName());
|
|
|
|
|
|
ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
int width = size.getWidth();
|
|
|
@@ -112,20 +177,95 @@ public class ImageFileProcessor {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private ObjectResult getJpegObject(String originalObjectName, String originalPath) throws Exception {
|
|
|
- byte[] bytes = ImageOps.convert2jpg(new File(originalPath));
|
|
|
- return saveImage(originalObjectName, bytes, ".jpeg");
|
|
|
+ public UploadFileRet processImage(ObjectResult objectResult) {
|
|
|
+ String originalObjectId = objectResult.getObjectId();
|
|
|
+ boolean duplicate = objectResult.isDuplicate();
|
|
|
+ if (duplicate) {
|
|
|
+ String dupObjectId = objectResult.getDupObjectId();
|
|
|
+ ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
|
|
|
+
|
|
|
+ ObjectResult jpegResult = getCopiedObject(imageFile.getJpegUrl());
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ int width = imageFile.getWidth();
|
|
|
+ int height = imageFile.getHeight();
|
|
|
+ boolean horizontal = imageFile.getHorizontal();
|
|
|
+ ImageFile imageFile1 = new ImageFile(originalObjectId, width, height, horizontal, jpegObjectId, jpegUrl);
|
|
|
+ mediaRepository.saveImageFile(imageFile1);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
+ try {
|
|
|
+ ObjectResult jpegResult = getJpegObject(objectResult, format);
|
|
|
+ String jpegObjectId = jpegResult.getObjectId();
|
|
|
+ String jpegUrl = objectNameService.getObjectUrl(jpegResult.getObjectName());
|
|
|
+
|
|
|
+ ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
+ int width = size.getWidth();
|
|
|
+ int height = size.getHeight();
|
|
|
+ boolean horizontal = width > height;
|
|
|
+ ImageFile imageFile = new ImageFile(originalObjectId, width, height, horizontal, jpegObjectId, jpegUrl);
|
|
|
+ mediaRepository.saveImageFile(imageFile);
|
|
|
+ return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- private ObjectResult getWebpObject(String originalObjectName, String originalPath) throws Exception {
|
|
|
- BufferedImage bi = ImageIO.read(new File(originalPath));
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(bi, "webp", baos);
|
|
|
- return saveImage(originalObjectName, baos.toByteArray(), ".webp");
|
|
|
+ private ObjectResult getCopiedObject(String url) {
|
|
|
+ String objectName = objectNameService.getObjectNameFromUrl(url);
|
|
|
+ String format = StringUtil.getSuffix(objectName);
|
|
|
+ return putObjectService.copyObject(objectName, format);
|
|
|
}
|
|
|
|
|
|
- private ObjectResult getThumbnailObject(String originalObjectName, String originalPath) throws Exception {
|
|
|
- return null;
|
|
|
+ private ObjectResult getJpegObject(ObjectResult objectResult, String format) throws Exception {
|
|
|
+ String originalObjectName = objectResult.getObjectName();
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ ObjectResult objectResult1;
|
|
|
+ if (imageFormats.contains(format)) {
|
|
|
+ String jpegObjectName = objectResult.getObjectName();
|
|
|
+ objectResult1 = putObjectService.copyObject(jpegObjectName, "."+format);
|
|
|
+ } else {
|
|
|
+ byte[] bytes = ImageOps.convert2jpg(new File(absolutePath));
|
|
|
+ objectResult1 = saveImage(originalObjectName, bytes, ".jpeg");
|
|
|
+ }
|
|
|
+
|
|
|
+ return objectResult1;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ObjectResult getWebpObject(ObjectResult objectResult, String format) throws Exception {
|
|
|
+ String originalObjectName = objectResult.getObjectName();
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
+ ObjectResult objectResult1;
|
|
|
+ if (format.equals("webp")) {
|
|
|
+ String webpObjectName = objectResult.getObjectName();
|
|
|
+ objectResult1 = putObjectService.copyObject(webpObjectName, ".webp");
|
|
|
+ } else {
|
|
|
+ BufferedImage bi = ImageIO.read(new File(absolutePath));
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(bi, "webp", baos);
|
|
|
+ objectResult1 = saveImage(originalObjectName, baos.toByteArray(), ".webp");
|
|
|
+ }
|
|
|
+
|
|
|
+ return objectResult1;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ObjectResult getThumbnailObject(ObjectResult objectResult) throws Exception {
|
|
|
+ String originalObjectName = objectResult.getObjectName();
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
+
|
|
|
+ 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");
|
|
|
}
|
|
|
|
|
|
private ObjectResult saveImage(String originalObjectName, byte[] bytes, String suffix) throws Exception {
|