From 360b69effc6a34e35cd0349afc70760af12c2d81 Mon Sep 17 00:00:00 2001 From: Stefan Hardegger Date: Sat, 27 Sep 2025 08:15:09 +0200 Subject: [PATCH] fix cleanup --- .../com/storycove/service/ImageService.java | 109 +++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/com/storycove/service/ImageService.java b/backend/src/main/java/com/storycove/service/ImageService.java index bf09224..50db65f 100644 --- a/backend/src/main/java/com/storycove/service/ImageService.java +++ b/backend/src/main/java/com/storycove/service/ImageService.java @@ -42,6 +42,12 @@ public class ImageService { @Autowired private StoryService storyService; + + @Autowired + private AuthorService authorService; + + @Autowired + private CollectionService collectionService; private String getUploadDir() { String libraryPath = libraryService.getCurrentImagePath(); @@ -590,6 +596,25 @@ public class ImageService { Pattern imagePattern = Pattern.compile("src=[\"']([^\"']*(?:content/[^\"']*\\.(jpg|jpeg|png)))[\"']", Pattern.CASE_INSENSITIVE); for (com.storycove.entity.Story story : allStories) { + // Add story cover image if present + if (story.getCoverPath() != null && !story.getCoverPath().trim().isEmpty()) { + String relativePath = convertAbsolutePathToRelative(story.getCoverPath()); + if (relativePath != null) { + referencedImages.add(relativePath); + logger.debug("Found cover image reference in story {}: {}", story.getId(), relativePath); + } + } + + // Add author avatar image if present + if (story.getAuthor() != null && story.getAuthor().getAvatarImagePath() != null && !story.getAuthor().getAvatarImagePath().trim().isEmpty()) { + String relativePath = convertAbsolutePathToRelative(story.getAuthor().getAvatarImagePath()); + if (relativePath != null) { + referencedImages.add(relativePath); + logger.debug("Found avatar image reference for author {}: {}", story.getAuthor().getId(), relativePath); + } + } + + // Add content images from HTML if (story.getContentHtml() != null) { Matcher matcher = imagePattern.matcher(story.getContentHtml()); @@ -600,12 +625,36 @@ public class ImageService { String relativePath = convertSrcToRelativePath(imageSrc); if (relativePath != null) { referencedImages.add(relativePath); - logger.debug("Found image reference in story {}: {}", story.getId(), relativePath); + logger.debug("Found content image reference in story {}: {}", story.getId(), relativePath); } } } } + // Also get all authors separately to catch avatars for authors without stories + List allAuthors = authorService.findAll(); + for (com.storycove.entity.Author author : allAuthors) { + if (author.getAvatarImagePath() != null && !author.getAvatarImagePath().trim().isEmpty()) { + String relativePath = convertAbsolutePathToRelative(author.getAvatarImagePath()); + if (relativePath != null) { + referencedImages.add(relativePath); + logger.debug("Found standalone avatar image reference for author {}: {}", author.getId(), relativePath); + } + } + } + + // Also get all collections to catch cover images + List allCollections = collectionService.findAll(); + for (com.storycove.entity.Collection collection : allCollections) { + if (collection.getCoverImagePath() != null && !collection.getCoverImagePath().trim().isEmpty()) { + String relativePath = convertAbsolutePathToRelative(collection.getCoverImagePath()); + if (relativePath != null) { + referencedImages.add(relativePath); + logger.debug("Found collection cover image reference for collection {}: {}", collection.getId(), relativePath); + } + } + } + } catch (Exception e) { logger.error("Error collecting image references from stories", e); } @@ -629,6 +678,64 @@ public class ImageService { return null; } + /** + * Convert absolute file path to relative path from upload directory + */ + private String convertAbsolutePathToRelative(String absolutePath) { + try { + if (absolutePath == null || absolutePath.trim().isEmpty()) { + return null; + } + + Path absPath = Paths.get(absolutePath); + Path uploadDirPath = Paths.get(getUploadDir()); + + // If the path is already relative to upload dir, return as-is + if (!absPath.isAbsolute()) { + return absolutePath.replace('\\', '/'); + } + + // Try to make it relative to the upload directory + if (absPath.startsWith(uploadDirPath)) { + Path relativePath = uploadDirPath.relativize(absPath); + return relativePath.toString().replace('\\', '/'); + } + + // If it's not under upload directory, check if it's library-specific path + String libraryPath = libraryService.getCurrentImagePath(); + Path baseUploadPath = Paths.get(baseUploadDir); + + if (absPath.startsWith(baseUploadPath)) { + Path relativePath = baseUploadPath.relativize(absPath); + String relativeStr = relativePath.toString().replace('\\', '/'); + + // Remove library prefix if present to make it library-agnostic for comparison + if (relativeStr.startsWith(libraryPath.substring(1))) { // Remove leading slash from library path + return relativeStr.substring(libraryPath.length() - 1); // Keep the leading slash + } + return relativeStr; + } + + // Fallback: just use the filename portion if it's in the right structure + String fileName = absPath.getFileName().toString(); + if (fileName.matches(".*\\.(jpg|jpeg|png)$")) { + // Try to preserve directory structure if it looks like covers/ or avatars/ + Path parent = absPath.getParent(); + if (parent != null) { + String parentName = parent.getFileName().toString(); + if (parentName.equals("covers") || parentName.equals("avatars")) { + return parentName + "/" + fileName; + } + } + return fileName; + } + + } catch (Exception e) { + logger.debug("Failed to convert absolute path to relative: {}", absolutePath, e); + } + return null; + } + /** * Get relative image path from absolute file path */