Skip to content

Commit

Permalink
updates from IQSS#9669
Browse files Browse the repository at this point in the history
  • Loading branch information
qqmyers committed Dec 5, 2023
1 parent 2f5e9d6 commit 889cbd2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public String getThumbnailString() {

thumbnailString = datasetThumbnail.getBase64image();
} else {
thumbnailString = thumbnailServiceWrapper.getDatasetCardImageAsBase64Url(dataset,
thumbnailString = thumbnailServiceWrapper.getDatasetCardImageAsUrl(dataset,
workingVersion.getId(),
!workingVersion.isDraft(),
ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
*/
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.dataaccess.DataAccess;
import edu.harvard.iq.dataverse.dataaccess.StorageIO;
import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter;
import static edu.harvard.iq.dataverse.dataset.DatasetUtil.datasetLogoThumbnail;

import edu.harvard.iq.dataverse.search.SolrSearchResult;
import edu.harvard.iq.dataverse.util.FileUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand All @@ -24,7 +18,6 @@
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.apache.commons.io.IOUtils;

/**
*
Expand All @@ -51,49 +44,6 @@ public class ThumbnailServiceWrapper implements java.io.Serializable {
private Map<Long, DvObject> dvobjectViewMap = new HashMap<>();
private Map<Long, Boolean> hasThumbMap = new HashMap<>();

private String getAssignedDatasetImage(Dataset dataset, int size) {
if (dataset == null) {
return null;
}

DataFile assignedThumbnailFile = dataset.getThumbnailFile();

if (assignedThumbnailFile != null) {
Long assignedThumbnailFileId = assignedThumbnailFile.getId();

if (this.dvobjectThumbnailsMap.containsKey(assignedThumbnailFileId)) {
// Yes, return previous answer
//logger.info("using cached result for ... "+assignedThumbnailFileId);
if (!"".equals(this.dvobjectThumbnailsMap.get(assignedThumbnailFileId))) {
return this.dvobjectThumbnailsMap.get(assignedThumbnailFileId);
}
return null;
}

String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(assignedThumbnailFile,
size);
//ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);

if (imageSourceBase64 != null) {
this.dvobjectThumbnailsMap.put(assignedThumbnailFileId, imageSourceBase64);
return imageSourceBase64;
}

// OK - we can't use this "assigned" image, because of permissions, or because
// the thumbnail failed to generate, etc... in this case we'll
// mark this dataset in the lookup map - so that we don't have to
// do all these lookups again...
this.dvobjectThumbnailsMap.put(assignedThumbnailFileId, "");

// TODO: (?)
// do we need to cache this datafile object in the view map?
// -- L.A., 4.2.2
}

return null;

}

// it's the responsibility of the user - to make sure the search result
// passed to this method is of the Datafile type!
public String getFileCardImageAsBase64Url(SolrSearchResult result) {
Expand Down Expand Up @@ -169,7 +119,7 @@ public boolean isThumbnailAvailable(DataFile entity) {

// it's the responsibility of the user - to make sure the search result
// passed to this method is of the Dataset type!
public String getDatasetCardImageAsBase64Url(SolrSearchResult result) {
public String getDatasetCardImageAsUrl(SolrSearchResult result) {
// Before we do anything else, check if it's a harvested dataset;
// no need to check anything else if so (harvested datasets never have
// thumbnails)
Expand All @@ -191,10 +141,10 @@ public String getDatasetCardImageAsBase64Url(SolrSearchResult result) {

Long versionId = result.getDatasetVersionId();

return getDatasetCardImageAsBase64Url(dataset, versionId, result.isPublishedState(), ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
return getDatasetCardImageAsUrl(dataset, versionId, result.isPublishedState(), ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
}

public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, boolean autoselect, int size) {
public String getDatasetCardImageAsUrl(Dataset dataset, Long versionId, boolean autoselect, int size) {
Long datasetId = dataset.getId();
if (datasetId != null) {
if (this.dvobjectThumbnailsMap.containsKey(datasetId)) {
Expand Down Expand Up @@ -227,10 +177,9 @@ public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, bo
}

String url = SystemConfig.getDataverseSiteUrlStatic() + "/api/datasets/" + dataset.getId() + "/logo";
logger.fine("getDatasetCardImageAsBase64Url: " + url);
logger.fine("getDatasetCardImageAsUrl: " + url);
this.dvobjectThumbnailsMap.put(datasetId,url);
return url;

}

// it's the responsibility of the user - to make sure the search result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ private static boolean isThumbnailAvailable(StorageIO<DataFile> storageIO, int s
logger.fine("Found cached thumbnail for " + file.getId());
return true;
}
return generateThumbnail(file, storageIO, size);

}

private static boolean generateThumbnail(DataFile file, StorageIO<DataFile> storageIO, int size) {
logger.log(Level.FINE, (file.isPreviewsHaveFailed() ? "Not trying" : "Trying") + " to generate thumbnail, file id: " + file.getId());
// Don't try to generate if there have been failures:
if (!file.isPreviewsHaveFailed()) {
Expand Down Expand Up @@ -446,27 +450,13 @@ public static String getImageThumbnailAsBase64(DataFile file, int size) {
logger.fine("Null channel for aux object " + THUMBNAIL_SUFFIX + size);

// try to generate, if not available and hasn't failed before
logger.log(Level.FINE, (file.isPreviewsHaveFailed() ? "Not trying" : "Trying") + "to generate base64 thumbnail, file id: " + file.getId());
if (!file.isPreviewsHaveFailed()) {
boolean generated = false;
if (file.getContentType().substring(0, 6).equalsIgnoreCase("image/")) {
generated = generateImageThumbnail(storageIO, size);
} else if (file.getContentType().equalsIgnoreCase("application/pdf")) {
generated = generatePDFThumbnail(storageIO, size);
}

if (!generated) {
// Record failure
logger.fine("Failed to generate base64 thumbnail for file id: " + file.getId());
} else {
// Success - try to open again:
if(generateThumbnail(file, storageIO, size)) {
try {
cachedThumbnailChannel = storageIO.openAuxChannel(THUMBNAIL_SUFFIX + size);
} catch (Exception ioEx) {
cachedThumbnailChannel = null;
}
}
}

// if still null - give up:
if (cachedThumbnailChannel == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ public void setDisplayCardValues() {
result.setImageUrl(thumbnailServiceWrapper.getDataverseCardImageAsBase64Url(result));
} else if (result.getType().equals("datasets")) {
if (result.getEntity() != null) {
result.setImageUrl(thumbnailServiceWrapper.getDatasetCardImageAsBase64Url(result));
result.setImageUrl(thumbnailServiceWrapper.getDatasetCardImageAsUrl(result));
}

if (result.isHarvested()) {
Expand Down

0 comments on commit 889cbd2

Please sign in to comment.