Skip to content

Commit

Permalink
Add PR 10797, 10820 from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosel committed Sep 12, 2024
1 parent cf245f7 commit 34a7a6e
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 121 deletions.
5 changes: 5 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import edu.harvard.iq.dataverse.license.License;
import edu.harvard.iq.dataverse.makedatacount.DatasetExternalCitations;
import edu.harvard.iq.dataverse.makedatacount.DatasetMetrics;
import edu.harvard.iq.dataverse.settings.FeatureFlags;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
Expand Down Expand Up @@ -206,6 +207,10 @@ public Dataset(boolean isHarvested) {
StorageUse storageUse = new StorageUse(this);
this.setStorageUse(storageUse);
}

if (FeatureFlags.DISABLE_DATASET_THUMBNAIL_AUTOSELECT.enabled()) {
this.setUseGenericThumbnail(true);
}
}

/**
Expand Down
140 changes: 71 additions & 69 deletions src/main/java/edu/harvard/iq/dataverse/DatasetVersionServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static edu.harvard.iq.dataverse.batch.jobs.importer.filesystem.FileRecordJobListener.SEP;
import edu.harvard.iq.dataverse.batch.util.LoggingUtil;
import edu.harvard.iq.dataverse.search.SolrSearchResult;
import edu.harvard.iq.dataverse.settings.FeatureFlags;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.MarkupChecker;
Expand Down Expand Up @@ -790,100 +791,101 @@ public Long getThumbnailByVersionId(Long versionId) {
return null;
}

Long thumbnailFileId;

// First, let's see if there are thumbnails that have already been
// generated:
try {
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
+ "WHERE dv.id = " + versionId + " "
+ "AND df.id = o.id "
+ "AND fm.datasetversion_id = dv.id "
+ "AND fm.datafile_id = df.id "
+ "AND df.restricted = false "
+ "AND df.embargo_id is null "
+ "AND df.retention_id is null "
+ "AND o.previewImageAvailable = true "
+ "ORDER BY df.id LIMIT 1;").getSingleResult();
} catch (Exception ex) {
thumbnailFileId = null;
}

if (thumbnailFileId != null) {
logger.fine("DatasetVersionService,getThumbnailByVersionid(): found already generated thumbnail for version " + versionId + ": " + thumbnailFileId);
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
return thumbnailFileId;
}

if (!systemConfig.isThumbnailGenerationDisabledForImages()) {
// OK, let's try and generate an image thumbnail!
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitImage();
if (!FeatureFlags.DISABLE_DATASET_THUMBNAIL_AUTOSELECT.enabled()) {
Long thumbnailFileId;

// First, let's see if there are thumbnails that have already been
// generated:
try {
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
+ "WHERE dv.id = " + versionId + " "
+ "AND df.id = o.id "
+ "AND fm.datasetversion_id = dv.id "
+ "AND fm.datafile_id = df.id "
+ "AND o.previewimagefail = false "
+ "AND df.restricted = false "
+ "AND df.embargo_id is null "
+ "AND df.retention_id is null "
+ "AND df.contenttype LIKE 'image/%' "
+ "AND NOT df.contenttype = 'image/fits' "
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
+ "AND o.previewImageAvailable = true "
+ "ORDER BY df.id LIMIT 1;").getSingleResult();
} catch (Exception ex) {
thumbnailFileId = null;
}

if (thumbnailFileId != null) {
logger.fine("obtained file id: " + thumbnailFileId);
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
if (thumbnailFile != null) {
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
return thumbnailFileId;
logger.fine("DatasetVersionService,getThumbnailByVersionid(): found already generated thumbnail for version " + versionId + ": " + thumbnailFileId);
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
return thumbnailFileId;
}

if (!systemConfig.isThumbnailGenerationDisabledForImages()) {
// OK, let's try and generate an image thumbnail!
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitImage();

try {
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
+ "WHERE dv.id = " + versionId + " "
+ "AND df.id = o.id "
+ "AND fm.datasetversion_id = dv.id "
+ "AND fm.datafile_id = df.id "
+ "AND o.previewimagefail = false "
+ "AND df.restricted = false "
+ "AND df.embargo_id is null "
+ "AND df.retention_id is null "
+ "AND df.contenttype LIKE 'image/%' "
+ "AND NOT df.contenttype = 'image/fits' "
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
} catch (Exception ex) {
thumbnailFileId = null;
}

if (thumbnailFileId != null) {
logger.fine("obtained file id: " + thumbnailFileId);
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
if (thumbnailFile != null) {
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
return thumbnailFileId;
}
}
}
}
}

// And if that didn't work, try the same thing for PDFs:
if (!systemConfig.isThumbnailGenerationDisabledForPDF()) {
// OK, let's try and generate an image thumbnail!
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitPDF();
try {
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
+ "WHERE dv.id = " + versionId + " "
+ "AND df.id = o.id "
+ "AND fm.datasetversion_id = dv.id "
+ "AND fm.datafile_id = df.id "
+ "AND o.previewimagefail = false "
+ "AND df.restricted = false "
+ "AND df.embargo_id is null "
+ "AND df.retention_id is null "
+ "AND df.contenttype = 'application/pdf' "
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
} catch (Exception ex) {
thumbnailFileId = null;
}
// And if that didn't work, try the same thing for PDFs:
if (!systemConfig.isThumbnailGenerationDisabledForPDF()) {
// OK, let's try and generate an image thumbnail!
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitPDF();
try {
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
+ "WHERE dv.id = " + versionId + " "
+ "AND df.id = o.id "
+ "AND fm.datasetversion_id = dv.id "
+ "AND fm.datafile_id = df.id "
+ "AND o.previewimagefail = false "
+ "AND df.restricted = false "
+ "AND df.embargo_id is null "
+ "AND df.retention_id is null "
+ "AND df.contenttype = 'application/pdf' "
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
} catch (Exception ex) {
thumbnailFileId = null;
}

if (thumbnailFileId != null) {
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
if (thumbnailFile != null) {
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
return thumbnailFileId;
if (thumbnailFileId != null) {
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
if (thumbnailFile != null) {
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
return thumbnailFileId;
}
}
}
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,19 @@ public static List<DatasetThumbnail> getThumbnailCandidates(Dataset dataset, boo
*
* @param dataset
* @param datasetVersion
* @return
* @param size of the requested thumbnail
* @return DatasetThumbnail object, or null if not available
*/
public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion datasetVersion, int size) {
if (dataset == null) {
return null;
}

if (size == 0) {
// Size 0 will fail (and set the failure flag) and should never be sent
logger.warning("getThumbnail called with size 0");
return null;
}
StorageIO<Dataset> dataAccess = null;

try{
Expand Down
Loading

0 comments on commit 34a7a6e

Please sign in to comment.