Skip to content

Commit

Permalink
Merge pull request #6830 from IQSS/6684-thumbnails
Browse files Browse the repository at this point in the history
6684 thumbnails
  • Loading branch information
mheppler authored Apr 16, 2020
2 parents b268b75 + 17ae5e1 commit 76449b9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ scripts/installer/default.config
tests/node_modules
tests/package-lock.json
venv

# from thumbnail tests in SearchIT
scripts/search/data/binary/trees.png.thumb140
src/main/webapp/resources/images/cc0.png.thumb140
src/main/webapp/resources/images/dataverseproject.png.thumb140
4 changes: 2 additions & 2 deletions doc/sphinx-guides/source/api/dataaccess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ the following parameter values are supported (for image and pdf files only):
============== ===========
Value Description
============== ===========
true Generates a thumbnail image, by rescaling to the default thumbnail size (64 pixels)
``N`` Rescales the image to ``N`` pixels.
true Generates a thumbnail image by rescaling to the default thumbnail size (64 pixels wide).
``N`` Rescales the image to ``N`` pixels wide. ``imageThumb=true`` and ``imageThumb=64`` are equivalent.
============== ===========

Multiple File ("bundle") download
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_CARDIMAGE_SIZE);
String base64image = ImageThumbConverter.getImageThumbnailAsBase64(datasetFileThumbnailToSwitchTo, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE);
datasetThumbnail = new DatasetThumbnail(base64image, datasetFileThumbnailToSwitchTo);
}

Expand Down Expand Up @@ -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_CARDIMAGE_SIZE);
String base64image = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(file, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE);
if (base64image != null) {
datasetThumbnail = new DatasetThumbnail(base64image, datasetFileThumbnailToSwitchTo);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ private String getAssignedDatasetImage(Dataset dataset) {
return null;
}

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

if (imageSourceBase64 != null) {
this.dvobjectThumbnailsMap.put(assignedThumbnailFileId, imageSourceBase64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +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_PREVIEW_SIZE = 400;

private static final Logger logger = Logger.getLogger(ImageThumbConverter.class.getCanonicalName());
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static List<DatasetThumbnail> getThumbnailCandidates(Dataset dataset, boo
&& ImageThumbConverter.isThumbnailAvailable(dataFile)
&& !dataFile.isRestricted()) {
String imageSourceBase64 = null;
imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(dataFile, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(dataFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE);

if (imageSourceBase64 != null) {
DatasetThumbnail datasetThumbnail = new DatasetThumbnail(imageSourceBase64, dataFile);
Expand Down Expand Up @@ -160,7 +160,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_CARDIMAGE_SIZE);
String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_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;
Expand All @@ -170,7 +170,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_CARDIMAGE_SIZE);
String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_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;
Expand Down Expand Up @@ -252,7 +252,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_CARDIMAGE_SIZE)) {
if (!testFile.isRestricted() && FileUtil.isThumbnailSupported(testFile) && ImageThumbConverter.isThumbnailAvailable(testFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE)) {
return testFile;
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ public static Dataset persistDatasetLogoToStorageAndCreateThumbnail(Dataset data
logger.severe(ex.getMessage());
return null;
}
String thumbFileLocation = ImageThumbConverter.rescaleImage(fullSizeImage, width, height, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE, tmpFileForResize.toPath().toString());
String thumbFileLocation = ImageThumbConverter.rescaleImage(fullSizeImage, width, height, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE, tmpFileForResize.toPath().toString());
logger.fine("thumbFileLocation = " + thumbFileLocation);
logger.fine("tmpFileLocation=" + tmpFileForResize.toPath().toString());
//now we must save the updated thumbnail
Expand Down
27 changes: 24 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter;
import java.awt.image.BufferedImage;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
import static java.lang.Thread.sleep;
import javax.imageio.ImageIO;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.OK;
Expand Down Expand Up @@ -331,7 +334,7 @@ public void testDatasetThumbnail() {

File trees = new File("scripts/search/data/binary/trees.png");
String treesAsBase64 = null;
treesAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(trees, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
treesAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(trees, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE);

if (treesAsBase64 == null) {
Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, "Failed to generate a base64 thumbnail from the file trees.png");
Expand Down Expand Up @@ -429,6 +432,24 @@ public void testDatasetThumbnail() {
.contentType("image/png")
.statusCode(OK.getStatusCode());

String trueOrWidthInPixels = "true";
Response getFileThumbnailImageA = UtilIT.getFileThumbnail(dataFileId1.toString(), trueOrWidthInPixels, apiToken);
getFileThumbnailImageA.then().assertThat()
.contentType("image/png")
.statusCode(OK.getStatusCode());

try {
BufferedImage bufferedImage = ImageIO.read(getFileThumbnailImageA.body().asInputStream());
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
System.out.println("width: " + width);
System.out.println("height: " + height);
int expectedWidth = 64;
assertEquals(expectedWidth, width);
} catch (IOException ex) {
Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, null, ex);
}

InputStream inputStream2creator = UtilIT.getInputStreamFromUnirest(thumbnailUrl, apiToken);
assertEquals(treesAsBase64, UtilIT.inputStreamToDataUrlSchemeBase64Png(inputStream2creator));

Expand Down Expand Up @@ -474,7 +495,7 @@ public void testDatasetThumbnail() {

File dataverseProjectLogo = new File(pathToFile);
String dataverseProjectLogoAsBase64 = null;
dataverseProjectLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(dataverseProjectLogo, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
dataverseProjectLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(dataverseProjectLogo, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE);

if (dataverseProjectLogoAsBase64 == null) {
Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, "Failed to generate a base64 thumbnail from the file dataverseproject.png");
Expand Down Expand Up @@ -535,7 +556,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_CARDIMAGE_SIZE);
String datasetLogoAsBase64 = datasetLogoAsBase64 = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(datasetLogoFile, ImageThumbConverter.DEFAULT_DATASET_THUMBNAIL_SIZE);

if (datasetLogoAsBase64 == null) {
Logger.getLogger(SearchIT.class.getName()).log(Level.SEVERE, "Failed to generate a base64 thumbnail from the file dataverseproject.png");
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,15 @@ static Response getDatasetThumbnailMetadata(Integer datasetId, String apiToken)
.get("/api/admin/datasets/thumbnailMetadata/" + datasetId);
}

/**
* @param trueOrWidthInPixels Passing "true" will result in the default width in pixels (64).
*/
static Response getFileThumbnail(String fileDatabaseId, String trueOrWidthInPixels, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/access/datafile/" + fileDatabaseId + "?imageThumb=" + trueOrWidthInPixels);
}

static Response useThumbnailFromDataFile(String datasetPersistentId, long dataFileId1, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
Expand Down

0 comments on commit 76449b9

Please sign in to comment.