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
| @Override @Transactional public ResponseResult updateNotice(Long id, UpdateNoticeDto updateNoticeDto) { SysNotice oldNotice = sysNoticeService.getById(id); if (oldNotice == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该通知不存在!"); }
String content = updateNoticeDto.getContent(); List<String> oldContentImages = new ArrayList<>(); List<String> newContentImages = new ArrayList<>();
if (StringUtils.hasText(oldNotice.getContent())) { oldContentImages = ImageUrlUtils.extractImageUrls(oldNotice.getContent()); }
if (ImageUrlUtils.containsTempImages(content)) { content = processContentImagesAndSaveRecords(content, updateNoticeDto.getTitle()); updateNoticeDto.setContent(content); }
if (StringUtils.hasText(content)) { newContentImages = ImageUrlUtils.extractImageUrls(content); }
SysNotice notice = BeanCopyUtils.copyBean(updateNoticeDto, SysNotice.class); notice.setId(id); boolean updated = sysNoticeService.updateById(notice); if (!updated) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "更新通知失败"); }
if (StringUtils.hasText(oldNotice.getTitle()) && StringUtils.hasText(updateNoticeDto.getTitle()) && !oldNotice.getTitle().equals(updateNoticeDto.getTitle())) { syncFileSourceByTitle(oldNotice.getTitle(), updateNoticeDto.getTitle()); }
for (String oldImageUrl : oldContentImages) { if (!newContentImages.contains(oldImageUrl)) { ossFileService.deleteFileByUrl(oldImageUrl); deleteFileRecordByUrl(oldImageUrl); log.info("删除旧的内容图片并同步文件表: {}", oldImageUrl); } }
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().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 deleteFileRecordByUrl(String url) { if (!StringUtils.hasText(url)) { return; }
LambdaQueryWrapper<SysFile> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysFile::getUrl, url); sysFileService.remove(queryWrapper); }
private void syncFileSourceByTitle(String oldTitle, String newTitle) { String oldPostPrefix = "通知《" + oldTitle + "》"; String newPostPrefix = "通知《" + newTitle + "》";
List<SysFile> fileList = sysFileService.list(new LambdaQueryWrapper<SysFile>() .like(SysFile::getFileSource, oldPostPrefix));
if (fileList.isEmpty()) { return; }
for (SysFile sysFile : fileList) { String fileSource = sysFile.getFileSource(); if (StringUtils.hasText(fileSource)) { sysFile.setFileSource(fileSource.replace(oldPostPrefix, newPostPrefix)); } }
sysFileService.updateBatchById(fileList); }
|