Skip to content

Commit

Permalink
Refactor generateThumbnail method.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Sep 9, 2019
1 parent f375c28 commit d41d3d8
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ bootJar {

ext {
ohMyEmailVersion = '0.0.4'
hutoolVersion = '4.5.0'
hutoolVersion = '4.6.3'
upyunSdkVersion = '4.0.1'
qiniuSdkVersion = '7.2.18'
aliyunSdkVersion = '3.4.2'
baiduSdkVersion = '0.10.36'
qcloudSdkVersion = '5.5.7'
thumbnailatorVersion = '0.4.8'
swaggerVersion = '2.9.2'
commonsLangVersion = '3.8.1'
httpclientVersion = '4.5.7'
Expand All @@ -74,7 +73,6 @@ dependencies {
implementation "com.aliyun.oss:aliyun-sdk-oss:$aliyunSdkVersion"
implementation "com.baidubce:bce-java-sdk:$baiduSdkVersion"
implementation "com.qcloud:cos_api:$qcloudSdkVersion"
implementation "net.coobird:thumbnailator:$thumbnailatorVersion"
implementation "io.springfox:springfox-swagger2:$swaggerVersion"
implementation "io.springfox:springfox-swagger-ui:$swaggerVersion"
implementation "org.apache.commons:commons-lang3:$commonsLangVersion"
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon Sep 09 12:27:59 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
17 changes: 12 additions & 5 deletions src/main/java/run/halo/app/handler/file/LocalFileHandler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package run.halo.app.handler.file;

import cn.hutool.core.img.ImgUtil;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -31,7 +31,8 @@
* Local file handler.
*
* @author johnniang
* @date 3/27/19
* @author ryanwang
* @date 2019-03-27
*/
@Slf4j
@Component
Expand All @@ -47,11 +48,13 @@ public class LocalFileHandler implements FileHandler {
/**
* Thumbnail width.
*/
@Deprecated
private final static int THUMB_WIDTH = 256;

/**
* Thumbnail height.
*/
@Deprecated
private final static int THUMB_HEIGHT = 256;

private final OptionService optionService;
Expand Down Expand Up @@ -135,8 +138,11 @@ public UploadResult upload(MultipartFile file) {
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSize(file.getSize());

// TODO refactor this: if image is svg ext. extension
boolean isSvg = "svg".equals(extension);

// Check file type
if (FileHandler.isImageType(uploadResult.getMediaType())) {
if (FileHandler.isImageType(uploadResult.getMediaType()) && !isSvg) {
// Upload a thumbnail
String thumbnailBasename = basename + THUMBNAIL_SUFFIX;
String thumbnailSubFilePath = subDir + thumbnailBasename + '.' + extension;
Expand All @@ -154,9 +160,10 @@ public UploadResult upload(MultipartFile file) {
// Set width and height
uploadResult.setWidth(image.getWidth());
uploadResult.setHeight(image.getHeight());

// Set thumb path
uploadResult.setThumbPath(thumbnailSubFilePath);
} else {
uploadResult.setThumbPath(subFilePath);
}

return uploadResult;
Expand Down Expand Up @@ -220,7 +227,7 @@ private void generateThumbnail(@NonNull Path imagePath, @NonNull Path thumbPath)
log.info("Generating thumbnail: [{}] for image: [{}]", thumbPath.getFileName(), imagePath.getFileName());

// Convert to thumbnail and copy the thumbnail
Thumbnails.of(imagePath.toFile()).size(THUMB_WIDTH, THUMB_HEIGHT).keepAspectRatio(true).toFile(thumbPath.toFile());
ImgUtil.scale(imagePath.toFile(), thumbPath.toFile(), 0.1f);
}

}
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/model/entity/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* @author johnniang
* @date 3/20/19
*/
@MappedSuperclass
@Data
@ToString
@MappedSuperclass
@EqualsAndHashCode
public class BaseEntity {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/model/entity/BasePost.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BasePost extends BaseEntity {
/**
* Post url.
*/
@Column(name = "url", columnDefinition = "varchar(255) not null")
@Column(name = "url", columnDefinition = "varchar(255) not null", unique = true)
private String url;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/model/entity/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Category extends BaseEntity {
/**
* Category slug name.
*/
@Column(name = "slug_name", columnDefinition = "varchar(50) not null")
@Column(name = "slug_name", columnDefinition = "varchar(50) not null", unique = true)
private String slugName;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/model/entity/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Tag extends BaseEntity {
/**
* Tag slug name.
*/
@Column(name = "slug_name", columnDefinition = "varchar(255) not null")
@Column(name = "slug_name", columnDefinition = "varchar(255) not null", unique = true)
private String slugName;

@Override
Expand Down

0 comments on commit d41d3d8

Please sign in to comment.