Skip to content

Commit

Permalink
增加推送到官方广场
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwrcy committed Jun 19, 2023
1 parent 26079d4 commit 0b0baa2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions Dockerfile-allinone
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ ADD ./target/classes/*.properties /app/
COPY --from=front /app/mblog-front/dist /app/static
ENV JAVA_OPTS="-Xms512m -Xmx512m"
ENV DB_TYPE=""
ENV MBLOG_EMBED=true
CMD java $JAVA_OPTS -jar /app/memo-0.0.1-SNAPSHOT.jar --spring.config.location=/app/application$DB_TYPE.properties
2 changes: 0 additions & 2 deletions src/main/java/st/coo/memo/common/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public ResponseDTO<Void> methodArgumentNotValidExceptionHandler(IllegalArgumentE
}

@ExceptionHandler(DuplicateKeyException.class)
// public ResponseEntity<ResponseDTO<Void>> defaultExceptionHandler(DuplicateKeyException ex) {
public ResponseDTO<Void> defaultExceptionHandler(DuplicateKeyException ex) {
return ResponseDTO.fail(ResponseCode.param_error.getCode(), "数据已存在");
// return ResponseEntity.status(500).body(ResponseDTO.fail(ResponseCode.param_error.getCode(), "数据已存在"));
}
}
20 changes: 14 additions & 6 deletions src/main/java/st/coo/memo/service/MemoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import st.coo.memo.mapper.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.*;
import java.util.*;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class MemoService {
private String officialSquareUrl;


@Value("${MBLOG_EMBED:}")
private String embed;
@Resource
private HttpClient httpClient;

Expand Down Expand Up @@ -231,23 +234,27 @@ private void pushOfficialSquare(TMemo memo, TUser user, MemoType memoType) {

List<TResource> list = resourceMapper.selectListByQuery(QueryWrapper.create().and(T_RESOURCE.MEMO_ID.eq(memo.getId())));
Gson gson = new Gson();
String domain = sysConfigService.getString(SysConfigConstant.DOMAIN);
String corsDomainList = sysConfigService.getString(SysConfigConstant.CORS_DOMAIN_LIST);

Map<String, Object> map = Maps.newHashMap();
map.put("content", memo.getContent());
map.put("tags", memo.getTags());
map.put("publishTime", memo.getCreated());
map.put("publishTime", memo.getCreated().getTime());
map.put("author", user.getDisplayName());
map.put("website", backendUrl);
if (StringUtils.hasText(embed) && StringUtils.hasText(backendUrl)){
map.put("website",backendUrl);
}else if (!StringUtils.hasText(embed) && StringUtils.hasText(corsDomainList)){
map.put("website", corsDomainList.split(",")[0]);
}
map.put("memoId", memo.getId());
map.put("avatarUrl", user.getAvatarUrl());
map.put("userId", user.getId());
map.put("resources", list.stream().map(r -> convertToResourceDto(domain, r)).toList());
map.put("resources", list.stream().map(r -> convertToResourceDto(backendUrl, r)).toList());
String body = gson.toJson(map);
log.info("发送webhook到 {} ,body:{}", url, body);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
request.setEntity(new StringEntity(body));
request.setEntity(new StringEntity(body, StandardCharsets.UTF_8));
HttpResponse httpResponse = httpClient.execute(request);
String response = EntityUtils.toString(httpResponse.getEntity());
log.info("发送webhook成功,返回码:{},body:{},耗时:{}ms", httpResponse.getStatusLine().getStatusCode(),response, stopwatch.elapsed(TimeUnit.MILLISECONDS));
Expand Down Expand Up @@ -467,7 +474,8 @@ private static ResourceDto convertToResourceDto(String domain, TResource r) {
item.setSuffix(r.getSuffix());
item.setPublicId(r.getPublicId());
item.setFileType(r.getFileType());

item.setStorageType(r.getStorageType());
item.setFileName(r.getFileName());
return item;
}

Expand Down
23 changes: 22 additions & 1 deletion src/main/java/st/coo/memo/service/SysConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import st.coo.memo.dto.sysConfig.SaveSysConfigRequest;
import st.coo.memo.dto.sysConfig.SysConfigDto;
import st.coo.memo.entity.TSysConfig;
import st.coo.memo.entity.TUser;
import st.coo.memo.mapper.SysConfigMapperExt;
import st.coo.memo.mapper.UserMapperExt;

import java.io.IOException;
import java.util.List;
Expand All @@ -36,6 +38,7 @@
import java.util.concurrent.TimeUnit;

import static st.coo.memo.entity.table.Tables.T_SYS_CONFIG;
import static st.coo.memo.entity.table.Tables.T_USER;

@Slf4j
@Component
Expand All @@ -50,6 +53,11 @@ public class SysConfigService {
@Resource
private HttpClient httpClient;

@Resource
private UserMapperExt userMapperExt;

@Value("${MBLOG_EMBED:}")
private String embed;

@PostConstruct
public void init(){
Expand All @@ -68,9 +76,22 @@ public void save(SaveSysConfigRequest saveSysConfigRequest) {
String token = getString(SysConfigConstant.WEB_HOOK_TOKEN);

if (push2OfficialSquare.isPresent()){
String url = officialSquareUrl+"/api/token/add";

TUser admin = userMapperExt.selectOneByQuery(QueryWrapper.create().and(T_USER.ROLE.eq("ADMIN")));
Optional<SysConfigDto> backendDomain = saveSysConfigRequest.getItems().stream()
.filter(r -> Objects.equals(r.getKey(), SysConfigConstant.DOMAIN) && Objects.equals("true", r.getValue())).findFirst();
Optional<SysConfigDto> corsDomainList = saveSysConfigRequest.getItems().stream()
.filter(r -> Objects.equals(r.getKey(), SysConfigConstant.CORS_DOMAIN_LIST) ).findFirst();
String url = officialSquareUrl+"/api/token";
Map<String, Object> map = Maps.newHashMap();
map.put("token", token);
map.put("author", admin.getDisplayName());
map.put("avatarUrl", admin.getAvatarUrl());
if (StringUtils.isNotEmpty(embed) && backendDomain.isPresent()){
map.put("website", backendDomain.get().getValue());
}else if (StringUtils.isEmpty(embed) && corsDomainList.isPresent() && StringUtils.isNotEmpty(corsDomainList.get().getValue())){
map.put("website", corsDomainList.get().getValue().split(",")[0]);
}
String body = new Gson().toJson(map);
log.info("注册token {},body:{}", url, body);
Stopwatch stopwatch = Stopwatch.createStarted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ private UploadResourceResponse upload(MultipartFile multipartFile, StorageType s
if (StringUtils.isEmpty(fileType)){
fileType = "image/"+ FileUtil.getSuffix(targetPath);
}
if (!Objects.equals(storageType.name(),StorageType.LOCAL.name())){
FileUtil.del(targetPath);
}

TResource tResource = new TResource();
tResource.setPublicId(publicId);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-sqlite.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ springdoc.swagger-ui.enabled=${ENABLE_SWAGGER:false}
springdoc.packagesToScan=st.coo.memo.controller
spring.web.resources.static-locations=file:/app/static

official.square.url=${ENABLE_SWAGGER:https://api.square.coo.st}
official.square.url=${OFFICIAL_SQUARE_URL:http://127.0.0.1:7832}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ springdoc.packagesToScan=st.coo.memo.controller
spring.web.resources.static-locations=file:/app/static
logging.level.st.coo.memo.common.MyBatisFlexConfig=debug

official.square.url=${ENABLE_SWAGGER:http://127.0.0.1:8787}
official.square.url=${OFFICIAL_SQUARE_URL:http://127.0.0.1:7832}

0 comments on commit 0b0baa2

Please sign in to comment.