|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Alan Orth |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: GPL-3.0-only |
| 5 | + */ |
| 6 | + |
| 7 | +package io.github.ilri.cgspace.scripts; |
| 8 | + |
| 9 | +import org.apache.commons.lang.StringUtils; |
| 10 | +import org.dspace.authorize.AuthorizeException; |
| 11 | +import org.dspace.content.Bitstream; |
| 12 | +import org.dspace.content.Bundle; |
| 13 | +import org.dspace.content.Collection; |
| 14 | +import org.dspace.content.Community; |
| 15 | +import org.dspace.content.DSpaceObject; |
| 16 | +import org.dspace.content.Item; |
| 17 | +import org.dspace.content.MetadataValue; |
| 18 | +import org.dspace.content.factory.ContentServiceFactory; |
| 19 | +import org.dspace.content.service.BundleService; |
| 20 | +import org.dspace.content.service.ItemService; |
| 21 | +import org.dspace.core.Constants; |
| 22 | +import org.dspace.core.Context; |
| 23 | +import org.dspace.handle.factory.HandleServiceFactory; |
| 24 | +import org.dspace.handle.service.HandleService; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.sql.SQLException; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Andrea Schweer [email protected] for the LCoNZ Institutional Research Repositories |
| 33 | + * @author Alan Orth for the International Livestock Research Institute |
| 34 | + * @version 7.6.1.4 |
| 35 | + * @since 7.6.1.4 |
| 36 | + */ |
| 37 | +public class RemoveGeneratedThumbnails { |
| 38 | + // note: static members belong to the class itself, not any one instance |
| 39 | + public static ItemService itemService = ContentServiceFactory.getInstance().getItemService(); |
| 40 | + public static HandleService handleService = |
| 41 | + HandleServiceFactory.getInstance().getHandleService(); |
| 42 | + public static BundleService bundleService = |
| 43 | + ContentServiceFactory.getInstance().getBundleService(); |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + String parentHandle = null; |
| 47 | + if (args.length >= 1) { |
| 48 | + parentHandle = args[0]; |
| 49 | + } |
| 50 | + |
| 51 | + Context context = null; |
| 52 | + try { |
| 53 | + context = new Context(); |
| 54 | + context.turnOffAuthorisationSystem(); |
| 55 | + |
| 56 | + if (StringUtils.isBlank(parentHandle)) { |
| 57 | + process(context, itemService.findAll(context)); |
| 58 | + } else { |
| 59 | + DSpaceObject parent = handleService.resolveToObject(context, parentHandle); |
| 60 | + if (parent != null) { |
| 61 | + switch (parent.getType()) { |
| 62 | + case Constants.SITE: |
| 63 | + process(context, itemService.findAll(context)); |
| 64 | + context.commit(); |
| 65 | + break; |
| 66 | + case Constants.COMMUNITY: |
| 67 | + List<Collection> collections = ((Community) parent).getCollections(); |
| 68 | + for (Collection collection : collections) { |
| 69 | + process( |
| 70 | + context, |
| 71 | + itemService.findAllByCollection(context, collection)); |
| 72 | + } |
| 73 | + context.commit(); |
| 74 | + break; |
| 75 | + case Constants.COLLECTION: |
| 76 | + process( |
| 77 | + context, |
| 78 | + itemService.findByCollection(context, (Collection) parent)); |
| 79 | + context.commit(); |
| 80 | + break; |
| 81 | + case Constants.ITEM: |
| 82 | + processItem(context, (Item) parent); |
| 83 | + context.commit(); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } catch (SQLException | AuthorizeException | IOException e) { |
| 89 | + e.printStackTrace(System.err); |
| 90 | + } finally { |
| 91 | + if (context != null && context.isValid()) { |
| 92 | + context.abort(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static void process(Context context, Iterator<Item> items) |
| 98 | + throws SQLException, IOException, AuthorizeException { |
| 99 | + while (items.hasNext()) { |
| 100 | + Item item = items.next(); |
| 101 | + processItem(context, item); |
| 102 | + itemService.update(context, item); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static void processItem(Context context, Item item) |
| 107 | + throws SQLException, AuthorizeException, IOException { |
| 108 | + List<Bundle> thumbnailBundles = item.getBundles("THUMBNAIL"); |
| 109 | + for (Bundle thumbnailBundle : thumbnailBundles) { |
| 110 | + List<Bitstream> thumbnailBundleBitstreams = thumbnailBundle.getBitstreams(); |
| 111 | + for (Bitstream thumbnailBitstream : thumbnailBundleBitstreams) { |
| 112 | + String thumbnailName = thumbnailBitstream.getName(); |
| 113 | + String thumbnailDescription = thumbnailBitstream.getDescription(); |
| 114 | + |
| 115 | + // There is no point continuing if the thumbnail's description is empty or null |
| 116 | + if (StringUtils.isEmpty(thumbnailDescription)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if (thumbnailName.toLowerCase().endsWith(".pdf.jpg")) { |
| 121 | + List<Bundle> originalBundles = item.getBundles("ORIGINAL"); |
| 122 | + for (Bundle originalBundle : originalBundles) { |
| 123 | + List<Bitstream> originalBundleBitstreams = originalBundle.getBitstreams(); |
| 124 | + |
| 125 | + for (Bitstream originalBitstream : originalBundleBitstreams) { |
| 126 | + String originalName = originalBitstream.getName(); |
| 127 | + |
| 128 | + /* |
| 129 | + - check if the original file name is the same as the thumbnail name minus the extra ".jpg" |
| 130 | + - check if the thumbnail description indicates it was automatically generated |
| 131 | + */ |
| 132 | + if (originalName.equalsIgnoreCase( |
| 133 | + StringUtils.removeEndIgnoreCase(thumbnailName, ".jpg")) |
| 134 | + && ("Generated Thumbnail".equals(thumbnailDescription) |
| 135 | + || "IM Thumbnail".equals(thumbnailDescription))) { |
| 136 | + System.out.println( |
| 137 | + item.getHandle() |
| 138 | + + ": removing " |
| 139 | + + thumbnailName); |
| 140 | + |
| 141 | + thumbnailBundle.removeBitstream(thumbnailBitstream); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments