mirror of https://github.com/halo-dev/halo
Support sm.ms api v2 #271
parent
557b286550
commit
e3b17ddcb4
|
@ -9,11 +9,15 @@ import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import run.halo.app.exception.FileOperationException;
|
import run.halo.app.exception.FileOperationException;
|
||||||
|
import run.halo.app.exception.ServiceException;
|
||||||
import run.halo.app.model.enums.AttachmentType;
|
import run.halo.app.model.enums.AttachmentType;
|
||||||
|
import run.halo.app.model.properties.SmmsProperties;
|
||||||
import run.halo.app.model.support.UploadResult;
|
import run.halo.app.model.support.UploadResult;
|
||||||
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.utils.FilenameUtils;
|
import run.halo.app.utils.FilenameUtils;
|
||||||
import run.halo.app.utils.HttpClientUtils;
|
import run.halo.app.utils.HttpClientUtils;
|
||||||
|
|
||||||
|
@ -30,24 +34,40 @@ import java.util.Objects;
|
||||||
@Component
|
@Component
|
||||||
public class SmmsFileHandler implements FileHandler {
|
public class SmmsFileHandler implements FileHandler {
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
private final static String UPLOAD_API = "https://sm.ms/api/upload";
|
private final static String UPLOAD_API = "https://sm.ms/api/upload";
|
||||||
|
|
||||||
|
private final static String UPLOAD_API_V2 = "https://sm.ms/api/v2/upload";
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
private final static String DELETE_API = "https://sm.ms/api/delete/%s";
|
private final static String DELETE_API = "https://sm.ms/api/delete/%s";
|
||||||
|
|
||||||
|
private final static String DELETE_API_V2 = "https://sm.ms/api/v2/delete/%s";
|
||||||
|
|
||||||
private final static String SUCCESS_CODE = "success";
|
private final static String SUCCESS_CODE = "success";
|
||||||
|
|
||||||
private final static String DEFAULT_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36";
|
private final static String DEFAULT_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36";
|
||||||
|
|
||||||
private final RestTemplate httpsRestTemplate;
|
private final RestTemplate httpsRestTemplate;
|
||||||
|
|
||||||
public SmmsFileHandler(RestTemplate httpsRestTemplate) {
|
private final OptionService optionService;
|
||||||
|
|
||||||
|
public SmmsFileHandler(RestTemplate httpsRestTemplate,
|
||||||
|
OptionService optionService) {
|
||||||
this.httpsRestTemplate = httpsRestTemplate;
|
this.httpsRestTemplate = httpsRestTemplate;
|
||||||
|
this.optionService = optionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UploadResult upload(MultipartFile file) {
|
public UploadResult upload(MultipartFile file) {
|
||||||
Assert.notNull(file, "Multipart file must not be null");
|
Assert.notNull(file, "Multipart file must not be null");
|
||||||
|
|
||||||
|
String apiSecretToken = optionService.getByPropertyOfNonNull(SmmsProperties.SMMS_API_SECRET_TOKEN).toString();
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(apiSecretToken)) {
|
||||||
|
throw new ServiceException("请先设置 SM.MS 的 Secret Token");
|
||||||
|
}
|
||||||
|
|
||||||
if (!FileHandler.isImageType(file.getContentType())) {
|
if (!FileHandler.isImageType(file.getContentType())) {
|
||||||
log.error("Invalid extension: [{}]", file.getContentType());
|
log.error("Invalid extension: [{}]", file.getContentType());
|
||||||
throw new FileOperationException("不支持的文件类型,仅支持 \"jpeg, jpg, png, gif, bmp\" 格式的图片");
|
throw new FileOperationException("不支持的文件类型,仅支持 \"jpeg, jpg, png, gif, bmp\" 格式的图片");
|
||||||
|
@ -58,6 +78,7 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
// Set user agent manually
|
// Set user agent manually
|
||||||
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
|
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
|
||||||
|
headers.set(HttpHeaders.AUTHORIZATION, apiSecretToken);
|
||||||
|
|
||||||
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
|
||||||
|
@ -74,7 +95,7 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
|
HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
|
||||||
|
|
||||||
// Upload file
|
// Upload file
|
||||||
ResponseEntity<SmmsResponse> mapResponseEntity = httpsRestTemplate.postForEntity(UPLOAD_API, httpEntity, SmmsResponse.class);
|
ResponseEntity<SmmsResponse> mapResponseEntity = httpsRestTemplate.postForEntity(UPLOAD_API_V2, httpEntity, SmmsResponse.class);
|
||||||
|
|
||||||
// Check status
|
// Check status
|
||||||
if (mapResponseEntity.getStatusCode().isError()) {
|
if (mapResponseEntity.getStatusCode().isError()) {
|
||||||
|
@ -117,7 +138,7 @@ public class SmmsFileHandler implements FileHandler {
|
||||||
Assert.hasText(key, "Deleting key must not be blank");
|
Assert.hasText(key, "Deleting key must not be blank");
|
||||||
|
|
||||||
// Build delete url
|
// Build delete url
|
||||||
String url = String.format(DELETE_API, key);
|
String url = String.format(DELETE_API_V2, key);
|
||||||
|
|
||||||
// Set user agent manually
|
// Set user agent manually
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package run.halo.app.model.properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base meta entity.
|
||||||
|
*
|
||||||
|
* @author ryanwang
|
||||||
|
* @author ikaisec
|
||||||
|
* @date 2019-08-04
|
||||||
|
*/
|
||||||
|
public enum SmmsProperties implements PropertyEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SM.MS personal api secret token
|
||||||
|
*/
|
||||||
|
SMMS_API_SECRET_TOKEN("smms_api_secret_token", String.class, "");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
private final Class<?> type;
|
||||||
|
|
||||||
|
private final String defaultValue;
|
||||||
|
|
||||||
|
SmmsProperties(String value, Class<?> type, String defaultValue) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
if (!PropertyEnum.isSupportedType(type)) {
|
||||||
|
throw new IllegalArgumentException("Unsupported blog property type: " + type);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.value = value;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String defaultValue() {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue