diff --git a/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java b/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java index 51e895cf9..3acc7c0ee 100644 --- a/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java +++ b/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java @@ -9,11 +9,15 @@ import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import run.halo.app.exception.FileOperationException; +import run.halo.app.exception.ServiceException; 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.service.OptionService; import run.halo.app.utils.FilenameUtils; import run.halo.app.utils.HttpClientUtils; @@ -30,24 +34,40 @@ import java.util.Objects; @Component public class SmmsFileHandler implements FileHandler { + @Deprecated 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_V2 = "https://sm.ms/api/v2/delete/%s"; + 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 RestTemplate httpsRestTemplate; - public SmmsFileHandler(RestTemplate httpsRestTemplate) { + private final OptionService optionService; + + public SmmsFileHandler(RestTemplate httpsRestTemplate, + OptionService optionService) { this.httpsRestTemplate = httpsRestTemplate; + this.optionService = optionService; } @Override public UploadResult upload(MultipartFile file) { 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())) { log.error("Invalid extension: [{}]", file.getContentType()); throw new FileOperationException("不支持的文件类型,仅支持 \"jpeg, jpg, png, gif, bmp\" 格式的图片"); @@ -58,6 +78,7 @@ public class SmmsFileHandler implements FileHandler { headers.setContentType(MediaType.MULTIPART_FORM_DATA); // Set user agent manually headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT); + headers.set(HttpHeaders.AUTHORIZATION, apiSecretToken); LinkedMultiValueMap body = new LinkedMultiValueMap<>(); @@ -74,7 +95,7 @@ public class SmmsFileHandler implements FileHandler { HttpEntity> httpEntity = new HttpEntity<>(body, headers); // Upload file - ResponseEntity mapResponseEntity = httpsRestTemplate.postForEntity(UPLOAD_API, httpEntity, SmmsResponse.class); + ResponseEntity mapResponseEntity = httpsRestTemplate.postForEntity(UPLOAD_API_V2, httpEntity, SmmsResponse.class); // Check status if (mapResponseEntity.getStatusCode().isError()) { @@ -117,7 +138,7 @@ public class SmmsFileHandler implements FileHandler { Assert.hasText(key, "Deleting key must not be blank"); // Build delete url - String url = String.format(DELETE_API, key); + String url = String.format(DELETE_API_V2, key); // Set user agent manually HttpHeaders headers = new HttpHeaders(); diff --git a/src/main/java/run/halo/app/model/properties/SmmsProperties.java b/src/main/java/run/halo/app/model/properties/SmmsProperties.java new file mode 100644 index 000000000..a12af7b8c --- /dev/null +++ b/src/main/java/run/halo/app/model/properties/SmmsProperties.java @@ -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; + } +}