From 4254a0fb01c324980fa7a1da57ad20f01fff9e4a Mon Sep 17 00:00:00 2001 From: Leonid Andreev Date: Fri, 17 Apr 2020 15:21:21 -0400 Subject: [PATCH] Making the size of the dataset thumbnail flexible. That's seemingly a lot of changes, but it was the assumption throughout the code base that all the dataset-level thumbnails are always the same size. Removing it now will help with making all the thumbnails for everything completely variable (as opposed to how they come in the few pre-defined sizes now), which we are planning to add as well, sometime soon (?) ref #6684 --- .../edu/harvard/iq/dataverse/Dataset.java | 8 +-- .../edu/harvard/iq/dataverse/DatasetPage.java | 7 ++- .../iq/dataverse/DatasetServiceBean.java | 2 +- .../iq/dataverse/DatasetWidgetsPage.java | 8 +-- .../iq/dataverse/EditDatafilesPage.java | 2 +- .../iq/dataverse/ThumbnailServiceWrapper.java | 19 ++++--- .../edu/harvard/iq/dataverse/api/Admin.java | 5 +- .../harvard/iq/dataverse/api/Datasets.java | 5 +- .../dataaccess/ImageThumbConverter.java | 2 +- .../iq/dataverse/dataset/DatasetUtil.java | 57 ++++++++++++------- .../impl/UpdateDatasetThumbnailCommand.java | 7 ++- .../harvard/iq/dataverse/util/FileUtil.java | 1 + .../harvard/iq/dataverse/api/SearchIT.java | 7 ++- .../iq/dataverse/dataset/DatasetUtilTest.java | 20 +++---- 14 files changed, 89 insertions(+), 61 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/Dataset.java b/src/main/java/edu/harvard/iq/dataverse/Dataset.java index 5eb9cfa3a4e..b94ec714c60 100644 --- a/src/main/java/edu/harvard/iq/dataverse/Dataset.java +++ b/src/main/java/edu/harvard/iq/dataverse/Dataset.java @@ -836,8 +836,8 @@ public boolean isAncestorOf( DvObject other ) { return equals(other) || equals(other.getOwner()); } - public DatasetThumbnail getDatasetThumbnail() { - return DatasetUtil.getThumbnail(this); + public DatasetThumbnail getDatasetThumbnail(int size) { + return DatasetUtil.getThumbnail(this, size); } /** @@ -848,8 +848,8 @@ public DatasetThumbnail getDatasetThumbnail() { * @param datasetVersion * @return A thumbnail of the dataset (may be {@code null}). */ - public DatasetThumbnail getDatasetThumbnail(DatasetVersion datasetVersion) { - return DatasetUtil.getThumbnail(this, datasetVersion); + public DatasetThumbnail getDatasetThumbnail(DatasetVersion datasetVersion, int size) { + return DatasetUtil.getThumbnail(this, datasetVersion, size); } } diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java b/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java index 0ece7e9c4c2..652144703b9 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java @@ -376,7 +376,7 @@ public String getThumbnailString() { } if (!readOnly) { - DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(); + DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (datasetThumbnail == null) { thumbnailString = ""; return null; @@ -392,7 +392,10 @@ public String getThumbnailString() { thumbnailString = datasetThumbnail.getBase64image(); } else { - thumbnailString = thumbnailServiceWrapper.getDatasetCardImageAsBase64Url(dataset, workingVersion.getId(),!workingVersion.isDraft()); + thumbnailString = thumbnailServiceWrapper.getDatasetCardImageAsBase64Url(dataset, + workingVersion.getId(), + !workingVersion.isDraft(), + ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (thumbnailString == null) { thumbnailString = ""; return null; diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java index 9c0eee6fbf3..172214e95d0 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java @@ -721,7 +721,7 @@ public Dataset setNonDatasetFileAsThumbnail(Dataset dataset, InputStream inputSt logger.fine("In setNonDatasetFileAsThumbnail but inputStream is null! Returning null."); return null; } - dataset = DatasetUtil.persistDatasetLogoToStorageAndCreateThumbnail(dataset, inputStream); + dataset = DatasetUtil.persistDatasetLogoToStorageAndCreateThumbnails(dataset, inputStream); dataset.setThumbnailFile(null); return merge(dataset); } diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetWidgetsPage.java b/src/main/java/edu/harvard/iq/dataverse/DatasetWidgetsPage.java index 463bf2949a9..3c9b263253a 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetWidgetsPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetWidgetsPage.java @@ -67,8 +67,8 @@ public String init() { if (!permissionsWrapper.canIssueCommand(dataset, UpdateDatasetVersionCommand.class)) { return permissionsWrapper.notAuthorized(); } - datasetThumbnails = DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate); - datasetThumbnail = dataset.getDatasetThumbnail(); + datasetThumbnails = DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); + datasetThumbnail = dataset.getDatasetThumbnail(ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (datasetThumbnail != null) { DataFile dataFile = datasetThumbnail.getDataFile(); if (dataFile != null) { @@ -117,7 +117,7 @@ public void setDatasetFileThumbnailToSwitchTo(DataFile datasetFileThumbnailToSwi public void setDataFileAsThumbnail() { logger.fine("setDataFileAsThumbnail clicked"); updateDatasetThumbnailCommand = new UpdateDatasetThumbnailCommand(dvRequestService.getDataverseRequest(), dataset, UpdateDatasetThumbnailCommand.UserIntent.setDatasetFileAsThumbnail, datasetFileThumbnailToSwitchTo.getId(), null); - String base64image = ImageThumbConverter.getImageThumbnailAsBase64(datasetFileThumbnailToSwitchTo, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + String base64image = ImageThumbConverter.getImageThumbnailAsBase64(datasetFileThumbnailToSwitchTo, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); datasetThumbnail = new DatasetThumbnail(base64image, datasetFileThumbnailToSwitchTo); } @@ -146,7 +146,7 @@ public void handleImageFileUpload(FileUploadEvent event) { Logger.getLogger(DatasetWidgetsPage.class.getName()).log(Level.SEVERE, null, ex); return; } - String base64image = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(file, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + String base64image = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(file, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (base64image != null) { datasetThumbnail = new DatasetThumbnail(base64image, datasetFileThumbnailToSwitchTo); } else { diff --git a/src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java b/src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java index 8fe7651fe85..34577302e1c 100644 --- a/src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java @@ -2586,7 +2586,7 @@ public void deleteDatasetLogoAndUseThisDataFileAsThumbnailInstead() { } public boolean isThumbnailIsFromDatasetLogoRatherThanDatafile() { - DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(); + DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); return datasetThumbnail != null && !datasetThumbnail.isFromDataFile(); } diff --git a/src/main/java/edu/harvard/iq/dataverse/ThumbnailServiceWrapper.java b/src/main/java/edu/harvard/iq/dataverse/ThumbnailServiceWrapper.java index e11c8eb03e1..6c8db8c124b 100644 --- a/src/main/java/edu/harvard/iq/dataverse/ThumbnailServiceWrapper.java +++ b/src/main/java/edu/harvard/iq/dataverse/ThumbnailServiceWrapper.java @@ -10,7 +10,6 @@ import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter; import edu.harvard.iq.dataverse.dataset.DatasetUtil; import static edu.harvard.iq.dataverse.dataset.DatasetUtil.datasetLogoThumbnail; -import static edu.harvard.iq.dataverse.dataset.DatasetUtil.thumb48addedByImageThumbConverter; import edu.harvard.iq.dataverse.search.SolrSearchResult; import edu.harvard.iq.dataverse.util.FileUtil; import java.io.File; @@ -51,7 +50,7 @@ public class ThumbnailServiceWrapper implements java.io.Serializable { private Map dvobjectThumbnailsMap = new HashMap<>(); private Map dvobjectViewMap = new HashMap<>(); - private String getAssignedDatasetImage(Dataset dataset) { + private String getAssignedDatasetImage(Dataset dataset, int size) { if (dataset == null) { return null; } @@ -71,7 +70,8 @@ private String getAssignedDatasetImage(Dataset dataset) { } String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(assignedThumbnailFile, - ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + size); + //ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); if (imageSourceBase64 != null) { this.dvobjectThumbnailsMap.put(assignedThumbnailFileId, imageSourceBase64); @@ -182,9 +182,10 @@ public String getDatasetCardImageAsBase64Url(SolrSearchResult result) { Long versionId = result.getDatasetVersionId(); - return getDatasetCardImageAsBase64Url(dataset, versionId, result.isPublishedState()); + return getDatasetCardImageAsBase64Url(dataset, versionId, result.isPublishedState(), ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); } - public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, boolean autoselect) { + + public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, boolean autoselect, int size) { Long datasetId = dataset.getId(); if (datasetId != null) { if (this.dvobjectThumbnailsMap.containsKey(datasetId)) { @@ -221,7 +222,8 @@ public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, bo // an auxilary file on the dataset level: // (don't bother checking if it exists; just try to open the input stream) try { - in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumb48addedByImageThumbConverter); + in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + ".thumb" + size); + //thumb48addedByImageThumbConverter); } catch (Exception ioex) { //ignore } @@ -247,7 +249,7 @@ public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, bo // If not, see if the dataset has one of its image files already assigned // to be the designated thumbnail: - cardImageUrl = this.getAssignedDatasetImage(dataset); + cardImageUrl = this.getAssignedDatasetImage(dataset, size); if (cardImageUrl != null) { //logger.info("dataset id " + result.getEntity().getId() + " has a dedicated image assigned; returning " + cardImageUrl); @@ -296,7 +298,8 @@ public String getDatasetCardImageAsBase64Url(Dataset dataset, Long versionId, bo if (dataFileService.isThumbnailAvailable(thumbnailImageFile)) { cardImageUrl = ImageThumbConverter.getImageThumbnailAsBase64( thumbnailImageFile, - ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); + size); + //ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); } if (cardImageUrl != null) { diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Admin.java b/src/main/java/edu/harvard/iq/dataverse/api/Admin.java index 1474104d379..2d5f20d239d 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Admin.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Admin.java @@ -75,6 +75,7 @@ import edu.harvard.iq.dataverse.authorization.RoleAssignee; import edu.harvard.iq.dataverse.authorization.UserRecordIdentifier; import edu.harvard.iq.dataverse.authorization.users.User; +import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter; import edu.harvard.iq.dataverse.dataset.DatasetThumbnail; import edu.harvard.iq.dataverse.dataset.DatasetUtil; import edu.harvard.iq.dataverse.engine.command.DataverseRequest; @@ -1178,9 +1179,9 @@ public Response getDatasetThumbnailMetadata(@PathParam("id") Long idSupplied) { return error(Response.Status.NOT_FOUND, "Could not find dataset based on id supplied: " + idSupplied + "."); } JsonObjectBuilder data = Json.createObjectBuilder(); - DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(); + DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); data.add("isUseGenericThumbnail", dataset.isUseGenericThumbnail()); - data.add("datasetLogoPresent", DatasetUtil.isDatasetLogoPresent(dataset)); + data.add("datasetLogoPresent", DatasetUtil.isDatasetLogoPresent(dataset, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); if (datasetThumbnail != null) { data.add("datasetThumbnailBase64image", datasetThumbnail.getBase64image()); DataFile dataFile = datasetThumbnail.getDataFile(); diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java index 0b2e25a7f02..06a6854a98f 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java @@ -82,6 +82,7 @@ import edu.harvard.iq.dataverse.api.dto.RoleAssignmentDTO; import edu.harvard.iq.dataverse.batch.util.LoggingUtil; import edu.harvard.iq.dataverse.dataaccess.DataAccess; +import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter; import edu.harvard.iq.dataverse.dataaccess.S3AccessIO; import edu.harvard.iq.dataverse.engine.command.exception.CommandException; import edu.harvard.iq.dataverse.engine.command.exception.UnforcedCommandException; @@ -1213,7 +1214,7 @@ public Response getDatasetThumbnailCandidates(@PathParam("id") String idSupplied } JsonArrayBuilder data = Json.createArrayBuilder(); boolean considerDatasetLogoAsCandidate = true; - for (DatasetThumbnail datasetThumbnail : DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate)) { + for (DatasetThumbnail datasetThumbnail : DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)) { JsonObjectBuilder candidate = Json.createObjectBuilder(); String base64image = datasetThumbnail.getBase64image(); if (base64image != null) { @@ -1238,7 +1239,7 @@ public Response getDatasetThumbnailCandidates(@PathParam("id") String idSupplied public Response getDatasetThumbnail(@PathParam("id") String idSupplied) { try { Dataset dataset = findDatasetOrDie(idSupplied); - InputStream is = DatasetUtil.getThumbnailAsInputStream(dataset); + InputStream is = DatasetUtil.getThumbnailAsInputStream(dataset, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); if(is == null) { return notFound("Thumbnail not available"); } diff --git a/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java b/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java index 196c2659cc1..1249e95494e 100644 --- a/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java +++ b/src/main/java/edu/harvard/iq/dataverse/dataaccess/ImageThumbConverter.java @@ -63,7 +63,7 @@ public class ImageThumbConverter { public static int DEFAULT_CARDIMAGE_SIZE = 48; public static int DEFAULT_THUMBNAIL_SIZE = 64; - public static int DEFAULT_DATASET_THUMBNAIL_SIZE = 140; + public static int DEFAULT_DATASETLOGO_SIZE = 140; public static int DEFAULT_PREVIEW_SIZE = 400; private static final Logger logger = Logger.getLogger(ImageThumbConverter.class.getCanonicalName()); diff --git a/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java b/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java index 6b6cb1bbd5b..f6de6b0c3f5 100644 --- a/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java +++ b/src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java @@ -32,15 +32,17 @@ import javax.imageio.ImageIO; import org.apache.commons.io.IOUtils; import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO; +import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO; +import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO; public class DatasetUtil { private static final Logger logger = Logger.getLogger(DatasetUtil.class.getCanonicalName()); public static String datasetLogoFilenameFinal = "dataset_logo_original"; public static String datasetLogoThumbnail = "dataset_logo"; - public static String thumb48addedByImageThumbConverter = ".thumb48"; + public static String thumbExtension = ".thumb"; - public static List getThumbnailCandidates(Dataset dataset, boolean considerDatasetLogoAsCandidate) { + public static List getThumbnailCandidates(Dataset dataset, boolean considerDatasetLogoAsCandidate, int size) { List thumbnails = new ArrayList<>(); if (dataset == null) { return thumbnails; @@ -62,7 +64,7 @@ public static List getThumbnailCandidates(Dataset dataset, boo InputStream in = null; try { - in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumb48addedByImageThumbConverter); + in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumbExtension + size); } catch (Exception ioex) { } @@ -88,7 +90,7 @@ public static List getThumbnailCandidates(Dataset dataset, boo && ImageThumbConverter.isThumbnailAvailable(dataFile) && !dataFile.isRestricted()) { String imageSourceBase64 = null; - imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(dataFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(dataFile, size); if (imageSourceBase64 != null) { DatasetThumbnail datasetThumbnail = new DatasetThumbnail(imageSourceBase64, dataFile); @@ -107,7 +109,7 @@ public static List getThumbnailCandidates(Dataset dataset, boo * @param datasetVersion * @return */ - public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion datasetVersion) { + public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion datasetVersion, int size) { if (dataset == null) { return null; } @@ -125,10 +127,11 @@ public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion data try { if (dataAccess == null) { logger.warning("getThumbnail(): Failed to initialize dataset StorageIO for " + dataset.getStorageIdentifier()); - } else if (dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumb48addedByImageThumbConverter) != null) { - in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumb48addedByImageThumbConverter); + } else { + in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + ".thumb" + size); } } catch (IOException ex) { + in = null; logger.fine("Dataset-level thumbnail file does not exist, or failed to open; will try to find an image file that can be used as the thumbnail."); } @@ -160,7 +163,7 @@ public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion data logger.fine("Dataset (id :" + dataset.getId() + ") does not have a thumbnail available that could be selected automatically."); return null; } else { - String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, size); DatasetThumbnail defaultDatasetThumbnail = new DatasetThumbnail(imageSourceBase64, thumbnailFile); logger.fine("thumbnailFile (id :" + thumbnailFile.getId() + ") will get thumbnail through automatic selection from DataFile id " + thumbnailFile.getId()); return defaultDatasetThumbnail; @@ -170,7 +173,7 @@ public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion data logger.fine("Dataset (id :" + dataset.getId() + ") has a thumbnail the user selected but the file must have later been restricted. Returning null."); return null; } else { - String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, size); DatasetThumbnail userSpecifiedDatasetThumbnail = new DatasetThumbnail(imageSourceBase64, thumbnailFile); logger.fine("Dataset (id :" + dataset.getId() + ") will get thumbnail the user specified from DataFile id " + thumbnailFile.getId()); return userSpecifiedDatasetThumbnail; @@ -179,11 +182,11 @@ public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion data } } - public static DatasetThumbnail getThumbnail(Dataset dataset) { + public static DatasetThumbnail getThumbnail(Dataset dataset, int size) { if (dataset == null) { return null; } - return getThumbnail(dataset, null); + return getThumbnail(dataset, null, size); } public static boolean deleteDatasetLogo(Dataset dataset) { @@ -199,7 +202,8 @@ public static boolean deleteDatasetLogo(Dataset dataset) { } storageIO.deleteAuxObject(datasetLogoFilenameFinal); - storageIO.deleteAuxObject(datasetLogoThumbnail + thumb48addedByImageThumbConverter); + storageIO.deleteAuxObject(datasetLogoThumbnail + thumbExtension + ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); + storageIO.deleteAuxObject(datasetLogoThumbnail + thumbExtension + ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); } catch (IOException ex) { logger.info("Failed to delete dataset logo: " + ex.getMessage()); @@ -252,7 +256,7 @@ public static DataFile attemptToAutomaticallySelectThumbnailFromDataFiles(Datase for (FileMetadata fmd : datasetVersion.getFileMetadatas()) { DataFile testFile = fmd.getDataFile(); // We don't want to use a restricted image file as the dedicated thumbnail: - if (!testFile.isRestricted() && FileUtil.isThumbnailSupported(testFile) && ImageThumbConverter.isThumbnailAvailable(testFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE)) { + if (!testFile.isRestricted() && FileUtil.isThumbnailSupported(testFile) && ImageThumbConverter.isThumbnailAvailable(testFile, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE)) { return testFile; } } @@ -260,7 +264,7 @@ public static DataFile attemptToAutomaticallySelectThumbnailFromDataFiles(Datase return null; } - public static Dataset persistDatasetLogoToStorageAndCreateThumbnail(Dataset dataset, InputStream inputStream) { + public static Dataset persistDatasetLogoToStorageAndCreateThumbnails(Dataset dataset, InputStream inputStream) { if (dataset == null) { return null; } @@ -329,15 +333,28 @@ public static Dataset persistDatasetLogoToStorageAndCreateThumbnail(Dataset data logger.severe(ex.getMessage()); return null; } - String thumbFileLocation = ImageThumbConverter.rescaleImage(fullSizeImage, width, height, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE, tmpFileForResize.toPath().toString()); + // We'll try to pre-generate the rescaled versions in both the + // DEFAULT_DATASET_LOGO (currently 140) and DEFAULT_CARDIMAGE_SIZE (48) + String thumbFileLocation = ImageThumbConverter.rescaleImage(fullSizeImage, width, height, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE, tmpFileForResize.toPath().toString()); + logger.fine("thumbFileLocation = " + thumbFileLocation); + logger.fine("tmpFileLocation=" + tmpFileForResize.toPath().toString()); + //now we must save the updated thumbnail + try { + dataAccess.savePathAsAux(Paths.get(thumbFileLocation), datasetLogoThumbnail+thumbExtension+ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); + } catch (IOException ex) { + logger.severe("Failed to move updated thumbnail file from " + tmpFile.getAbsolutePath() + " to its DataAccess location" + ": " + ex); + } + + thumbFileLocation = ImageThumbConverter.rescaleImage(fullSizeImage, width, height, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE, tmpFileForResize.toPath().toString()); logger.fine("thumbFileLocation = " + thumbFileLocation); logger.fine("tmpFileLocation=" + tmpFileForResize.toPath().toString()); //now we must save the updated thumbnail try { - dataAccess.savePathAsAux(Paths.get(thumbFileLocation), datasetLogoThumbnail+thumb48addedByImageThumbConverter); + dataAccess.savePathAsAux(Paths.get(thumbFileLocation), datasetLogoThumbnail+thumbExtension+ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); } catch (IOException ex) { logger.severe("Failed to move updated thumbnail file from " + tmpFile.getAbsolutePath() + " to its DataAccess location" + ": " + ex); } + //This deletes the tempfiles created for rescaling and encoding boolean tmpFileWasDeleted = tmpFile.delete(); boolean originalTempFileWasDeleted = tmpFileForResize.delete(); @@ -351,11 +368,11 @@ public static Dataset persistDatasetLogoToStorageAndCreateThumbnail(Dataset data return dataset; } - public static InputStream getThumbnailAsInputStream(Dataset dataset) { + public static InputStream getThumbnailAsInputStream(Dataset dataset, int size) { if (dataset == null) { return null; } - DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(); + DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(size); if (datasetThumbnail == null) { return null; } else { @@ -382,7 +399,7 @@ public static InputStream getThumbnailAsInputStream(Dataset dataset) { * file that is uploaded. Rather, we delete it after first creating at least * one thumbnail from it. */ - public static boolean isDatasetLogoPresent(Dataset dataset) { + public static boolean isDatasetLogoPresent(Dataset dataset, int size) { if (dataset == null) { return false; } @@ -391,7 +408,7 @@ public static boolean isDatasetLogoPresent(Dataset dataset) { try { dataAccess = DataAccess.getStorageIO(dataset); - return dataAccess.isAuxObjectCached(datasetLogoThumbnail + thumb48addedByImageThumbConverter); + return dataAccess.isAuxObjectCached(datasetLogoThumbnail + thumbExtension + size); } catch (IOException ioex) { } return false; diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetThumbnailCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetThumbnailCommand.java index ccb276a8b98..3f4b3c36b70 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetThumbnailCommand.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetThumbnailCommand.java @@ -3,6 +3,7 @@ import edu.harvard.iq.dataverse.DataFile; import edu.harvard.iq.dataverse.Dataset; import edu.harvard.iq.dataverse.authorization.Permission; +import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter; import edu.harvard.iq.dataverse.dataset.DatasetThumbnail; import edu.harvard.iq.dataverse.engine.command.AbstractCommand; import edu.harvard.iq.dataverse.engine.command.CommandContext; @@ -71,7 +72,7 @@ public DatasetThumbnail execute(CommandContext ctxt) throws CommandException { throw new CommandException("Could not find file based on id supplied: " + dataFileIdSupplied + ".", this); } Dataset ds1 = ctxt.datasets().setDatasetFileAsThumbnail(dataset, datasetFileThumbnailToSwitchTo); - DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail(); + DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); if (datasetThumbnail != null) { DataFile dataFile = datasetThumbnail.getDataFile(); if (dataFile != null) { @@ -108,14 +109,14 @@ public DatasetThumbnail execute(CommandContext ctxt) throws CommandException { Dataset datasetWithNewThumbnail = ctxt.datasets().setNonDatasetFileAsThumbnail(dataset, fileAsStream); IOUtils.closeQuietly(fileAsStream); if (datasetWithNewThumbnail != null) { - return datasetWithNewThumbnail.getDatasetThumbnail(); + return datasetWithNewThumbnail.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); } else { return null; } case removeThumbnail: Dataset ds2 = ctxt.datasets().removeDatasetThumbnail(dataset); - DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail(); + DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); if (datasetThumbnail2 == null) { return null; } else { diff --git a/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java b/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java index a4370c7b38f..01a337272b6 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java @@ -82,6 +82,7 @@ import org.apache.commons.io.FilenameUtils; import com.amazonaws.AmazonServiceException; +import static edu.harvard.iq.dataverse.datasetutility.FileSizeChecker.bytesToHumanReadable; /** * a 4.0 implementation of the DVN FileUtil; diff --git a/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java b/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java index ac541cf9488..b03e8df3e62 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java @@ -35,6 +35,7 @@ import org.hamcrest.Matchers; import org.junit.After; import static org.junit.Assert.assertNotEquals; +import static java.lang.Thread.sleep; public class SearchIT { @@ -334,7 +335,7 @@ public void testDatasetThumbnail() { File trees = new File("scripts/search/data/binary/trees.png"); String treesAsBase64 = null; - treesAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(trees, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + treesAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(trees, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (treesAsBase64 == null) { Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, "Failed to generate a base64 thumbnail from the file trees.png"); @@ -495,7 +496,7 @@ public void testDatasetThumbnail() { File dataverseProjectLogo = new File(pathToFile); String dataverseProjectLogoAsBase64 = null; - dataverseProjectLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(dataverseProjectLogo, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + dataverseProjectLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(dataverseProjectLogo, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (dataverseProjectLogoAsBase64 == null) { Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, "Failed to generate a base64 thumbnail from the file dataverseproject.png"); @@ -556,7 +557,7 @@ public void testDatasetThumbnail() { String datasetLogo = "src/main/webapp/resources/images/cc0.png"; File datasetLogoFile = new File(datasetLogo); - String datasetLogoAsBase64 = datasetLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(datasetLogoFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE); + String datasetLogoAsBase64 = datasetLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(datasetLogoFile, ImageThumbConverter.DEFAULT_DATASETLOGO_SIZE); if (datasetLogoAsBase64 == null) { Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, "Failed to generate a base64 thumbnail from the file dataverseproject.png"); diff --git a/src/test/java/edu/harvard/iq/dataverse/dataset/DatasetUtilTest.java b/src/test/java/edu/harvard/iq/dataverse/dataset/DatasetUtilTest.java index bb19a32c4f8..bc63c94153f 100644 --- a/src/test/java/edu/harvard/iq/dataverse/dataset/DatasetUtilTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/dataset/DatasetUtilTest.java @@ -23,7 +23,7 @@ public class DatasetUtilTest { */ @Test public void testGetThumbnailCandidates() { - assertEquals(new ArrayList<>(), DatasetUtil.getThumbnailCandidates(null, false)); + assertEquals(new ArrayList<>(), DatasetUtil.getThumbnailCandidates(null, false, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); Dataset dataset = MocksFactory.makeDataset(); DataFile dataFile = MocksFactory.makeDataFile(); @@ -37,21 +37,21 @@ public void testGetThumbnailCandidates() { List fmds = new ArrayList<>(); fmds.add(MocksFactory.addFileMetadata(dataFile)); version.setFileMetadatas(fmds); - assertEquals(new ArrayList<>(), DatasetUtil.getThumbnailCandidates(dataset, false)); + assertEquals(new ArrayList<>(), DatasetUtil.getThumbnailCandidates(dataset, false, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); } @Test public void testGetThumbnailNullDataset() { - assertNull(DatasetUtil.getThumbnail(null)); - assertNull(DatasetUtil.getThumbnail(null, null)); + assertNull(DatasetUtil.getThumbnail(null, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); + assertNull(DatasetUtil.getThumbnail(null, null, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); Dataset dataset = MocksFactory.makeDataset(); System.setProperty("dataverse.files.testfile.type", "file"); dataset.setStorageIdentifier("testfile://"); dataset.setUseGenericThumbnail(true); - assertNull(DatasetUtil.getThumbnail(dataset)); - assertNull(DatasetUtil.getThumbnail(dataset, new DatasetVersion())); + assertNull(DatasetUtil.getThumbnail(dataset, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); + assertNull(DatasetUtil.getThumbnail(dataset, new DatasetVersion(), ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); } @Test @@ -62,7 +62,7 @@ public void testGetThumbnailRestricted() { thumbnailFile.setId(42l); thumbnailFile.setRestricted(true); dataset.setThumbnailFile(thumbnailFile); - DatasetThumbnail result = DatasetUtil.getThumbnail(dataset); + DatasetThumbnail result = DatasetUtil.getThumbnail(dataset, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE); assertNull(result); } /** @@ -88,7 +88,7 @@ public void testGetDefaultThumbnailFile() { */ @Test public void testPersistDatasetLogoToStorageAndCreateThumbnail() { - assertNull(DatasetUtil.persistDatasetLogoToStorageAndCreateThumbnail(null, null)); + assertNull(DatasetUtil.persistDatasetLogoToStorageAndCreateThumbnails(null, null)); //Todo: a test for this that test main logic } @@ -97,7 +97,7 @@ public void testPersistDatasetLogoToStorageAndCreateThumbnail() { */ @Test public void testGetThumbnailAsInputStream() { - assertNull(DatasetUtil.getThumbnailAsInputStream(null)); + assertNull(DatasetUtil.getThumbnailAsInputStream(null, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); } /** @@ -106,7 +106,7 @@ public void testGetThumbnailAsInputStream() { @Test public void testIsDatasetLogoPresent() { Dataset dataset = MocksFactory.makeDataset(); - assertEquals(false, DatasetUtil.isDatasetLogoPresent(dataset)); + assertEquals(false, DatasetUtil.isDatasetLogoPresent(dataset, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE)); } @Test