|
@@ -13,6 +13,7 @@ import cn.reghao.dfs.store.util.media.ImageOps;
|
|
|
import cn.reghao.jutil.tool.id.IdGenerator;
|
|
import cn.reghao.jutil.tool.id.IdGenerator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
@@ -39,9 +40,11 @@ public class ImageFileService {
|
|
|
this.idGenerator = new IdGenerator("image-file-id");
|
|
this.idGenerator = new IdGenerator("image-file-id");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ImageFileRet process(String fileId, PathUrl pathUrl) throws Exception {
|
|
|
|
|
|
|
+ public ImageFileRet process(UploadedFile uploadedFile) throws Exception {
|
|
|
|
|
+ String fileId = uploadedFile.getFileId();
|
|
|
|
|
+ PathUrl pathUrl = uploadedFile.getPathUrl();
|
|
|
String filePath = pathUrl.getAbsolutePath();
|
|
String filePath = pathUrl.getAbsolutePath();
|
|
|
- String url = pathUrl.getUrl();
|
|
|
|
|
|
|
+ String originalUrl = pathUrl.getUrl();
|
|
|
|
|
|
|
|
File originalFile = new File(filePath);
|
|
File originalFile = new File(filePath);
|
|
|
ImageOps.Size imgSize = ImageOps.info(originalFile);
|
|
ImageOps.Size imgSize = ImageOps.info(originalFile);
|
|
@@ -52,12 +55,32 @@ public class ImageFileService {
|
|
|
|
|
|
|
|
imageFileMapper.save(imageFile);
|
|
imageFileMapper.save(imageFile);
|
|
|
imageUrlMapper.save(imageUrl);
|
|
imageUrlMapper.save(imageUrl);
|
|
|
- return genThumbnail(imageFileId, filePath, url);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ long length = originalFile.length();
|
|
|
|
|
+ if (length < 1024*1024) {
|
|
|
|
|
+ return new ImageFileRet(uploadedFile.getUploadId(), imageFileId, pathUrl.getUrl(), originalUrl);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return genThumbnail(imageFileId, originalFile, originalUrl);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private ImageFileRet genThumbnail(String imageFileId, String originalPath, String originalUrl) throws Exception {
|
|
|
|
|
- BufferedImage bufferedImage = ImageIO.read(new FileInputStream(originalPath));
|
|
|
|
|
- BufferedImage bufferedImage1 = ImageOps.resize(bufferedImage, 4);
|
|
|
|
|
|
|
+ private ImageFileRet genThumbnail(String imageFileId, File originalFile, String originalUrl) throws Exception {
|
|
|
|
|
+ long length = originalFile.length();
|
|
|
|
|
+ int ratio;
|
|
|
|
|
+ if (length > 1024*1024 && length < 1024*1024*2) {
|
|
|
|
|
+ ratio = 2;
|
|
|
|
|
+ } else if (length > 1024*1024*2 && length < 1024*1024*4) {
|
|
|
|
|
+ ratio = 4;
|
|
|
|
|
+ } else if (length > 1024*1024*4 && length < 1024*1024*8) {
|
|
|
|
|
+ ratio = 8;
|
|
|
|
|
+ } else if (length > 1024*1024*8 && length < 1024*1024*16) {
|
|
|
|
|
+ ratio = 16;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ratio = 32;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BufferedImage bufferedImage = ImageIO.read(new FileInputStream(originalFile));
|
|
|
|
|
+ BufferedImage bufferedImage1 = ImageOps.resize(bufferedImage, ratio);
|
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
ImageIO.write(bufferedImage1, "jpg", baos);
|
|
ImageIO.write(bufferedImage1, "jpg", baos);
|
|
@@ -81,6 +104,7 @@ public class ImageFileService {
|
|
|
return new ImageFileRet(uploadId, imageFileId, pathUrl.getUrl(), originalUrl);
|
|
return new ImageFileRet(uploadId, imageFileId, pathUrl.getUrl(), originalUrl);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void saveImage(ImageFile imageFile, List<ImageUrl> imageUrls) {
|
|
public void saveImage(ImageFile imageFile, List<ImageUrl> imageUrls) {
|
|
|
if (!imageUrls.isEmpty()) {
|
|
if (!imageUrls.isEmpty()) {
|
|
|
imageFileMapper.save(imageFile);
|
|
imageFileMapper.save(imageFile);
|