Skip to content

Commit 0a29fa4

Browse files
committed
assure flags are set from API, cleanup
1 parent 6559d9c commit 0a29fa4

1 file changed

Lines changed: 35 additions & 24 deletions

File tree

‎src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java‎

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,7 @@ private static boolean isThumbnailAvailable(StorageIO<DataFile> storageIO, int s
124124
logger.fine("Found cached thumbnail for " + file.getId());
125125
// Thumbnail exists in storage but the database flag may not be set — persist it now
126126
if (!file.isPreviewImageAvailable()) {
127-
file.setPreviewImageAvailable(true);
128-
try {
129-
DataFileServiceBean datafileService = CDI.current().select(DataFileServiceBean.class).get();
130-
datafileService.save(file);
131-
logger.fine("Marked previewImageAvailable = true for cached thumbnail on file " + file.getId());
132-
} catch (Exception ex) {
133-
logger.warning("Failed to persist previewImageAvailable flag for file " + file.getId() + ": " + ex.getMessage());
134-
}
127+
markThumbnailGenerationStatus(file, true);
135128
}
136129
return true;
137130
}
@@ -150,25 +143,44 @@ private static boolean generateThumbnail(DataFile file, StorageIO<DataFile> stor
150143
thumbnailGenerated = generatePDFThumbnail(storageIO, size);
151144
}
152145
if (thumbnailGenerated) {
153-
file.setPreviewImageAvailable(true);
154-
file.setPreviewImageFail(false);
155-
logger.fine("Thumbnail generated for " + file.getId() + ", marking previewImageAvailable = true");
146+
markThumbnailGenerationStatus(file, true);
156147
} else {
157-
file.setPreviewImageFail(true);
158-
file.setPreviewImageAvailable(false);
159-
logger.fine("No thumbnail generated for " + file.getId());
160-
}
161-
try {
162-
DataFileServiceBean datafileService = CDI.current().select(DataFileServiceBean.class).get();
163-
datafileService.save(file);
164-
} catch (Exception ex) {
165-
logger.warning("Failed to persist previewImageAvailable flag for file " + file.getId() + ": " + ex.getMessage());
148+
markThumbnailGenerationStatus(file, false);
149+
return false;
166150
}
167151
}
168152

169153
return thumbnailGenerated;
170154
}
171155

156+
/**
157+
* Marks a file's thumbnail generation status and persists the change.
158+
* Used to avoid having file saves scattered throughout the thumbnail generation code.
159+
*
160+
* @param file the data file to update
161+
* @param success true if thumbnail generation succeeded, false if it failed
162+
*/
163+
private static void markThumbnailGenerationStatus(DataFile file, boolean success) {
164+
if (file == null) {
165+
return;
166+
}
167+
if (success) {
168+
file.setPreviewImageAvailable(true);
169+
file.setPreviewImageFail(false);
170+
logger.fine("Marking thumbnail as available for file " + file.getId());
171+
} else {
172+
file.setPreviewImageFail(true);
173+
file.setPreviewImageAvailable(false);
174+
logger.fine("Marking thumbnail as failed for file " + file.getId());
175+
}
176+
try {
177+
DataFileServiceBean datafileService = CDI.current().select(DataFileServiceBean.class).get();
178+
datafileService.save(file);
179+
} catch (Exception ex) {
180+
logger.warning("Failed to persist thumbnail status for file " + file.getId() + ": " + ex.getMessage());
181+
}
182+
}
183+
172184
// Note that this method works on ALL file types for which thumbnail
173185
// generation is supported - image/*, pdf;
174186
// not just on images! The type differentiation is handled inside
@@ -212,10 +224,7 @@ public static InputStreamIO getImageThumbnailAsInputStream(StorageIO<DataFile> s
212224
// We found an older 0 length thumbnail. Newer image uploads will not have this issue.
213225
// Once cleaned up, this thumbnail will no longer have this issue
214226
logger.warning("Cleaning up zero sized thumbnail ID: "+ storageIO.getDataFile().getId());
215-
storageIO.getDataFile().setPreviewImageFail(true);
216-
storageIO.getDataFile().setPreviewImageAvailable(false);
217-
DataFileServiceBean datafileService = CDI.current().select(DataFileServiceBean.class).get();
218-
datafileService.save(storageIO.getDataFile());
227+
markThumbnailGenerationStatus(storageIO.getDataFile(), false);
219228

220229
// Now that we have marked this File as a thumbnail failure,
221230
// no reason not to try and delete this 0-size cache here:
@@ -480,10 +489,12 @@ public static String getImageThumbnailAsBase64(DataFile file, int size) {
480489
storageIO = file.getStorageIO();
481490
} catch (Exception ioEx) {
482491
logger.fine("Caught an exception while trying to obtain a thumbnail as Base64 string - could not open StorageIO on the datafile.");
492+
markThumbnailGenerationStatus(file, false);
483493
return null;
484494
}
485495

486496
if (storageIO == null) {
497+
markThumbnailGenerationStatus(file, false);
487498
return null;
488499
}
489500

0 commit comments

Comments
 (0)