fix: sm.ms upload handler.

pull/755/head
ruibaby 2019-12-16 17:45:13 +08:00
parent ac0905b9a2
commit 26da4bd3c7
11 changed files with 151 additions and 21 deletions

View File

@ -21,7 +21,7 @@
轻快,简洁,功能强大,使用 Java 开发的博客系统。
> [社区](https://bbs.halo.run) | [QQ 交流群](https://jq.qq.com/?_wv=1027&k=5tnr930) | [Telegram 交流群](https://t.me/HaloBlog) | [Telegram 频道](https://t.me/halo_dev) | [WeHalo 小程序](https://github.com/aquanlerou/WeHalo)。
> [网](https://halo.run) | [社区](https://bbs.halo.run) | [QQ 交流群](https://jq.qq.com/?_wv=1027&k=5tnr930) | [Telegram 交流群](https://t.me/HaloBlog) | [Telegram 频道](https://t.me/halo_dev)
## 快速开始
@ -55,6 +55,7 @@ java -jar halo-latest.jar
- 独立评论模块halo-comment<https://github.com/halo-dev/halo-comment>
- 管理 APPhalo-app<https://github.com/halo-dev/halo-app>
- 主题仓库:<https://halo.run/theme>
- WeHalo 小程序:<https://github.com/aquanlerou/WeHalo>
## 许可证

View File

@ -69,7 +69,7 @@ public class PostCommentController {
public Page<BaseCommentVO> listCommentTree(@PathVariable("postId") Integer postId,
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
return postCommentService.pageVosBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
return postCommentService.pageVosAllBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
}
@GetMapping("{postId:\\d+}/list_view")

View File

@ -64,7 +64,7 @@ public class SheetCommentController {
public Page<BaseCommentVO> listCommentTree(@PathVariable("sheetId") Integer sheetId,
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
return sheetCommentService.pageVosBy(sheetId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
return sheetCommentService.pageVosAllBy(sheetId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
}
@GetMapping("{sheetId:\\d+}/list_view")

View File

@ -4,7 +4,10 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
@ -22,13 +25,14 @@ import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.HttpClientUtils;
import java.io.IOException;
import java.util.Objects;
import java.util.*;
/**
* Sm.ms file handler.
*
* @author johnniang
* @date 3/29/19
* @author ryanwang
* @date 2019-03-29
*/
@Slf4j
@Component
@ -46,8 +50,6 @@ public class SmmsFileHandler implements FileHandler {
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;
private final OptionService optionService;
@ -56,6 +58,10 @@ public class SmmsFileHandler implements FileHandler {
OptionService optionService) {
this.httpsRestTemplate = httpsRestTemplate;
this.optionService = optionService;
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
this.httpsRestTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
}
@Override
@ -77,8 +83,6 @@ public class SmmsFileHandler implements FileHandler {
// Set content type
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set(HttpHeaders.AUTHORIZATION, apiSecretToken);
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
@ -111,6 +115,10 @@ public class SmmsFileHandler implements FileHandler {
throw new FileOperationException(smmsResponse == null ? "SM.MS 服务返回内容为空" : smmsResponse.getMessage()).setErrorData(smmsResponse);
}
if (smmsResponse.getSuccess()) {
throw new FileOperationException("上传请求失败:" + smmsResponse.getMessage()).setErrorData(smmsResponse);
}
// Get response data
SmmsResponseData data = smmsResponse.getData();
@ -141,7 +149,6 @@ public class SmmsFileHandler implements FileHandler {
// Set user agent manually
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
// Delete the file
ResponseEntity<String> responseEntity = httpsRestTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null, headers), String.class);
@ -177,12 +184,15 @@ public class SmmsFileHandler implements FileHandler {
@NoArgsConstructor
private static class SmmsResponse {
private Boolean success;
private String code;
private String message;
private SmmsResponseData data;
private String RequestId;
}
@Data
@ -190,23 +200,24 @@ public class SmmsFileHandler implements FileHandler {
@NoArgsConstructor
private static class SmmsResponseData {
private Integer width;
private Integer height;
private String filename;
private String storename;
private Integer size;
private Integer width;
private Integer height;
private String path;
private String hash;
private String delete;
private String url;
private String path;
private String delete;
private String page;
}
}

View File

@ -7,7 +7,9 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.transaction.annotation.Transactional;
import run.halo.app.model.entity.BaseComment;
import run.halo.app.model.entity.PostCategory;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.projection.CommentChildrenCountProjection;
import run.halo.app.model.projection.CommentCountProjection;
@ -75,6 +77,24 @@ public interface BaseCommentRepository<COMMENT extends BaseComment> extends Base
*/
long countByStatus(@NonNull CommentStatus status);
/**
* Removes comments by post id.
*
* @param postId post id must not be null
* @return a list of comment deleted
*/
@NonNull
List<COMMENT> deleteByPostId(@NonNull Integer postId);
/**
* Removes comments by parent id.
*
* @param id comment id must not be null
* @return a list of comment deleted
*/
@NonNull
List<COMMENT> deleteByParentId(@NonNull Long id);
/**
* Finds comments by post id, comment status.
*

View File

@ -84,6 +84,17 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
@NonNull
Page<COMMENT> pageBy(@NonNull CommentQuery commentQuery, @NonNull Pageable pageable);
/**
* Lists comment vos by post id.
*
* @param postId post id must not be null
* @param pageable page info must not be null
* @param status status must not be null
* @return a page of comment vo
*/
@NonNull
Page<BaseCommentVO> pageVosAllBy(@NonNull Integer postId, @NonNull Pageable pageable);
/**
* Lists comment vos by post id.
*
@ -94,6 +105,16 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
@NonNull
Page<BaseCommentVO> pageVosBy(@NonNull Integer postId, @NonNull Pageable pageable);
/**
* Lists comment vos by list of COMMENT.
*
* @param comments comments must not be null
* @param pageable page info must not be null
* @return a page of comment vo
*/
@NonNull
Page<BaseCommentVO> pageVosBy(@NonNull List<COMMENT> comments, @NonNull Pageable pageable);
/**
* Lists comment with parent vo.
*
@ -160,6 +181,14 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
@NonNull
List<COMMENT> updateStatusByIds(@NonNull List<Long> ids, @NonNull CommentStatus status);
/**
* Remove comments by post id.
*
* @param postId post id must not be null
* @return a list of comments
*/
List<COMMENT> removeByPostId(@NonNull Integer postId);
/**
* Removes comments in batch.
*

View File

@ -18,6 +18,7 @@ import run.halo.app.event.comment.CommentNewEvent;
import run.halo.app.event.comment.CommentPassEvent;
import run.halo.app.event.comment.CommentReplyEvent;
import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.BaseCommentDTO;
import run.halo.app.model.entity.BaseComment;
import run.halo.app.model.entity.User;
@ -111,19 +112,27 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
}
@Override
public Page<BaseCommentVO> pageVosBy(Integer postId, Pageable pageable) {
public Page<BaseCommentVO> pageVosAllBy(Integer postId, Pageable pageable) {
Assert.notNull(postId, "Post id must not be null");
Assert.notNull(pageable, "Page info must not be null");
log.debug("Getting comment tree view of post: [{}], page info: [{}]", postId, pageable);
// List all the top comments (Caution: This list will be cleared)
List<COMMENT> comments = baseCommentRepository.findAllByPostIdAndStatus(postId, CommentStatus.PUBLISHED);
List<COMMENT> comments = baseCommentRepository.findAllByPostId(postId);
Comparator<BaseCommentVO> commentVOComparator = buildCommentComparator(pageable.getSortOr(Sort.by(Sort.Direction.DESC, "createTime")));
return pageVosBy(comments, pageable);
}
@Override
public Page<BaseCommentVO> pageVosBy(List<COMMENT> comments, Pageable pageable) {
Assert.notNull(comments, "Comments must not be null");
Assert.notNull(pageable, "Page info must not be null");
Comparator<BaseCommentVO> commentComparator = buildCommentComparator(pageable.getSortOr(Sort.by(Sort.Direction.DESC, "createTime")));
// Convert to vo
List<BaseCommentVO> topComments = convertToVo(comments, commentVOComparator);
List<BaseCommentVO> topComments = convertToVo(comments, commentComparator);
List<BaseCommentVO> pageContent;
@ -145,7 +154,19 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
}
return new CommentPage<>(pageContent, pageable, topComments.size(), comments.size());
}
@Override
public Page<BaseCommentVO> pageVosBy(Integer postId, Pageable pageable) {
Assert.notNull(postId, "Post id must not be null");
Assert.notNull(pageable, "Page info must not be null");
log.debug("Getting comment tree view of post: [{}], page info: [{}]", postId, pageable);
// List all the top comments (Caution: This list will be cleared)
List<COMMENT> comments = baseCommentRepository.findAllByPostIdAndStatus(postId, CommentStatus.PUBLISHED);
return pageVosBy(comments, pageable);
}
@Override
@ -339,6 +360,27 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
}).collect(Collectors.toList());
}
@Override
public List<COMMENT> removeByPostId(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return baseCommentRepository.deleteByPostId(postId);
}
@Override
public COMMENT removeById(Long id) {
Assert.notNull(id, "Comment id must not be null");
COMMENT comment = baseCommentRepository.findById(id).orElseThrow(() -> new NotFoundException("查询不到该评论的信息").setErrorData(id));
if (comment.getParentId() == 0) {
// Remove comment children.
List<COMMENT> comments = baseCommentRepository.deleteByParentId(id);
log.debug("Removed comment children: [{}]", comments);
}
return super.removeById(id);
}
@Override
public List<COMMENT> removeByIds(Collection<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {

View File

@ -1,5 +1,6 @@
package run.halo.app.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
@ -12,6 +13,7 @@ import org.springframework.util.CollectionUtils;
import run.halo.app.model.dto.JournalDTO;
import run.halo.app.model.dto.JournalWithCmtCountDTO;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.JournalComment;
import run.halo.app.model.enums.JournalType;
import run.halo.app.model.params.JournalParam;
import run.halo.app.model.params.JournalQuery;
@ -32,6 +34,7 @@ import java.util.stream.Collectors;
* @author ryanwang
* @date 2019-04-24
*/
@Slf4j
@Service
public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> implements JournalService {
@ -72,6 +75,17 @@ public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> im
return journalRepository.findAllByType(type, pageable);
}
@Override
public Journal removeById(Integer id) {
Assert.notNull(id, "Journal id must not be null");
// Remove journal comments
List<JournalComment> journalComments = journalCommentService.removeByPostId(id);
log.debug("Removed journal comments: [{}]", journalComments);
return super.removeById(id);
}
@Override
public JournalDTO convertTo(Journal journal) {
Assert.notNull(journal, "Journal must not be null");

View File

@ -415,9 +415,14 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
log.debug("Removed post categories: [{}]", postCategories);
// Remove post metas
List<PostMeta> postMetas = postMetaService.removeByPostId(postId);
log.debug("Removed post metas: [{}]", postMetas);
// Remove post comments
List<PostComment> postComments = postCommentService.removeByPostId(postId);
log.debug("Removed post comments: [{}]", postComments);
Post deletedPost = super.removeById(postId);
// Log it

View File

@ -13,7 +13,9 @@ import run.halo.app.event.post.SheetVisitEvent;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.InternalSheetDTO;
import run.halo.app.model.entity.PostComment;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.entity.SheetComment;
import run.halo.app.model.entity.SheetMeta;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostStatus;
@ -223,7 +225,13 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
@Override
public Sheet removeById(Integer id) {
// Remove sheet comments
List<SheetComment> sheetComments = sheetCommentService.removeByPostId(id);
log.debug("Removed sheet comments: [{}]", sheetComments);
Sheet sheet = super.removeById(id);
// Log it
eventPublisher.publishEvent(new LogEvent(this, id.toString(), LogType.SHEET_DELETED, sheet.getTitle()));

View File

@ -2,6 +2,6 @@
<#if !post.disallowComment!false>
<script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
<script src="${options.comment_internal_plugin_js!'//cdn.jsdelivr.net/gh/halo-dev/halo-comment@latest/dist/halo-comment.min.js'}"></script>
<halo-comment id="${post.id}" type="${type}"/>
<halo-comment id="${post.id?c}" type="${type}"/>
</#if>
</#macro>