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.model.support.UploadResult;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
|
import run.halo.app.utils.ImageUtils;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -82,11 +82,15 @@ public class AliOssFileHandler implements FileHandler {
|
||||||
|
|
||||||
// Handle thumbnail
|
// Handle thumbnail
|
||||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
|
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||||
|
uploadResult.setThumbPath(filePath);
|
||||||
|
} else {
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -17,8 +17,8 @@ import run.halo.app.model.properties.BaiduBosProperties;
|
||||||
import run.halo.app.model.support.UploadResult;
|
import run.halo.app.model.support.UploadResult;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
|
import run.halo.app.utils.ImageUtils;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -87,11 +87,15 @@ public class BaiduBosFileHandler implements FileHandler {
|
||||||
|
|
||||||
// Handle thumbnail
|
// Handle thumbnail
|
||||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
|
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||||
|
uploadResult.setThumbPath(filePath);
|
||||||
|
} else {
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package run.halo.app.handler.file;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.coobird.thumbnailator.Thumbnails;
|
import net.coobird.thumbnailator.Thumbnails;
|
||||||
import net.sf.image4j.codec.ico.ICODecoder;
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.Assert;
|
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.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
import run.halo.app.utils.HaloUtils;
|
import run.halo.app.utils.HaloUtils;
|
||||||
|
import run.halo.app.utils.ImageUtils;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -150,7 +149,7 @@ public class LocalFileHandler implements FileHandler {
|
||||||
Path thumbnailPath = Paths.get(workDir + thumbnailSubFilePath);
|
Path thumbnailPath = Paths.get(workDir + thumbnailSubFilePath);
|
||||||
|
|
||||||
// Read as image
|
// Read as image
|
||||||
BufferedImage originalImage = getImageFromFile(uploadPath.toFile(), extension);
|
BufferedImage originalImage = ImageUtils.getImageFromFile(new FileInputStream(uploadPath.toFile()), extension);
|
||||||
// Set width and height
|
// Set width and height
|
||||||
uploadResult.setWidth(originalImage.getWidth());
|
uploadResult.setWidth(originalImage.getWidth());
|
||||||
uploadResult.setHeight(originalImage.getHeight());
|
uploadResult.setHeight(originalImage.getHeight());
|
||||||
|
@ -237,15 +236,4 @@ public class LocalFileHandler implements FileHandler {
|
||||||
}
|
}
|
||||||
return result;
|
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.model.support.UploadResult;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
|
import run.halo.app.utils.ImageUtils;
|
||||||
import run.halo.app.utils.JsonUtils;
|
import run.halo.app.utils.JsonUtils;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
@ -114,8 +116,12 @@ public class QiniuOssFileHandler implements FileHandler {
|
||||||
result.setSize(file.getSize());
|
result.setSize(file.getSize());
|
||||||
|
|
||||||
if (isImageType(result.getMediaType())) {
|
if (isImageType(result.getMediaType())) {
|
||||||
|
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||||
|
result.setThumbPath(filePath);
|
||||||
|
} else {
|
||||||
result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -76,9 +76,9 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
// Set content type
|
// Set content type
|
||||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
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.AUTHORIZATION, apiSecretToken);
|
||||||
|
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
|
||||||
|
|
||||||
|
|
||||||
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
|
||||||
|
@ -89,7 +89,6 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到 SM.MS 失败", e);
|
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到 SM.MS 失败", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
body.add("ssl", false);
|
|
||||||
body.add("format", "json");
|
body.add("format", "json");
|
||||||
|
|
||||||
HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
|
HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
|
||||||
|
@ -109,7 +108,7 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
// Check error
|
// Check error
|
||||||
if (!isResponseSuccessfully(smmsResponse)) {
|
if (!isResponseSuccessfully(smmsResponse)) {
|
||||||
log.error("Smms response detail: [{}]", 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
|
// Get response data
|
||||||
|
@ -180,7 +179,7 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
|
|
||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
private String msg;
|
private String message;
|
||||||
|
|
||||||
private SmmsResponseData data;
|
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.model.support.UploadResult;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
|
import run.halo.app.utils.ImageUtils;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -100,11 +100,15 @@ public class TencentCosFileHandler implements FileHandler {
|
||||||
|
|
||||||
// Handle thumbnail
|
// Handle thumbnail
|
||||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
|
if (ImageUtils.EXTENSION_ICO.equals(extension)) {
|
||||||
|
uploadResult.setThumbPath(filePath);
|
||||||
|
} else {
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -14,8 +14,8 @@ import run.halo.app.model.properties.UpOssProperties;
|
||||||
import run.halo.app.model.support.UploadResult;
|
import run.halo.app.model.support.UploadResult;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
|
import run.halo.app.utils.ImageUtils;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -86,11 +86,15 @@ public class UpOssFileHandler implements FileHandler {
|
||||||
|
|
||||||
// Handle thumbnail
|
// Handle thumbnail
|
||||||
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
if (FileHandler.isImageType(uploadResult.getMediaType())) {
|
||||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
BufferedImage image = ImageUtils.getImageFromFile(file.getInputStream(), extension);
|
||||||
uploadResult.setWidth(image.getWidth());
|
uploadResult.setWidth(image.getWidth());
|
||||||
uploadResult.setHeight(image.getHeight());
|
uploadResult.setHeight(image.getHeight());
|
||||||
|
if(ImageUtils.EXTENSION_ICO.equals(extension)){
|
||||||
|
uploadResult.setThumbPath(filePath);
|
||||||
|
}else {
|
||||||
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
uploadResult.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -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