mirror of https://github.com/halo-dev/halo
Complete uploadToYpYun service
parent
ab58e800c4
commit
b85152f8a7
|
@ -0,0 +1,18 @@
|
||||||
|
package cc.ryanc.halo.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Missing property value exception.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 3/22/19
|
||||||
|
*/
|
||||||
|
public class MissingPropertyException extends BadRequestException {
|
||||||
|
|
||||||
|
public MissingPropertyException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MissingPropertyException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +0,0 @@
|
||||||
package cc.ryanc.halo.exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Missing property value exception.
|
|
||||||
*
|
|
||||||
* @author johnniang
|
|
||||||
* @date 3/22/19
|
|
||||||
*/
|
|
||||||
public class MissingPropertyValueException extends BadRequestException {
|
|
||||||
|
|
||||||
public MissingPropertyValueException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public MissingPropertyValueException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package cc.ryanc.halo.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Property format exception.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 3/27/19
|
||||||
|
*/
|
||||||
|
public class PropertyFormatException extends BadRequestException {
|
||||||
|
|
||||||
|
public PropertyFormatException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PropertyFormatException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cc.ryanc.halo.model.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You pai yun properties.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 3/27/19
|
||||||
|
*/
|
||||||
|
public enum UpYunProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
OSS_SOURCE("upyun_oss_source", String.class),
|
||||||
|
OSS_PASSWORD("upyun_oss_password", String.class),
|
||||||
|
OSS_BUCKET("upyun_oss_bucket", String.class),
|
||||||
|
OSS_DOMAIN("upyun_oss_domain", String.class),
|
||||||
|
OSS_OPERATOR("upyun_oss_operator", String.class),
|
||||||
|
OSS_SMALL_URL("ypyun_oss_small_url", String.class);
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
private Class<?> type;
|
||||||
|
|
||||||
|
UpYunProperties(String value, Class<?> type) {
|
||||||
|
this.value = value;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getValue() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package cc.ryanc.halo.service;
|
package cc.ryanc.halo.service;
|
||||||
|
|
||||||
import cc.ryanc.halo.exception.MissingPropertyValueException;
|
import cc.ryanc.halo.exception.MissingPropertyException;
|
||||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Option;
|
import cc.ryanc.halo.model.entity.Option;
|
||||||
import cc.ryanc.halo.model.enums.PropertyEnum;
|
import cc.ryanc.halo.model.enums.PropertyEnum;
|
||||||
|
@ -116,7 +116,7 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
*
|
*
|
||||||
* @param property blog property
|
* @param property blog property
|
||||||
* @return an optiona value
|
* @return an optiona value
|
||||||
* @throws MissingPropertyValueException throws when property value dismisses
|
* @throws MissingPropertyException throws when property value dismisses
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
String getByPropertyOfNonNull(@NonNull PropertyEnum property);
|
String getByPropertyOfNonNull(@NonNull PropertyEnum property);
|
||||||
|
|
|
@ -2,8 +2,10 @@ package cc.ryanc.halo.service.impl;
|
||||||
|
|
||||||
import cc.ryanc.halo.config.properties.HaloProperties;
|
import cc.ryanc.halo.config.properties.HaloProperties;
|
||||||
import cc.ryanc.halo.exception.FileUploadException;
|
import cc.ryanc.halo.exception.FileUploadException;
|
||||||
|
import cc.ryanc.halo.exception.PropertyFormatException;
|
||||||
import cc.ryanc.halo.exception.ServiceException;
|
import cc.ryanc.halo.exception.ServiceException;
|
||||||
import cc.ryanc.halo.model.enums.QnYunProperties;
|
import cc.ryanc.halo.model.enums.QnYunProperties;
|
||||||
|
import cc.ryanc.halo.model.enums.UpYunProperties;
|
||||||
import cc.ryanc.halo.model.support.QiNiuPutSet;
|
import cc.ryanc.halo.model.support.QiNiuPutSet;
|
||||||
import cc.ryanc.halo.model.support.UploadResult;
|
import cc.ryanc.halo.model.support.UploadResult;
|
||||||
import cc.ryanc.halo.service.FileService;
|
import cc.ryanc.halo.service.FileService;
|
||||||
|
@ -11,6 +13,7 @@ import cc.ryanc.halo.service.OptionService;
|
||||||
import cc.ryanc.halo.utils.FilenameUtils;
|
import cc.ryanc.halo.utils.FilenameUtils;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
import cc.ryanc.halo.utils.HaloUtils;
|
||||||
import cc.ryanc.halo.utils.JsonUtils;
|
import cc.ryanc.halo.utils.JsonUtils;
|
||||||
|
import com.UpYun;
|
||||||
import com.qiniu.common.QiniuException;
|
import com.qiniu.common.QiniuException;
|
||||||
import com.qiniu.common.Zone;
|
import com.qiniu.common.Zone;
|
||||||
import com.qiniu.http.Response;
|
import com.qiniu.http.Response;
|
||||||
|
@ -27,13 +30,13 @@ import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.DigestUtils;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
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;
|
||||||
|
@ -252,7 +255,66 @@ public class FileServiceImpl implements FileService {
|
||||||
@Override
|
@Override
|
||||||
public UploadResult uploadToYpYun(MultipartFile file) {
|
public UploadResult uploadToYpYun(MultipartFile file) {
|
||||||
Assert.notNull(file, "Multipart file must not be null");
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
return null;
|
|
||||||
|
String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE);
|
||||||
|
|
||||||
|
if (StringUtils.startsWith(ossSource, "/")) {
|
||||||
|
throw new PropertyFormatException(UpYunProperties.OSS_SOURCE.getValue() + ": " + ossSource + " doesn't start with '/'");
|
||||||
|
}
|
||||||
|
|
||||||
|
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD);
|
||||||
|
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET);
|
||||||
|
String ossDomain = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_DOMAIN);
|
||||||
|
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR);
|
||||||
|
// small url can be null
|
||||||
|
String ossSmallUrl = optionService.getByPropertyOfNullable(UpYunProperties.OSS_SMALL_URL);
|
||||||
|
|
||||||
|
// Create up yun
|
||||||
|
UpYun upYun = new UpYun(ossBucket, ossOperator, ossPassword);
|
||||||
|
upYun.setDebug(log.isDebugEnabled());
|
||||||
|
upYun.setTimeout(60);
|
||||||
|
// TODO Provide a property for choosing
|
||||||
|
upYun.setApiDomain(UpYun.ED_AUTO);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get file basename
|
||||||
|
String basename = FilenameUtils.getBasename(file.getOriginalFilename());
|
||||||
|
// Get file extension
|
||||||
|
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
|
||||||
|
// Get md5 value of the file
|
||||||
|
String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream());
|
||||||
|
// Build file path
|
||||||
|
String upFilePath = ossSource + md5OfFile + '.' + extension;
|
||||||
|
// Set md5Content
|
||||||
|
upYun.setContentMD5(md5OfFile);
|
||||||
|
// Write file
|
||||||
|
boolean uploadSuccess = upYun.writeFile(upFilePath, file.getInputStream(), true, null);
|
||||||
|
if (!uploadSuccess) {
|
||||||
|
throw new FileUploadException("Failed to upload file " + file.getOriginalFilename() + " to UpYun " + upFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
String filePath = StringUtils.removeEnd(ossDomain, "/") + upFilePath;
|
||||||
|
|
||||||
|
// Build upload result
|
||||||
|
UploadResult uploadResult = new UploadResult();
|
||||||
|
uploadResult.setFilename(basename);
|
||||||
|
uploadResult.setFilePath(filePath);
|
||||||
|
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
|
||||||
|
uploadResult.setSuffix(extension);
|
||||||
|
uploadResult.setSize(file.getSize());
|
||||||
|
|
||||||
|
// Handle thumbnail
|
||||||
|
if (isImageType(uploadResult.getMediaType())) {
|
||||||
|
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||||
|
uploadResult.setWidth(image.getWidth());
|
||||||
|
uploadResult.setHeight(image.getHeight());
|
||||||
|
uploadResult.setThumbPath(StringUtils.isBlank(ossSmallUrl) ? filePath : filePath + ossSmallUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
return uploadResult;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new FileUploadException("Failed to upload file " + file.getOriginalFilename() + " to UpYun", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package cc.ryanc.halo.service.impl;
|
package cc.ryanc.halo.service.impl;
|
||||||
|
|
||||||
import cc.ryanc.halo.exception.MissingPropertyValueException;
|
import cc.ryanc.halo.exception.MissingPropertyException;
|
||||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Option;
|
import cc.ryanc.halo.model.entity.Option;
|
||||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
import cc.ryanc.halo.model.enums.BlogProperties;
|
||||||
|
@ -106,9 +106,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
properties.forEach((property, value) -> {
|
properties.forEach((property, value) -> save(property.getValue(), value, source));
|
||||||
save(property.getValue(), value, source);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,7 +137,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getByKeyOfNonNull(String key) {
|
public String getByKeyOfNonNull(String key) {
|
||||||
return getByKey(key).orElseThrow(() -> new MissingPropertyValueException("You have to config " + key + " setting"));
|
return getByKey(key).orElseThrow(() -> new MissingPropertyException("You have to config " + key + " setting"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue