src/main/java: Minimum working version of FixJpgJpgThumbnails

It goes through each item and checks the THUMBNAIL bundle to find
any bitstreams ending in ".jpg.jpg", which indicates that they are
a thumbnail of a thumbnail. For each match it checks the ORIGINAL
bundle for a bitstream with the same name (minus ".jpg") and then
moves it to the THUMBNAIL bundle and deletes the original as well
as the "JpgJpg" thumbnail.
This commit is contained in:
Alan Orth 2020-08-06 13:53:58 +03:00
parent 08e7546a87
commit b790d5e4db
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9
1 changed files with 25 additions and 23 deletions

View File

@ -72,32 +72,34 @@ public class FixJpgJpgThumbnails {
private static void processItem(Item item) throws SQLException, AuthorizeException, IOException { private static void processItem(Item item) throws SQLException, AuthorizeException, IOException {
Bundle[] thumbnailBundles = item.getBundles("THUMBNAIL"); Bundle[] thumbnailBundles = item.getBundles("THUMBNAIL");
for (Bundle bundle : thumbnailBundles) { for (Bundle thumbnailBundle : thumbnailBundles) {
//System.out.println(Arrays.toString(bundle.getItems())); Bitstream[] thumbnailBundleBitstreams = thumbnailBundle.getBitstreams();
//[org.dspace.content.Item@92944ff4] for (Bitstream thumbnailBitstream : thumbnailBundleBitstreams) {
Bitstream[] bitstreams = bundle.getBitstreams(); String thumbnailName = thumbnailBitstream.getName();
for (Bitstream bitstream : bitstreams) {
//System.out.println(bitstream.getName()); if (thumbnailName.contains(".jpg.jpg")) {
//AgricSystems.jpg.jpg Bundle[] originalBundles = item.getBundles("ORIGINAL");
if ("image/png".equals(bitstream.getFormat().getMIMEType()) && "Generated Thumbnail".equals(bitstream.getDescription())) { for (Bundle originalBundle : originalBundles) {
String bitstreamName = bitstream.getName(); Bitstream[] originalBundleBitstreams = originalBundle.getBitstreams();
if (hasJpegThumbnail(thumbnailBundles, bitstreamName)) {
bundle.removeBitstream(bitstream); for(Bitstream originalBitstream : originalBundleBitstreams) {
System.out.println("Removed generated PDF thumbnail " + bitstreamName + " from item id=" + item.getID() + ", it has a new JPG thumbnail"); String originalName = originalBitstream.getName();
//check if the original file name is the same as the thumbnail name minus the extra ".jpg"
if (originalName.equals(StringUtils.removeEndIgnoreCase(thumbnailName, ".jpg")) && "Generated Thumbnail".equals(thumbnailBitstream.getDescription())) {
System.out.println(item.getHandle() + ": replacing " + thumbnailName + " with " + originalName);
//add the original bitstream to the THUMBNAIL bundle
thumbnailBundle.addBitstream(originalBitstream);
//remove the original bitstream from the ORIGINAL bundle
originalBundle.removeBitstream(originalBitstream);
//remove the JpgJpg bitstream from the THUMBNAIL bundle
thumbnailBundle.removeBitstream(thumbnailBitstream);
}
}
} }
} }
} }
} }
} }
private static boolean hasJpegThumbnail(Bundle[] thumbnailBundles, String bitstreamName) {
String jpgName = StringUtils.removeEndIgnoreCase(bitstreamName, ".png") + ".jpg";
for (Bundle bundle : thumbnailBundles) {
Bitstream candidate = bundle.getBitstreamByName(jpgName);
if (candidate != null) {
return true;
}
}
return false;
}
} }