@@ -90,7 +90,18 @@ public static List<DatasetThumbnail> getThumbnailCandidates(Dataset dataset, boo
9090 }
9191 IOUtils .closeQuietly (in );
9292 }
93- for (FileMetadata fileMetadata : dataset .getLatestVersion ().getFileMetadatas ()) {
93+ // Use database-level filtering to get only thumbnail-candidate files (images, PDFs)
94+ // instead of iterating through all files in the dataset.
95+ // Fall back to iterating all files if CDI is not available (e.g., unit tests).
96+ List <FileMetadata > candidateFileMetadatas ;
97+ try {
98+ DataFileServiceBean dataFileService = CDI .current ().select (DataFileServiceBean .class ).get ();
99+ candidateFileMetadatas = dataFileService .findThumbnailCandidateFileMetadatasByDatasetVersionId (dataset .getLatestVersion ().getId ());
100+ } catch (IllegalStateException ex ) {
101+ // CDI not available (e.g., unit tests) - fall back to iterating all files
102+ candidateFileMetadatas = dataset .getLatestVersion ().getFileMetadatas ();
103+ }
104+ for (FileMetadata fileMetadata : candidateFileMetadatas ) {
94105 DataFile dataFile = fileMetadata .getDataFile ();
95106
96107 if (dataFile != null && FileUtil .isThumbnailSupported (dataFile )
@@ -111,6 +122,47 @@ public static List<DatasetThumbnail> getThumbnailCandidates(Dataset dataset, boo
111122 return thumbnails ;
112123 }
113124
125+ /**
126+ * Returns thumbnail candidates as lazy DatasetThumbnail objects that hold
127+ * only DataFile references. Base64 images are generated on-demand when
128+ * getBase64image() is called, deferring expensive storage I/O until the
129+ * thumbnail is actually displayed.
130+ *
131+ * @param dataset the dataset to find thumbnail candidates for
132+ * @return list of lazy DatasetThumbnail objects (base64 not pre-computed)
133+ */
134+ public static List <DatasetThumbnail > getLazyThumbnailCandidates (Dataset dataset ) {
135+ List <DatasetThumbnail > thumbnails = new ArrayList <>();
136+ if (dataset == null ) {
137+ return thumbnails ;
138+ }
139+ // Use database-level filtering to get only thumbnail-candidate files (images, PDFs)
140+ // instead of iterating through all files in the dataset.
141+ // Fall back to iterating all files if CDI is not available (e.g., unit tests).
142+ List <FileMetadata > candidateFileMetadatas ;
143+ try {
144+ DataFileServiceBean dataFileService = CDI .current ().select (DataFileServiceBean .class ).get ();
145+ candidateFileMetadatas = dataFileService .findThumbnailCandidateFileMetadatasByDatasetVersionId (dataset .getLatestVersion ().getId ());
146+ } catch (IllegalStateException ex ) {
147+ // CDI not available (e.g., unit tests) - fall back to iterating all files
148+ candidateFileMetadatas = dataset .getLatestVersion ().getFileMetadatas ();
149+ }
150+ for (FileMetadata fileMetadata : candidateFileMetadatas ) {
151+ DataFile dataFile = fileMetadata .getDataFile ();
152+
153+ if (dataFile != null && FileUtil .isThumbnailSupported (dataFile )
154+ && !dataFile .isRestricted ()
155+ && !FileUtil .isActivelyEmbargoed (dataFile )
156+ && ImageThumbConverter .isThumbnailAvailable (dataFile )
157+ ) {
158+ // Create lazy thumbnail — base64 will be generated on first access
159+ DatasetThumbnail datasetThumbnail = new DatasetThumbnail (dataFile );
160+ thumbnails .add (datasetThumbnail );
161+ }
162+ }
163+ return thumbnails ;
164+ }
165+
114166 /**
115167 * Note "datasetVersionId" can be null. If needed, it helps the "efficiency"
116168 * of "attemptToAutomaticallySelectThumbnailFromDataFiles"
@@ -259,14 +311,25 @@ public static DataFile attemptToAutomaticallySelectThumbnailFromDataFiles(Datase
259311 return null ;
260312 }
261313
262- for (FileMetadata fmd : datasetVersion .getFileMetadatas ()) {
314+ // Use database-level filtering to get only thumbnail-candidate files (images, PDFs)
315+ // instead of iterating through all files in the dataset.
316+ // Fall back to iterating all files if CDI is not available (e.g., unit tests).
317+ List <FileMetadata > candidateFileMetadatas ;
318+ try {
319+ DataFileServiceBean dataFileService = CDI .current ().select (DataFileServiceBean .class ).get ();
320+ candidateFileMetadatas = dataFileService .findThumbnailCandidateFileMetadatasByDatasetVersionId (datasetVersion .getId ());
321+ } catch (IllegalStateException ex ) {
322+ // CDI not available (e.g., unit tests) - fall back to iterating all files
323+ candidateFileMetadatas = datasetVersion .getFileMetadatas ();
324+ }
325+ for (FileMetadata fmd : candidateFileMetadatas ) {
263326 DataFile testFile = fmd .getDataFile ();
264327 // We don't want to use a restricted image file as the dedicated thumbnail:
265328 if (!testFile .isRestricted () && !FileUtil .isActivelyEmbargoed (testFile ) && FileUtil .isThumbnailSupported (testFile ) && ImageThumbConverter .isThumbnailAvailable (testFile , ImageThumbConverter .DEFAULT_DATASETLOGO_SIZE )) {
266329 return testFile ;
267330 }
268331 }
269- logger .fine ("In attemptToAutomaticallySelectThumbnailFromDataFiles and interated through all the files but couldn't find a thumbnail." );
332+ logger .fine ("In attemptToAutomaticallySelectThumbnailFromDataFiles and iterated through all candidate files but couldn't find a thumbnail." );
270333 return null ;
271334 }
272335
0 commit comments