|
|
@@ -40,7 +40,7 @@ public class ImageFileProcessor {
|
|
|
this.putObjectService = putObjectService;
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet processImage(ObjectResult objectResult, int channelId) {
|
|
|
+ public UploadFileRet processImage(ObjectResult objectResult, int channelId) throws Exception {
|
|
|
String objectName = objectResult.getObjectName();
|
|
|
String objectId = objectResult.getObjectId();
|
|
|
String imageFileId = objectId;
|
|
|
@@ -59,28 +59,21 @@ public class ImageFileProcessor {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- 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() || channelId == UploadChannel.disk.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();
|
|
|
+ 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() || channelId == UploadChannel.disk.getCode()) {
|
|
|
+ ImageFile imageFile = getConvertedImageFile(objectResult, "webp", width, height);
|
|
|
+ list.add(imageFile);
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
+ imageRepository.saveImageFiles(list);
|
|
|
+ return new UploadFileRet(objectId, null);
|
|
|
}
|
|
|
|
|
|
- private UploadFileRet processDuplicate(ObjectResult objectResult, int channelId) {
|
|
|
+ private UploadFileRet processDuplicate(ObjectResult objectResult, int channelId) throws Exception {
|
|
|
String objectName = objectResult.getObjectName();
|
|
|
String objectId = objectResult.getObjectId();
|
|
|
String imageFileId = objectId;
|
|
|
@@ -105,9 +98,7 @@ public class ImageFileProcessor {
|
|
|
int width = imageFile.getWidth();
|
|
|
int height = imageFile.getHeight();
|
|
|
ImageFile imageFile2 = getConvertedImageFile(objectResult, "webp", width, height);
|
|
|
- if (imageFile2 != null) {
|
|
|
- list.add(imageFile2);
|
|
|
- }
|
|
|
+ list.add(imageFile2);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -115,16 +106,23 @@ public class ImageFileProcessor {
|
|
|
return new UploadFileRet(objectId, null);
|
|
|
}
|
|
|
|
|
|
- private ImageFile getConvertedImageFile(ObjectResult objectResult, String format, int width, int height) {
|
|
|
+ private ImageFile getConvertedImageFile(ObjectResult objectResult, String format, int width, int height) throws Exception {
|
|
|
String imageFileId = objectResult.getObjectId();
|
|
|
String originalObjectName = objectResult.getObjectName();
|
|
|
String absolutePath = objectResult.getAbsolutePath();
|
|
|
File srcFile = new File(absolutePath);
|
|
|
-
|
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
String destPath = fileStoreService.genFilePath(contentId, srcFile.length(), "."+format);
|
|
|
File destFile = new File(destPath);
|
|
|
- try {
|
|
|
+
|
|
|
+ String srcFormat = ImageOps.getFormat(srcFile);
|
|
|
+ if (srcFormat.equals("png")) {
|
|
|
+ if ("jpeg".equals(format) || "webp".equals(format)) {
|
|
|
+ ImageOps.convertPng(srcFile, destFile, format);
|
|
|
+ } else {
|
|
|
+ ImageOps.convert2thumbnail(srcFile, destFile, width, height);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
if ("jpeg".equals(format)) {
|
|
|
ImageOps.convert2jpeg(srcFile, destFile);
|
|
|
} else if ("webp".equals(format)) {
|
|
|
@@ -132,19 +130,17 @@ public class ImageFileProcessor {
|
|
|
} else {
|
|
|
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;
|
|
|
+ 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);
|
|
|
+ } else {
|
|
|
+ throw new Exception("image conversion failed");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private ImageFile getThumbnailFile(ObjectResult objectResult, int width, int height) {
|