Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
1. 对接imgtp图床
2. 优化附件选项卡,实现图床选项动态加载
3. 更新依赖库
  • Loading branch information
liuyiwuqing committed Jun 3, 2024
1 parent 79ef580 commit 5bd123f
Show file tree
Hide file tree
Showing 15 changed files with 650 additions and 107 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
}

dependencies {
implementation platform('run.halo.tools.platform:plugin:2.14.0-SNAPSHOT')
implementation platform('run.halo.tools.platform:plugin:2.15.0-SNAPSHOT')
compileOnly 'run.halo.app:api'

testImplementation 'run.halo.app:api'
Expand Down Expand Up @@ -53,6 +53,6 @@ build {
}

halo {
version = '2.15.2'
version = '2.16.0'
debug = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class CommonConstant {
public static class PictureBedType {
public static final String LSKY = "lsky";
public static final String SMMS = "smms";
public static final String IMGTP = "imgtp";
}
}
28 changes: 28 additions & 0 deletions src/main/java/site/muyin/picturebed/domain/ImgtpImage.java
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 src/main/java/site/muyin/picturebed/service/BaseImageService.java
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 src/main/java/site/muyin/picturebed/service/ImgtpService.java
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 src/main/java/site/muyin/picturebed/service/Impl/ImgtpServiceImpl.java
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) {
}
}
35 changes: 1 addition & 34 deletions src/main/java/site/muyin/picturebed/service/LskyProService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package site.muyin.picturebed.service;

import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;
import site.muyin.picturebed.domain.LskyProAlbum;
import site.muyin.picturebed.domain.LskyProImage;
import site.muyin.picturebed.query.CommonQuery;
import site.muyin.picturebed.vo.PageResult;
import site.muyin.picturebed.vo.ResultsVO;

import java.util.List;

Expand All @@ -16,17 +13,7 @@
* @version: v1.0.0
* @description: 兰空图床服务接口
**/
public interface LskyProService {

/**
* 上传图片
*
* @param multipartData:
* @return: reactor.core.publisher.Mono<site.muyin.picturebed.vo.ResultsVO>
* @author: lywq
* @date: 2024/05/22 21:31
**/
Mono<ResultsVO> uploadImage(MultiValueMap<String, ?> multipartData);
public interface LskyProService extends BaseImageService<LskyProImage> {

/**
* 获取图库列表
Expand All @@ -37,24 +24,4 @@ public interface LskyProService {
* @date: 2024/05/22 21:31
**/
Mono<List<LskyProAlbum>> getAlbumList(CommonQuery query);

/**
* 获取图片列表
*
* @param query:
* @return: reactor.core.publisher.Mono<site.muyin.picturebed.vo.PageResult < site.muyin.picturebed.domain.LskyProImage>>
* @author: lywq
* @date: 2024/05/22 21:31
**/
Mono<PageResult<LskyProImage>> getImageList(CommonQuery query);

/**
* 删除图片
*
* @param query:
* @return: reactor.core.publisher.Mono<java.lang.Boolean>
* @author: lywq
* @date: 2024/05/22 21:31
**/
Mono<Boolean> deleteImage(CommonQuery query);
}
26 changes: 26 additions & 0 deletions src/main/java/site/muyin/picturebed/service/PictureBedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;
import site.muyin.picturebed.domain.ImgtpImage;
import site.muyin.picturebed.domain.LskyProAlbum;
import site.muyin.picturebed.domain.LskyProImage;
import site.muyin.picturebed.domain.SmmsImage;
Expand All @@ -18,6 +19,7 @@
import java.io.File;
import java.util.List;

import static site.muyin.picturebed.constant.CommonConstant.PictureBedType.IMGTP;
import static site.muyin.picturebed.constant.CommonConstant.PictureBedType.LSKY;
import static site.muyin.picturebed.constant.CommonConstant.PictureBedType.SMMS;

Expand All @@ -33,6 +35,7 @@ public class PictureBedService {

private final LskyProService lskyProService;
private final SmmsService smmsService;
private final ImgtpService imgtpService;

public Mono<ResultsVO> uploadImage(CommonQuery query, MultiValueMap<String, Part> parts) {
String type = query.getType();
Expand All @@ -41,6 +44,8 @@ public Mono<ResultsVO> uploadImage(CommonQuery query, MultiValueMap<String, Part
return lskyProService.uploadImage(parts);
case SMMS:
return smmsService.uploadImage(parts);
case IMGTP:
return imgtpService.uploadImage(parts);
default:
// TODO: get album list from other picture bed service
throw new IllegalArgumentException("暂不支持该图片托管服务");
Expand Down Expand Up @@ -78,6 +83,11 @@ public Mono<PageResult<ImageVO>> getImageList(CommonQuery query) {
PageResult<ImageVO> imageVOPageResult = convertSmmsImageListToImageVOList(smmsImages);
return Mono.just(imageVOPageResult);
});
case IMGTP:
return imgtpService.getImageList(query).flatMap(imgtpImages -> {
PageResult<ImageVO> imageVOPageResult = convertImgtpImageListToImageVOList(imgtpImages);
return Mono.just(imageVOPageResult);
});
default:
// TODO: get image list from other picture bed service
throw new IllegalArgumentException("暂不支持该图片托管服务");
Expand All @@ -91,6 +101,8 @@ public Mono<Boolean> deleteImage(CommonQuery query) {
return lskyProService.deleteImage(query);
case SMMS:
return smmsService.deleteImage(query);
case IMGTP:
return imgtpService.deleteImage(query);
default:
// TODO: delete image from other picture bed service
throw new IllegalArgumentException("暂不支持该图片托管服务");
Expand Down Expand Up @@ -137,6 +149,20 @@ private PageResult<ImageVO> convertSmmsImageListToImageVOList(PageResult<SmmsIma
return new PageResult<>(page.getPage(), page.getSize(), page.getTotalCount(), page.getTotalPages(), imageVOList);
}

private PageResult<ImageVO> convertImgtpImageListToImageVOList(PageResult<ImgtpImage> imgtpImages) {
List<ImgtpImage> imageList = imgtpImages.getList();
List<ImageVO> imageVOList = imageList.stream().map(image -> {
ImageVO imageVO = new ImageVO();
imageVO.setId(image.getId())
.setName(image.getName())
.setUrl(image.getUrl())
.setMediaType(image.getMime())
.setSize(image.getSize());
return imageVO;
}).toList();
return new PageResult<>(imgtpImages.getPage(), imgtpImages.getSize(), imgtpImages.getTotalCount(), imgtpImages.getTotalPages(), imageVOList);
}

public static String getMediaType(String fileName) {
MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
File file = new File(fileName);
Expand Down
Loading

0 comments on commit 5bd123f

Please sign in to comment.