use of edu.harvard.iq.dataverse.dataset.DatasetThumbnail in project dataverse by IQSS.
the class Admin method getDatasetThumbnailMetadata.
/**
* This method is used in API tests, called from UtilIt.java.
*/
@GET
@Path("datasets/thumbnailMetadata/{id}")
public Response getDatasetThumbnailMetadata(@PathParam("id") Long idSupplied) {
Dataset dataset = datasetSvc.find(idSupplied);
if (dataset == null) {
return error(Response.Status.NOT_FOUND, "Could not find dataset based on id supplied: " + idSupplied + ".");
}
JsonObjectBuilder data = Json.createObjectBuilder();
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
data.add("isUseGenericThumbnail", dataset.isUseGenericThumbnail());
data.add("datasetLogoPresent", DatasetUtil.isDatasetLogoPresent(dataset));
if (datasetThumbnail != null) {
data.add("datasetThumbnailBase64image", datasetThumbnail.getBase64image());
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
/**
* @todo Change this from a String to a long.
*/
data.add("dataFileId", dataFile.getId().toString());
}
}
return ok(data);
}
use of edu.harvard.iq.dataverse.dataset.DatasetThumbnail in project dataverse by IQSS.
the class FileUtil method getThumbnail.
/*
* The method below appears to be unnecessary;
* it duplicates the method generateImageThumbnailFromFileAsBase64() from ImageThumbConverter;
* plus it creates an unnecessary temp file copy of the source file.
public static String rescaleImage(File file) throws IOException {
if (file == null) {
logger.info("file was null!!");
return null;
}
File tmpFile = File.createTempFile("tempFileToRescale", ".tmp");
BufferedImage fullSizeImage = ImageIO.read(file);
if (fullSizeImage == null) {
logger.info("fullSizeImage was null!");
return null;
}
int width = fullSizeImage.getWidth();
int height = fullSizeImage.getHeight();
FileChannel src = new FileInputStream(file).getChannel();
FileChannel dest = new FileOutputStream(tmpFile).getChannel();
dest.transferFrom(src, 0, src.size());
String pathToResizedFile = ImageThumbConverter.rescaleImage(fullSizeImage, width, height, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE, tmpFile.getAbsolutePath());
File resizedFile = new File(pathToResizedFile);
return ImageThumbConverter.getImageAsBase64FromFile(resizedFile);
}
*/
public static DatasetThumbnail getThumbnail(DataFile file) {
String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(file, ImageThumbConverter.DEFAULT_THUMBNAIL_SIZE);
DatasetThumbnail defaultDatasetThumbnail = new DatasetThumbnail(imageSourceBase64, file);
return defaultDatasetThumbnail;
}
use of edu.harvard.iq.dataverse.dataset.DatasetThumbnail in project dataverse by IQSS.
the class EditDatafilesPage method deleteDatasetLogoAndUseThisDataFileAsThumbnailInstead.
public void deleteDatasetLogoAndUseThisDataFileAsThumbnailInstead() {
logger.log(Level.FINE, "For dataset id {0} the current thumbnail is from a dataset logo rather than a dataset file, blowing away the logo and using this FileMetadata id instead: {1}", new Object[] { dataset.getId(), fileMetadataSelectedForThumbnailPopup });
/**
* @todo Rather than deleting and merging right away, try to respect how
* this page seems to stage actions and giving the user a chance to
* review before clicking "Save Changes".
*/
try {
DatasetThumbnail datasetThumbnail = commandEngine.submit(new UpdateDatasetThumbnailCommand(dvRequestService.getDataverseRequest(), dataset, UpdateDatasetThumbnailCommand.UserIntent.setDatasetFileAsThumbnail, fileMetadataSelectedForThumbnailPopup.getDataFile().getId(), null));
// look up the dataset again because the UpdateDatasetThumbnailCommand mutates (merges) the dataset
dataset = datasetService.find(dataset.getId());
} catch (CommandException ex) {
String error = "Problem setting thumbnail for dataset id " + dataset.getId() + ".: " + ex;
// show this error to the user?
logger.info(error);
}
}
use of edu.harvard.iq.dataverse.dataset.DatasetThumbnail in project dataverse by IQSS.
the class UpdateDatasetThumbnailCommand method execute.
@Override
public DatasetThumbnail execute(CommandContext ctxt) throws CommandException {
if (dataset == null) {
String message = "Can't update dataset thumbnail. Dataset is null.";
logger.info(message);
throw new IllegalCommandException(message, this);
}
// }
if (userIntent == null) {
throw new IllegalCommandException("No changes to save.", this);
}
switch(userIntent) {
case setDatasetFileAsThumbnail:
if (dataFileIdSupplied == null) {
throw new CommandException("A file was not selected to be the new dataset thumbnail.", this);
}
DataFile datasetFileThumbnailToSwitchTo = ctxt.files().find(dataFileIdSupplied);
if (datasetFileThumbnailToSwitchTo == null) {
throw new CommandException("Could not find file based on id supplied: " + dataFileIdSupplied + ".", this);
}
Dataset ds1 = ctxt.datasets().setDatasetFileAsThumbnail(dataset, datasetFileThumbnailToSwitchTo);
DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail();
if (datasetThumbnail != null) {
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
if (dataFile.getId().equals(dataFileIdSupplied)) {
return datasetThumbnail;
} else {
throw new CommandException("Dataset thumbnail is should be based on file id " + dataFile.getId() + " but instead it is " + dataFileIdSupplied + ".", this);
}
}
} else {
throw new CommandException("Dataset thumbnail is unexpectedly absent.", this);
}
case setNonDatasetFileAsThumbnail:
File uploadedFile;
try {
uploadedFile = FileUtil.inputStreamToFile(inputStream);
} catch (IOException ex) {
throw new CommandException("In setNonDatasetFileAsThumbnail caught exception calling inputStreamToFile: " + ex, this);
}
if (uploadedFile == null) {
throw new CommandException("In setNonDatasetFileAsThumbnail uploadedFile was null.", this);
}
long uploadLogoSizeLimit = ctxt.systemConfig().getUploadLogoSizeLimit();
if (uploadedFile.length() > uploadLogoSizeLimit) {
throw new IllegalCommandException("File is larger than maximum size: " + uploadLogoSizeLimit + ".", this);
}
FileInputStream fileAsStream = null;
try {
fileAsStream = new FileInputStream(uploadedFile);
} catch (FileNotFoundException ex) {
Logger.getLogger(UpdateDatasetThumbnailCommand.class.getName()).log(Level.SEVERE, null, ex);
}
Dataset datasetWithNewThumbnail = ctxt.datasets().setNonDatasetFileAsThumbnail(dataset, fileAsStream);
if (datasetWithNewThumbnail != null) {
return datasetWithNewThumbnail.getDatasetThumbnail();
} else {
return null;
}
case removeThumbnail:
Dataset ds2 = ctxt.datasets().removeDatasetThumbnail(dataset);
DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail();
if (datasetThumbnail2 == null) {
return null;
} else {
throw new CommandException("User wanted to remove the thumbnail it still has one!", this);
}
default:
throw new IllegalCommandException("Whatever you are trying to do to the dataset thumbnail is not supported.", this);
}
}
use of edu.harvard.iq.dataverse.dataset.DatasetThumbnail in project dataverse by IQSS.
the class Datasets method getDatasetThumbnailCandidates.
@GET
@Path("{id}/thumbnail/candidates")
public Response getDatasetThumbnailCandidates(@PathParam("id") String idSupplied) {
try {
Dataset dataset = findDatasetOrDie(idSupplied);
boolean canUpdateThumbnail = false;
try {
canUpdateThumbnail = permissionSvc.requestOn(createDataverseRequest(findUserOrDie()), dataset).canIssue(UpdateDatasetThumbnailCommand.class);
} catch (WrappedResponse ex) {
logger.info("Exception thrown while trying to figure out permissions while getting thumbnail for dataset id " + dataset.getId() + ": " + ex.getLocalizedMessage());
}
if (!canUpdateThumbnail) {
return error(Response.Status.FORBIDDEN, "You are not permitted to list dataset thumbnail candidates.");
}
JsonArrayBuilder data = Json.createArrayBuilder();
boolean considerDatasetLogoAsCandidate = true;
for (DatasetThumbnail datasetThumbnail : DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate)) {
JsonObjectBuilder candidate = Json.createObjectBuilder();
String base64image = datasetThumbnail.getBase64image();
if (base64image != null) {
logger.fine("found a candidate!");
candidate.add("base64image", base64image);
}
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
candidate.add("dataFileId", dataFile.getId());
}
data.add(candidate);
}
return ok(data);
} catch (WrappedResponse ex) {
return error(Response.Status.NOT_FOUND, "Could not find dataset based on id supplied: " + idSupplied + ".");
}
}
Aggregations