1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| @Override @Transactional public ResponseResult addPosts(AddPostsDto postsDto) {
String content = postsDto.getContent(); if (ImageUrlUtils.containsTempImages(content)) { content = processContentImagesAndSaveRecords(content, postsDto.getTitle()); postsDto.setContent(content); }
String thumbnail = postsDto.getThumbnail(); if (StringUtils.hasText(thumbnail) && thumbnail.contains("temp/")) { UploadFileMetaVo uploadFileMetaVo = ossFileService.moveTempToFormal(thumbnail); if (uploadFileMetaVo == null || !StringUtils.hasText(uploadFileMetaVo.getUrl())) { throw new RuntimeException("缩略图保存失败,无法保存文章记录: " + thumbnail); } String fileSource = "文章《" + postsDto.getTitle() + "》中的缩略图"; thumbnail = uploadFileMetaVo.getUrl(); postsDto.setThumbnail(thumbnail); saveFileRecord(uploadFileMetaVo.getName(), uploadFileMetaVo.getFilePath(), uploadFileMetaVo.getUrl(), uploadFileMetaVo.getSize(), uploadFileMetaVo.getMimeType(), fileSource); }
Article article = BeanCopyUtils.copyBean(postsDto, Article.class); articleService.save(article);
redisCache.setCacheMapValue(SystemConstants.ARTICLE_VIEW_COUNT, article.getId().toString(), 0);
List<ArticleTag> articleTags = postsDto.getTags().stream() .map(tagId -> new ArticleTag(article.getId(), tagId)) .collect(Collectors.toList());
articleTagService.saveBatch(articleTags); return ResponseResult.okResult(); }
@Override @Transactional public ResponseResult updatePosts(Long id, UpdatePostsDto updatePostsDto) { updatePostsDto.setId(id);
Article oldArticle = articleService.getById(id); if (oldArticle == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该文章不存在!"); }
String content = updatePostsDto.getContent(); List<String> oldContentImages = new ArrayList<>(); List<String> newContentImages = new ArrayList<>();
if (StringUtils.hasText(oldArticle.getContent())) { oldContentImages = ImageUrlUtils.extractImageUrls(oldArticle.getContent()); }
if (ImageUrlUtils.containsTempImages(content)) { content = processContentImagesAndSaveRecords(content, oldArticle.getTitle()); updatePostsDto.setContent(content); }
if (StringUtils.hasText(content)) { newContentImages = ImageUrlUtils.extractImageUrls(content); }
for (String oldImageUrl : oldContentImages) { if (!newContentImages.contains(oldImageUrl)) { ossFileService.deleteFileByUrl(oldImageUrl); deleteFileRecordByUrl(oldImageUrl); log.info("删除旧的内容图片并同步文件表: {}", oldImageUrl); } }
String newThumbnail = updatePostsDto.getThumbnail(); String oldThumbnail = oldArticle.getThumbnail();
if (StringUtils.hasText(newThumbnail) && newThumbnail.contains("temp/")) { UploadFileMetaVo uploadFileMetaVo = ossFileService.moveTempToFormal(newThumbnail); if (uploadFileMetaVo == null || !StringUtils.hasText(uploadFileMetaVo.getUrl())) { throw new RuntimeException("缩略图保存失败,无法更新文章记录: " + newThumbnail); } newThumbnail = uploadFileMetaVo.getUrl(); updatePostsDto.setThumbnail(newThumbnail); saveFileRecord(uploadFileMetaVo.getName(), uploadFileMetaVo.getFilePath(), uploadFileMetaVo.getUrl(), uploadFileMetaVo.getSize(), uploadFileMetaVo.getMimeType(), "文章《" + oldArticle.getTitle() + "》中的缩略图"); }
if (StringUtils.hasText(oldThumbnail) && StringUtils.hasText(newThumbnail) && !oldThumbnail.equals(newThumbnail)) { ossFileService.deleteFileByUrl(oldThumbnail); deleteFileRecordByUrl(oldThumbnail); log.info("删除旧的缩略图并同步文件表: {}", oldThumbnail); }
Article article = BeanCopyUtils.copyBean(updatePostsDto, Article.class); articleService.updateById(article);
LambdaQueryWrapper<ArticleTag> deleteWrapper = new LambdaQueryWrapper<>(); deleteWrapper.eq(ArticleTag::getArticleId, updatePostsDto.getId()); articleTagMapper.delete(deleteWrapper);
if (updatePostsDto.getTags() != null && !updatePostsDto.getTags().isEmpty()) { List<ArticleTag> articleTags = updatePostsDto.getTags().stream() .map(tagId -> new ArticleTag(article.getId(), tagId)) .collect(Collectors.toList()); articleTagService.saveBatch(articleTags); }
return ResponseResult.okResult(); }
@Override @Transactional public ResponseResult deletePosts(Long[] ids) { if (ids == null || ids.length == 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "请选择要删除的文章"); }
List<Long> idList = List.of(ids);
List<Article> articlesToDelete = articleService.listByIds(idList); if (articlesToDelete.size() != ids.length) { Set<Long> existIds = articlesToDelete.stream() .map(Article::getId) .collect(Collectors.toSet());
List<Long> notExistIds = idList.stream() .filter(id -> !existIds.contains(id)) .toList();
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除失败!!原因:以下文章 id 不存在:" + notExistIds); }
for (Long articleId : idList) { Article article = articleService.getById(articleId);
Set<String> imageUrlSet = new LinkedHashSet<>(); if (StringUtils.hasText(article.getContent())) { List<String> contentImages = ImageUrlUtils.extractImageUrls(article.getContent()); imageUrlSet.addAll(contentImages); log.info("文章ID: {} 内容中提取到的图片URL: {}", articleId, contentImages); } if (StringUtils.hasText(article.getThumbnail())) { imageUrlSet.add(article.getThumbnail()); log.info("文章ID: {} 缩略图URL: {}", articleId, article.getThumbnail()); }
List<String> imageUrls = new ArrayList<>(imageUrlSet); log.info("文章ID: {} 去重后的图片URL: {}", articleId, imageUrls);
if (!imageUrls.isEmpty()) { int movedCount = ossFileService.batchMoveFilesToDeleted(imageUrls); log.info("文章ID: {} 的图片已移动到deleted文件夹,成功: {}/{}", articleId, movedCount, imageUrls.size());
for (String imageUrl : imageUrls) { deleteFileRecordByUrl(imageUrl); } log.info("文章ID: {} 的图片文件表记录已删除,共 {} 条", articleId, imageUrls.size()); }
LambdaQueryWrapper<ArticleTag> deleteWrapper = new LambdaQueryWrapper<>(); deleteWrapper.eq(ArticleTag::getArticleId, articleId); int deletedCount = articleTagMapper.delete(deleteWrapper); log.info("删除文章ID: {} 的标签关联记录,共 {} 条", articleId, deletedCount); }
articleService.removeByIds(idList); log.info("批量逻辑删除文章成功,ID列表: {}", idList);
return ResponseResult.okResult(); }
private String processContentImagesAndSaveRecords(String content, String postTitle) { List<String> tempImageUrls = ImageUrlUtils.extractTempImageUrls(content); String result = content;
List<String> distinctTempImageUrls = tempImageUrls.stream().distinct().collect(Collectors.toList());
for (String tempUrl : distinctTempImageUrls) { UploadFileMetaVo uploadFileMetaVo = ossFileService.moveTempToFormal(tempUrl);
if (uploadFileMetaVo == null || !StringUtils.hasText(uploadFileMetaVo.getUrl())) { throw new RuntimeException("图片保存失败,无法保存文章内容中的图片记录: " + tempUrl); }
result = ImageUrlUtils.replaceImageUrl(result, tempUrl, uploadFileMetaVo.getUrl()); if (StringUtils.hasText(uploadFileMetaVo.getFilePath())) { String fileSource = "文章《" + postTitle + "》中的图片"; saveFileRecord(uploadFileMetaVo.getName(), uploadFileMetaVo.getFilePath(), uploadFileMetaVo.getUrl(), uploadFileMetaVo.getSize(), uploadFileMetaVo.getMimeType(), fileSource); } }
return result; }
private void saveFileRecord(String fileName, String filePath, String url, Long size, String mimeType, String fileSource) { SysFile sysFile = new SysFile(); sysFile.setName(fileName); sysFile.setFilePath(filePath); sysFile.setUrl(url); sysFile.setSize(size); sysFile.setMimeType(mimeType); sysFile.setFileSource(fileSource); sysFile.setType(SystemConstants.FILE_TYPE_IMAGE); sysFileService.save(sysFile); }
private void deleteFileRecordByUrl(String url) { if (!StringUtils.hasText(url)) { return; }
LambdaQueryWrapper<SysFile> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysFile::getUrl, url); sysFileService.remove(queryWrapper); }
|