generated from liuyiwuqing/halo-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 对接imgtp图床 2. 优化附件选项卡,实现图床选项动态加载 3. 更新依赖库
- Loading branch information
1 parent
79ef580
commit 5bd123f
Showing
15 changed files
with
650 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/site/muyin/picturebed/domain/ImgtpImage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package site.muyin.picturebed.domain; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author: lywq | ||
* @date: 2024/05/29 09:41 | ||
* @version: v1.0.0 | ||
* @description: | ||
**/ | ||
@Data | ||
public class ImgtpImage { | ||
private String id; | ||
private String strategy; | ||
private String path; | ||
private String name; | ||
private String alias_name; | ||
private String pathname; | ||
private Float size; | ||
private String mime; | ||
private String sha1; | ||
private String md5; | ||
private String ip; | ||
private Integer suspicious; | ||
private String upload_time; | ||
private String upload_date; | ||
private String url; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/site/muyin/picturebed/service/BaseImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package site.muyin.picturebed.service; | ||
|
||
import org.springframework.util.MultiValueMap; | ||
import reactor.core.publisher.Mono; | ||
import site.muyin.picturebed.query.CommonQuery; | ||
import site.muyin.picturebed.vo.PageResult; | ||
import site.muyin.picturebed.vo.ResultsVO; | ||
|
||
/** | ||
* @author: lywq | ||
* @date: 2024/05/29 09:51 | ||
* @version: v1.0.0 | ||
* @description: | ||
**/ | ||
public interface BaseImageService<T> { | ||
/** | ||
* 上传图片 | ||
* | ||
* @param multipartData: | ||
* @return: reactor.core.publisher.Mono<site.muyin.picturebed.vo.ResultsVO> | ||
* @author: lywq | ||
* @date: 2024/05/22 21:33 | ||
**/ | ||
Mono<ResultsVO> uploadImage(MultiValueMap<String, ?> multipartData); | ||
|
||
/** | ||
* 获取图片列表 | ||
* | ||
* @param query: | ||
* @return: reactor.core.publisher.Mono<site.muyin.picturebed.vo.PageResult < site.muyin.picturebed.domain.SmmsImage>> | ||
* @author: lywq | ||
* @date: 2024/05/22 21:33 | ||
**/ | ||
Mono<PageResult<T>> getImageList(CommonQuery query); | ||
|
||
/** | ||
* 删除图片 | ||
* | ||
* @param query: | ||
* @return: reactor.core.publisher.Mono<java.lang.Boolean> | ||
* @author: lywq | ||
* @date: 2024/05/22 21:33 | ||
**/ | ||
Mono<Boolean> deleteImage(CommonQuery query); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/site/muyin/picturebed/service/ImgtpService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package site.muyin.picturebed.service; | ||
|
||
import site.muyin.picturebed.domain.ImgtpImage; | ||
|
||
/** | ||
* @author: lywq | ||
* @date: 2024/05/29 09:48 | ||
* @version: v1.0.0 | ||
* @description: Imgtp图床服务接口 | ||
**/ | ||
public interface ImgtpService extends BaseImageService<ImgtpImage> { | ||
} |
134 changes: 134 additions & 0 deletions
134
src/main/java/site/muyin/picturebed/service/Impl/ImgtpServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package site.muyin.picturebed.service.Impl; | ||
|
||
import cn.hutool.core.util.ObjectUtil; | ||
import cn.hutool.core.util.StrUtil; | ||
import cn.hutool.http.HttpUtil; | ||
import cn.hutool.json.JSONUtil; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
import site.muyin.picturebed.config.PictureBedConfig; | ||
import site.muyin.picturebed.domain.ImgtpImage; | ||
import site.muyin.picturebed.query.CommonQuery; | ||
import site.muyin.picturebed.service.ImgtpService; | ||
import site.muyin.picturebed.utils.PluginCacheManager; | ||
import site.muyin.picturebed.vo.PageResult; | ||
import site.muyin.picturebed.vo.ResultsVO; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static site.muyin.picturebed.constant.CommonConstant.PictureBedType.IMGTP; | ||
|
||
/** | ||
* @author: lywq | ||
* @date: 2024/05/29 09:52 | ||
* @version: v1.0.0 | ||
* @description: Imgtp图床服务接口 | ||
**/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ImgtpServiceImpl implements ImgtpService { | ||
|
||
private final PluginCacheManager pluginCacheManager; | ||
|
||
@Override | ||
public Mono<ResultsVO> uploadImage(MultiValueMap<String, ?> multipartData) { | ||
Map<String, Object> paramMap = new HashMap(1); | ||
paramMap.put("file", multipartData); | ||
return req("upload", paramMap) | ||
.map(response -> { | ||
if (response.code == 200) { | ||
return ResultsVO.success(response.msg, response.data); | ||
} | ||
return ResultsVO.failure(response.msg); | ||
}); | ||
} | ||
|
||
@Override | ||
public Mono<PageResult<ImgtpImage>> getImageList(CommonQuery query) { | ||
Map<String, Object> paramMap = new HashMap(1); | ||
paramMap.put("page", query.getPage()); | ||
paramMap.put("rows", query.getSize()); | ||
String params = HttpUtil.toParams(paramMap); | ||
|
||
return req("images" + "?" + params, null) | ||
.map(response -> { | ||
if (response.code == 200) { | ||
ImgtpImagePageRes imgtpImagePageRes = JSONUtil.toBean(JSONUtil.toJsonStr(response.data), ImgtpImagePageRes.class); | ||
return new PageResult<>(imgtpImagePageRes.current_page, imgtpImagePageRes.per_page, imgtpImagePageRes.total, imgtpImagePageRes.last_page, imgtpImagePageRes.data); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public Mono<Boolean> deleteImage(CommonQuery query) { | ||
if (ObjectUtil.isEmpty(query.getImageId())) { | ||
return Mono.just(false); | ||
} | ||
Map<String, Object> paramMap = new HashMap(1); | ||
paramMap.put("id", query.getImageId()); | ||
String params = HttpUtil.toParams(paramMap); | ||
return req("delete" + "?" + params, null) | ||
.map(response -> { | ||
return response.code == 200; | ||
}); | ||
} | ||
|
||
private Mono<ImgtpResponseRecord> req(String path, Map<String, Object> paramMap) { | ||
PictureBedConfig pictureBedConfig = pluginCacheManager.getConfig(PictureBedConfig.class); | ||
PictureBedConfig.PictureBed config = pictureBedConfig.getPictureBeds().stream().filter(p -> p.getPictureBedType().equals(IMGTP)).findFirst().orElseThrow(); | ||
String url = config.getPictureBedUrl(); | ||
String authorization = config.getPictureBedToken(); | ||
|
||
WebClient WEB_CLIENT = | ||
WebClient.builder() | ||
.defaultHeader("token", authorization).build(); | ||
|
||
if (StrUtil.startWithAny(path, "images", "delete")) { | ||
return WEB_CLIENT.post() | ||
.uri(url + path) | ||
.retrieve() | ||
.bodyToMono(new ParameterizedTypeReference<>() { | ||
}); | ||
} else if (StrUtil.equals(path, "upload")) { | ||
MultiValueMap<String, ?> multiValueMap = (MultiValueMap<String, ?>) paramMap.get("file"); | ||
MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>(); | ||
multipartData.add("image", multiValueMap.getFirst("file")); | ||
|
||
return WEB_CLIENT.post() | ||
.uri(url + path) | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_JPEG_VALUE) | ||
.contentType(MediaType.MULTIPART_FORM_DATA) | ||
.body(BodyInserters.fromMultipartData(multipartData)) | ||
.retrieve() | ||
.bodyToMono(new ParameterizedTypeReference<>() { | ||
}); | ||
|
||
} else { | ||
throw new IllegalArgumentException("Unsupported path: " + path); | ||
} | ||
} | ||
|
||
@Data | ||
class ImgtpImagePageRes { | ||
Integer total; | ||
Integer per_page; | ||
Integer current_page; | ||
Integer last_page; | ||
List<ImgtpImage> data; | ||
} | ||
|
||
public record ImgtpResponseRecord(Integer code, String msg, Object data, Integer time) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.