mirror of https://github.com/halo-dev/halo
fix: #416
parent
304ad0e9f7
commit
267e184af1
|
@ -16,8 +16,8 @@ import run.halo.app.model.properties.AliOssProperties;
|
|||
import run.halo.app.model.support.UploadResult;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.utils.FilenameUtils;
|
||||
import run.halo.app.utils.ImageUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -82,10 +82,14 @@ public class AliOssFileHandler implements FileHandler {
|
|||
|
||||
// Handle thumbnail
|
||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||
uploadResult.setWidth(image.getWidth());
|
||||
uploadResult.setHeight(image.getHeight());
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||
uploadResult.setThumbPath(filePath);
|
||||
} else {
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
}
|
||||
}
|
||||
|
||||
return uploadResult;
|
||||
|
|
|
@ -17,8 +17,8 @@ import run.halo.app.model.properties.BaiduBosProperties;
|
|||
import run.halo.app.model.support.UploadResult;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.utils.FilenameUtils;
|
||||
import run.halo.app.utils.ImageUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -87,10 +87,14 @@ public class BaiduBosFileHandler implements FileHandler {
|
|||
|
||||
// Handle thumbnail
|
||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||
uploadResult.setWidth(image.getWidth());
|
||||
uploadResult.setHeight(image.getHeight());
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||
uploadResult.setThumbPath(filePath);
|
||||
} else {
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
}
|
||||
}
|
||||
|
||||
return uploadResult;
|
||||
|
|
|
@ -2,7 +2,6 @@ package run.halo.app.handler.file;
|
|||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import net.sf.image4j.codec.ico.ICODecoder;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
@ -15,10 +14,10 @@ import run.halo.app.model.support.UploadResult;
|
|||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.utils.FilenameUtils;
|
||||
import run.halo.app.utils.HaloUtils;
|
||||
import run.halo.app.utils.ImageUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
@ -150,7 +149,7 @@ public class LocalFileHandler implements FileHandler {
|
|||
Path thumbnailPath = Paths.get(workDir + thumbnailSubFilePath);
|
||||
|
||||
// Read as image
|
||||
BufferedImage originalImage = getImageFromFile(uploadPath.toFile(), extension);
|
||||
BufferedImage originalImage = ImageUtils.getImageFromFile(new FileInputStream(uploadPath.toFile()), extension);
|
||||
// Set width and height
|
||||
uploadResult.setWidth(originalImage.getWidth());
|
||||
uploadResult.setHeight(originalImage.getHeight());
|
||||
|
@ -237,15 +236,4 @@ public class LocalFileHandler implements FileHandler {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private BufferedImage getImageFromFile(File file, String extension) throws IOException {
|
||||
log.debug("Current File type is : [{}]", extension);
|
||||
|
||||
if ("ico".equals(extension)) {
|
||||
return ICODecoder.read(file).get(0);
|
||||
} else {
|
||||
return ImageIO.read(file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,8 +22,10 @@ import run.halo.app.model.support.QiNiuPutSet;
|
|||
import run.halo.app.model.support.UploadResult;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.utils.FilenameUtils;
|
||||
import run.halo.app.utils.ImageUtils;
|
||||
import run.halo.app.utils.JsonUtils;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
@ -114,7 +116,11 @@ public class QiniuOssFileHandler implements FileHandler {
|
|||
result.setSize(file.getSize());
|
||||
|
||||
if (isImageType(result.getMediaType())) {
|
||||
result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||
result.setThumbPath(filePath);
|
||||
} else {
|
||||
result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -76,9 +76,9 @@ public class SmmsFileHandler implements FileHandler {
|
|||
HttpHeaders headers = new HttpHeaders();
|
||||
// Set content type
|
||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
// Set user agent manually
|
||||
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
|
||||
headers.set(HttpHeaders.AUTHORIZATION, apiSecretToken);
|
||||
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
|
||||
|
||||
|
||||
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
|
||||
|
@ -89,7 +89,6 @@ public class SmmsFileHandler implements FileHandler {
|
|||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到 SM.MS 失败", e);
|
||||
}
|
||||
|
||||
body.add("ssl", false);
|
||||
body.add("format", "json");
|
||||
|
||||
HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
|
||||
|
@ -109,7 +108,7 @@ public class SmmsFileHandler implements FileHandler {
|
|||
// Check error
|
||||
if (!isResponseSuccessfully(smmsResponse)) {
|
||||
log.error("Smms response detail: [{}]", smmsResponse);
|
||||
throw new FileOperationException(smmsResponse == null ? "SM.MS 服务返回内容为空" : smmsResponse.getMsg()).setErrorData(smmsResponse);
|
||||
throw new FileOperationException(smmsResponse == null ? "SM.MS 服务返回内容为空" : smmsResponse.getMessage()).setErrorData(smmsResponse);
|
||||
}
|
||||
|
||||
// Get response data
|
||||
|
@ -180,7 +179,7 @@ public class SmmsFileHandler implements FileHandler {
|
|||
|
||||
private String code;
|
||||
|
||||
private String msg;
|
||||
private String message;
|
||||
|
||||
private SmmsResponseData data;
|
||||
|
||||
|
|
|
@ -20,8 +20,8 @@ import run.halo.app.model.properties.TencentCosProperties;
|
|||
import run.halo.app.model.support.UploadResult;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.utils.FilenameUtils;
|
||||
import run.halo.app.utils.ImageUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -100,10 +100,14 @@ public class TencentCosFileHandler implements FileHandler {
|
|||
|
||||
// Handle thumbnail
|
||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||
uploadResult.setWidth(image.getWidth());
|
||||
uploadResult.setHeight(image.getHeight());
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||
uploadResult.setThumbPath(filePath);
|
||||
} else {
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
}
|
||||
}
|
||||
|
||||
return uploadResult;
|
||||
|
|
|
@ -14,8 +14,8 @@ import run.halo.app.model.properties.UpOssProperties;
|
|||
import run.halo.app.model.support.UploadResult;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.utils.FilenameUtils;
|
||||
import run.halo.app.utils.ImageUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -86,10 +86,14 @@ public class UpOssFileHandler implements FileHandler {
|
|||
|
||||
// Handle thumbnail
|
||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||
uploadResult.setWidth(image.getWidth());
|
||||
uploadResult.setHeight(image.getHeight());
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
if(ImageUtils.EXTENSION_ICO.equals(extension)){
|
||||
uploadResult.setThumbPath(filePath);
|
||||
}else {
|
||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||
}
|
||||
}
|
||||
|
||||
return uploadResult;
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package run.halo.app.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.image4j.codec.ico.ICODecoder;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author ryanwang
|
||||
* @date 2019-12-10
|
||||
*/
|
||||
@Slf4j
|
||||
public class ImageUtils {
|
||||
|
||||
public static final String EXTENSION_ICO = "ico";
|
||||
|
||||
public static BufferedImage getImageFromFile(InputStream is, String extension) throws IOException {
|
||||
log.debug("Current File type is : [{}]", extension);
|
||||
|
||||
if (EXTENSION_ICO.equals(extension)) {
|
||||
return ICODecoder.read(is).get(0);
|
||||
} else {
|
||||
return ImageIO.read(is);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue