// TODO 优化代码,视频评论和文字评论大部分重复 @RequestMapping(value = PREFIX_API_1_COMMENTS + "create", method = RequestMethod.POST) @ResponseBody public Object create( @RequestParam(value = "text") String text, @RequestParam(value = "postId", required = false) String postId, @RequestParam(value = "replyCommentId", required = false) String replyCommentId) { try { logger.debug("正在发布文字评论:"); logger.debug(" text : {}", text); logger.debug(" postId : {}", postId); logger.debug(" replyCommentId : {}", replyCommentId); if (StringUtils.isBlank(postId) && StringUtils.isBlank(replyCommentId)) { return ErrorDto.badRequest("参数postId和replyCommentId不能都为空"); } Comment comment = new Comment(); // 回复Comment if (StringUtils.isNotBlank(replyCommentId)) { Comment replyComment = commentService.getComment(replyCommentId); if (replyComment == null) { return ErrorDto.badRequest(String.format("replyCommentId[%s]无效", replyCommentId)); } comment.setReplyCommentId(replyCommentId); comment.setReplyCommentUserId(replyComment.getUserId()); comment.setPostId(replyComment.getPostId()); // TODO 存被回复者screenName以提高性能 } // 回复Post else { Post post = statusService.getPost(postId); if (post == null) { return ErrorDto.badRequest(String.format("postId[%s]不存在", postId)); } if (!post.isPublic()) { return ErrorDto.badRequest("不能评论非所有人可见的视频"); } if (post.getOriginPostId() == null) { comment.setPostId(postId); } else { comment.setPostId(post.getOriginPostId()); } } comment.setUserId(accountService.getCurrentUserId()); comment.setText(text); comment = commentService.create(comment, null, null, null); return commentService.toCommentDto(comment); } catch (DuplicateException e) { return ErrorDto.duplicate(); } catch (Exception e) { logger.info("发布评论失败", e); return ErrorDto.internalServerError(e.getMessage()); } }
/** 视频评论 */ @RequestMapping(value = PREFIX_API_1_COMMENTS + "createWithMedia", method = RequestMethod.POST) @ResponseBody public Object createWithMedia( @RequestParam(value = "video", required = false) MultipartFile video, @RequestParam(value = "image", required = false) MultipartFile image, @RequestParam(value = "audio", required = false) MultipartFile audio, @RequestParam(value = "audioDuration", required = false) Long audioDuration, @RequestParam(value = "text", required = false) String text, @RequestParam(value = "postId", required = false) String postId, @RequestParam(value = "replyCommentId", required = false) String replyCommentId) { try { logger.debug("正在发布视频评论:"); if (video != null) { logger.debug(" video : {}", FileUtils.byteCountToDisplaySize(video.getSize())); } if (image != null) { logger.debug(" image : {}", FileUtils.byteCountToDisplaySize(image.getSize())); } if (audio != null) { logger.debug(" audio : {}", FileUtils.byteCountToDisplaySize(audio.getSize())); } logger.debug(" text : {}", text); logger.debug(" postId : {}", postId); logger.debug(" replyCommentId : {}", replyCommentId); // 参数检查 if (StringUtils.isBlank(postId) && StringUtils.isBlank(replyCommentId)) { return ErrorDto.badRequest("参数postId和replyCommentId不能都为空"); } // 文件大小限制 if (video != null && video.getSize() > Setting.MAX_VIDEO_LENGTH_BYTES || image != null && image.getSize() > Setting.MAX_IMAGE_LENGTH_BYTES || audio != null && audio.getSize() > Setting.MAX_AUDIO_LENGTH_BYTES) { return ErrorDto.badRequest("上传的文件太大"); } FileInfo videoFileInfo = null; FileInfo imageFileInfo = null; FileInfo audioFileInfo = null; // 视频评论 if (video != null && image != null) { videoFileInfo = ControllerUtils.getFileInfo(video); imageFileInfo = ControllerUtils.getFileInfo(image); } // 语音评论 else if (audio != null) { audioFileInfo = ControllerUtils.getFileInfo(audio); audioFileInfo.setDuration(audioDuration); } // 参数错误 else { return ErrorDto.badRequest( "参数必须满足这个条件:(video != null && image != null) || (audio != null)"); } return createComment( text, replyCommentId, postId, videoFileInfo, imageFileInfo, audioFileInfo); } catch (DuplicateException e) { return ErrorDto.duplicate(); } catch (Exception e) { logger.info("发布评论失败", e); return ErrorDto.internalServerError(e.getMessage()); } }