|
@@ -1,6 +1,7 @@
|
|
|
package cn.reghao.dfs.store.task;
|
|
package cn.reghao.dfs.store.task;
|
|
|
|
|
|
|
|
import cn.reghao.dfs.store.db.repository.MediaRepository;
|
|
import cn.reghao.dfs.store.db.repository.MediaRepository;
|
|
|
|
|
+import cn.reghao.dfs.store.model.po.ImageUrl;
|
|
|
import cn.reghao.dfs.store.model.vo.ObjectProp;
|
|
import cn.reghao.dfs.store.model.vo.ObjectProp;
|
|
|
import cn.reghao.dfs.store.model.vo.ObjectResult;
|
|
import cn.reghao.dfs.store.model.vo.ObjectResult;
|
|
|
import cn.reghao.dfs.store.service.FileStoreService;
|
|
import cn.reghao.dfs.store.service.FileStoreService;
|
|
@@ -10,12 +11,15 @@ import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.dfs.store.model.po.ImageFile;
|
|
import cn.reghao.dfs.store.model.po.ImageFile;
|
|
|
import cn.reghao.jutil.media.ImageOps;
|
|
import cn.reghao.jutil.media.ImageOps;
|
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import net.coobird.thumbnailator.Thumbnails;
|
|
import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
|
|
|
|
|
@@ -23,6 +27,7 @@ import java.util.UUID;
|
|
|
* @author reghao
|
|
* @author reghao
|
|
|
* @date 2023-06-11 01:29:47
|
|
* @date 2023-06-11 01:29:47
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
public class ImageFileProcessor {
|
|
public class ImageFileProcessor {
|
|
|
private final MediaRepository mediaRepository;
|
|
private final MediaRepository mediaRepository;
|
|
@@ -40,38 +45,47 @@ public class ImageFileProcessor {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public UploadFileRet processImage(ObjectResult objectResult) {
|
|
public UploadFileRet processImage(ObjectResult objectResult) {
|
|
|
- String originalObjectId = objectResult.getObjectId();
|
|
|
|
|
|
|
+ String objectName = objectResult.getObjectName();
|
|
|
|
|
+ String objectId = objectResult.getObjectId();
|
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
|
if (duplicate) {
|
|
if (duplicate) {
|
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
|
ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
|
|
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 width = imageFile.getWidth();
|
|
|
int height = imageFile.getHeight();
|
|
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);
|
|
|
|
|
|
|
+ ImageFile imageFile1 = new ImageFile(objectId, width, height);
|
|
|
|
|
+
|
|
|
|
|
+ List<ImageUrl> imageUrls = mediaRepository.findImageUrls(dupObjectId);
|
|
|
|
|
+ if (imageUrls.size() > 1) {
|
|
|
|
|
+ log.info("{} 文件经过转码, 暂不处理", objectName);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ImageUrl imageUrl = imageUrls.get(0);
|
|
|
|
|
+ String format = imageUrl.getFormat();
|
|
|
|
|
+ String url = objectNameService.getObjectUrl(objectName);
|
|
|
|
|
+ ImageUrl imageUrl1 = new ImageUrl(objectId, format, objectId, url, width, height);
|
|
|
|
|
+
|
|
|
|
|
+ mediaRepository.saveImageFile(imageFile1, List.of(imageUrl1));
|
|
|
|
|
+ return new UploadFileRet(objectId, url);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
String format = ImageOps.getFormat(new File(absolutePath));
|
|
String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
try {
|
|
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);
|
|
|
|
|
|
|
+ if (imageFormats.contains(format)) {
|
|
|
|
|
+ ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
|
|
+ int width = size.getWidth();
|
|
|
|
|
+ int height = size.getHeight();
|
|
|
|
|
+ String url = objectNameService.getObjectUrl(objectName);
|
|
|
|
|
+
|
|
|
|
|
+ ImageFile imageFile = new ImageFile(objectId, width, height);
|
|
|
|
|
+ ImageUrl imageUrl = new ImageUrl(objectId, format, objectId, url, width, height);
|
|
|
|
|
+ mediaRepository.saveImageFile(imageFile, List.of(imageUrl));
|
|
|
|
|
+ return new UploadFileRet(objectId, url);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("不支持 {} 格式的文件", format);
|
|
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
@@ -79,48 +93,65 @@ public class ImageFileProcessor {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public UploadFileRet processPhoto(ObjectResult objectResult) {
|
|
public UploadFileRet processPhoto(ObjectResult objectResult) {
|
|
|
- String originalObjectId = objectResult.getObjectId();
|
|
|
|
|
|
|
+ String objectName = objectResult.getObjectName();
|
|
|
|
|
+ String objectId = objectResult.getObjectId();
|
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
|
if (duplicate) {
|
|
if (duplicate) {
|
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
|
ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
|
|
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 width = imageFile.getWidth();
|
|
|
int height = imageFile.getHeight();
|
|
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(originalObjectId, jpegUrl);
|
|
|
|
|
|
|
+ ImageFile imageFile1 = new ImageFile(objectId, width, height);
|
|
|
|
|
+ List<ImageUrl> list = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ List<ImageUrl> imageUrls = mediaRepository.findImageUrls(dupObjectId);
|
|
|
|
|
+ if (imageUrls.size() > 1) {
|
|
|
|
|
+ ImageUrl imageUrl = imageUrls.get(1);
|
|
|
|
|
+ ObjectResult objectResult1 = getCopiedObject(imageUrl.getUrl());
|
|
|
|
|
+ String objectName1 = objectResult1.getObjectName();
|
|
|
|
|
+ String objectId1 = objectResult1.getObjectId();
|
|
|
|
|
+
|
|
|
|
|
+ String format = imageUrl.getFormat();
|
|
|
|
|
+ String url1 = objectNameService.getObjectUrl(objectName1);
|
|
|
|
|
+ ImageUrl imageUrl2 = new ImageUrl(objectId, format, objectId1, url1, width, height);
|
|
|
|
|
+ list.add(imageUrl2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ImageUrl imageUrl = imageUrls.get(0);
|
|
|
|
|
+ String format = imageUrl.getFormat();
|
|
|
|
|
+ String url = objectNameService.getObjectUrl(objectName);
|
|
|
|
|
+ ImageUrl imageUrl1 = new ImageUrl(objectId, format, objectId, url, width, height);
|
|
|
|
|
+ list.add(imageUrl1);
|
|
|
|
|
+
|
|
|
|
|
+ mediaRepository.saveImageFile(imageFile1, list);
|
|
|
|
|
+ return new UploadFileRet(objectId, url);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
String format = ImageOps.getFormat(new File(absolutePath));
|
|
String format = ImageOps.getFormat(new File(absolutePath));
|
|
|
try {
|
|
try {
|
|
|
- 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();
|
|
|
|
|
- int height = size.getHeight();
|
|
|
|
|
- boolean horizontal = width > height;
|
|
|
|
|
- ImageFile imageFile = new ImageFile(originalObjectId, width, height, horizontal,
|
|
|
|
|
- jpegObjectId, jpegUrl, webpObjectId, webpUrl);
|
|
|
|
|
- mediaRepository.saveImageFile(imageFile);
|
|
|
|
|
- return new UploadFileRet(originalObjectId, jpegUrl);
|
|
|
|
|
|
|
+ if (imageFormats.contains(format)) {
|
|
|
|
|
+ ImageOps.Size size = ImageOps.info(new File(absolutePath));
|
|
|
|
|
+ int width = size.getWidth();
|
|
|
|
|
+ int height = size.getHeight();
|
|
|
|
|
+ String url = objectNameService.getObjectUrl(objectName);
|
|
|
|
|
+
|
|
|
|
|
+ ImageFile imageFile = new ImageFile(objectId, width, height);
|
|
|
|
|
+ List<ImageUrl> imageUrls = new ArrayList<>();
|
|
|
|
|
+ imageUrls.add(new ImageUrl(objectId, format, objectId, url, width, height));
|
|
|
|
|
+ if (!"webp".equals(format)) {
|
|
|
|
|
+ ObjectResult webpResult = getWebpObject(objectResult);
|
|
|
|
|
+ String webpObjectName = webpResult.getObjectName();
|
|
|
|
|
+ String webpObjectId = webpResult.getObjectId();
|
|
|
|
|
+ String webpUrl = objectNameService.getObjectUrl(webpObjectName);
|
|
|
|
|
+ imageUrls.add(new ImageUrl(objectId, "webp", webpObjectId, webpUrl, width, height));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mediaRepository.saveImageFile(imageFile, imageUrls);
|
|
|
|
|
+ return new UploadFileRet(objectId, url);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("不支持 {} 格式的文件", format);
|
|
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
@@ -147,21 +178,13 @@ public class ImageFileProcessor {
|
|
|
return objectResult1;
|
|
return objectResult1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private ObjectResult getWebpObject(ObjectResult objectResult, String format) throws Exception {
|
|
|
|
|
- String originalObjectName = objectResult.getObjectName();
|
|
|
|
|
|
|
+ private ObjectResult getWebpObject(ObjectResult objectResult) throws Exception {
|
|
|
|
|
+ String objectName = objectResult.getObjectName();
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
- ObjectResult objectResult1;
|
|
|
|
|
- if (format.equals("webp")) {
|
|
|
|
|
- String webpObjectName = objectResult.getObjectName();
|
|
|
|
|
- objectResult1 = putObjectService.copyObject(webpObjectName);
|
|
|
|
|
- } 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;
|
|
|
|
|
|
|
+ BufferedImage bi = ImageIO.read(new File(absolutePath));
|
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
+ ImageIO.write(bi, "webp", baos);
|
|
|
|
|
+ return saveImage(objectName, baos.toByteArray(), ".webp");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private ObjectResult getThumbnailObject(ObjectResult objectResult) throws Exception {
|
|
private ObjectResult getThumbnailObject(ObjectResult objectResult) throws Exception {
|
|
@@ -178,8 +201,8 @@ public class ImageFileProcessor {
|
|
|
return saveImage(originalObjectName, baos.toByteArray(), ".jpeg");
|
|
return saveImage(originalObjectName, baos.toByteArray(), ".jpeg");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private ObjectResult saveImage(String originalObjectName, byte[] bytes, String suffix) throws Exception {
|
|
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(originalObjectName, suffix);
|
|
|
|
|
|
|
+ private ObjectResult saveImage(String objectName, byte[] bytes, String suffix) throws Exception {
|
|
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(objectName, suffix);
|
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
File savedFile = fileStoreService.saveFile(bytes, contentId);
|
|
File savedFile = fileStoreService.saveFile(bytes, contentId);
|
|
|
String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
|
|
String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
|