From c9a05408582e313076dfd8e4faebaac6b7952f88 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Thu, 24 Jul 2025 15:52:07 +0800 Subject: [PATCH] Adjust image references when saving or deleting drafts --- .../com/openisle/service/DraftService.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/openisle/service/DraftService.java b/src/main/java/com/openisle/service/DraftService.java index 86efd85c2..f14086218 100644 --- a/src/main/java/com/openisle/service/DraftService.java +++ b/src/main/java/com/openisle/service/DraftService.java @@ -8,6 +8,7 @@ import com.openisle.repository.CategoryRepository; import com.openisle.repository.DraftRepository; import com.openisle.repository.TagRepository; import com.openisle.repository.UserRepository; +import com.openisle.service.ImageUploader; import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -24,12 +25,15 @@ public class DraftService { private final UserRepository userRepository; private final CategoryRepository categoryRepository; private final TagRepository tagRepository; + private final ImageUploader imageUploader; @Transactional public Draft saveDraft(String username, Long categoryId, String title, String content, List tagIds) { User user = userRepository.findByUsername(username) .orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found")); Draft draft = draftRepository.findByAuthor(user).orElse(new Draft()); + String oldContent = draft.getContent(); + boolean existing = draft.getId() != null; draft.setAuthor(user); draft.setTitle(title); draft.setContent(content); @@ -45,7 +49,13 @@ public class DraftService { tags.addAll(tagRepository.findAllById(tagIds)); } draft.setTags(tags); - return draftRepository.save(draft); + Draft saved = draftRepository.save(draft); + if (existing) { + imageUploader.adjustReferences(oldContent == null ? "" : oldContent, content); + } else { + imageUploader.addReferences(imageUploader.extractUrls(content)); + } + return saved; } @Transactional(readOnly = true) @@ -56,7 +66,11 @@ public class DraftService { @Transactional public void deleteDraft(String username) { - userRepository.findByUsername(username) - .ifPresent(draftRepository::deleteByAuthor); + userRepository.findByUsername(username).ifPresent(user -> + draftRepository.findByAuthor(user).ifPresent(draft -> { + imageUploader.removeReferences(imageUploader.extractUrls(draft.getContent())); + draftRepository.delete(draft); + }) + ); } }