private Map<Album, Map<ImageType, Resource>> collectAlbumTargets( Library library, final ResourceLocator resourceLocator, Collection<Album> changedAlbums) { Map<Album, Map<ImageType, Resource>> result = new HashMap<>(); for (Album album : library.getAlbums()) { Resource artworkAssetResource = album.artworkAssetResource(); if (artworkAssetResource != null) { Map<ImageType, Resource> targets = new HashMap<ImageType, Resource>(); for (ImageType type : ImageType.values()) { String imagePath = resourceLocator.getAlbumImagePath(album, type); if (imagePath != null) { Resource resource = resourceLocator.getResource(imagePath); try { if (changedAlbums.contains(album) || !resource.exists()) { resource.getParent().mkdirs(); targets.put(type, resource); } } catch (IOException e) { LOGGER.warning("Could not write image file: " + resource.getPath().toAbsolutePath()); } } } if (!targets.isEmpty()) { result.put(album, targets); } } } return result; }
private void deleteIfExists(Resource imageResource) { try { if (imageResource.exists()) { imageResource.delete(); } } catch (IOException e) { LOGGER.warning("Could not delete image file: " + imageResource.getPath().toAbsolutePath()); } }
private void formatImages(BufferedImage image, Map<ImageType, Resource> targets) { for (Map.Entry<ImageType, Resource> targetEntry : targets.entrySet()) { ImageType imageType = targetEntry.getKey(); Resource imageResource = targetEntry.getValue(); try (OutputStream output = imageResource.getOutputStream()) { writeImage(image, imageType, output); } catch (IOException e) { LOGGER.log( Level.WARNING, "Could not write image file: " + imageResource.getPath().toAbsolutePath(), e); deleteIfExists(imageResource); } } }