Merge pull request #353 from weiwensangsang/dev-local-1030

修复上传附件时选择ico文件会失败的问题
pull/376/head
Ryan Wang 2019-11-01 21:13:53 +08:00 committed by GitHub
commit 9a7608831e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -56,6 +56,7 @@ ext {
jgitVersion = '5.3.0.201903130848-r' jgitVersion = '5.3.0.201903130848-r'
flexmarkVersion = '0.42.12' flexmarkVersion = '0.42.12'
thumbnailatorVersion = '0.4.8' thumbnailatorVersion = '0.4.8'
image4jVersion = '0.7zensight1'
} }
dependencies { dependencies {
@ -96,7 +97,8 @@ dependencies {
implementation "com.vladsch.flexmark:flexmark-html-parser:$flexmarkVersion" implementation "com.vladsch.flexmark:flexmark-html-parser:$flexmarkVersion"
implementation "net.coobird:thumbnailator:$thumbnailatorVersion" implementation "net.coobird:thumbnailator:$thumbnailatorVersion"
implementation "net.sf.image4j:image4j:$image4jVersion"
runtimeOnly 'com.h2database:h2' runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java' runtimeOnly 'mysql:mysql-connector-java'
@ -106,4 +108,4 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
} }

View File

@ -17,13 +17,16 @@ import run.halo.app.utils.HaloUtils;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File;
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;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import net.sf.image4j.codec.ico.ICODecoder;
import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR; import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR;
@ -151,7 +154,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 = ImageIO.read(uploadPath.toFile()); BufferedImage originalImage = getImageFromFile(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());
@ -239,4 +242,14 @@ 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);
}
}
} }