Skip to content

Commit

Permalink
Added: returning imageUrl with new associated endpoint for retrieving…
Browse files Browse the repository at this point in the history
… the featured item image from the client
  • Loading branch information
GPortas committed Dec 27, 2024
1 parent 11a0592 commit ec3b6f2
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.util.SystemConfig;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Min;
Expand Down Expand Up @@ -68,4 +69,8 @@ public String getImageFileName() {
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String getImageFileUrl() {
return SystemConfig.getDataverseSiteUrlStatic() + "/api/access/dataverseFeatureItemImage/" + id;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.settings.JvmSettings;
import jakarta.ejb.Stateless;
import jakarta.inject.Named;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;

@Stateless
@Named
Expand All @@ -14,6 +19,10 @@ public class DataverseFeaturedItemServiceBean implements Serializable {
@PersistenceContext(unitName = "VDCNet-ejbPU")
private EntityManager em;

public DataverseFeaturedItem findById(Long id) {
return em.find(DataverseFeaturedItem.class, id);
}

public DataverseFeaturedItem save(DataverseFeaturedItem dataverseFeaturedItem) {
if (dataverseFeaturedItem.getId() == null) {
em.persist(dataverseFeaturedItem);
Expand All @@ -22,4 +31,12 @@ public DataverseFeaturedItem save(DataverseFeaturedItem dataverseFeaturedItem) {
return em.merge(dataverseFeaturedItem);
}
}

public InputStream getImageFileAsInputStream(DataverseFeaturedItem dataverseFeaturedItem) throws IOException {
Path imagePath = Path.of(JvmSettings.DOCROOT_DIRECTORY.lookup(),
JvmSettings.FEATURED_ITEMS_IMAGE_UPLOADS_DIRECTORY.lookup(),
dataverseFeaturedItem.getDataverse().getId().toString(),
dataverseFeaturedItem.getImageFileName());
return Files.newInputStream(imagePath);
}
}
60 changes: 23 additions & 37 deletions src/main/java/edu/harvard/iq/dataverse/api/Access.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,7 @@

package edu.harvard.iq.dataverse.api;

import edu.harvard.iq.dataverse.AuxiliaryFile;
import edu.harvard.iq.dataverse.AuxiliaryFileServiceBean;
import edu.harvard.iq.dataverse.DataCitation;
import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.FileAccessRequest;
import edu.harvard.iq.dataverse.FileMetadata;
import edu.harvard.iq.dataverse.DataFileServiceBean;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.DatasetVersion;
import edu.harvard.iq.dataverse.DatasetVersionServiceBean;
import edu.harvard.iq.dataverse.DatasetServiceBean;
import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.DataverseRequestServiceBean;
import edu.harvard.iq.dataverse.DataverseRoleServiceBean;
import edu.harvard.iq.dataverse.DataverseServiceBean;
import edu.harvard.iq.dataverse.DataverseSession;
import edu.harvard.iq.dataverse.DataverseTheme;
import edu.harvard.iq.dataverse.FileDownloadServiceBean;
import edu.harvard.iq.dataverse.GuestbookResponse;
import edu.harvard.iq.dataverse.GuestbookResponseServiceBean;
import edu.harvard.iq.dataverse.PermissionServiceBean;
import edu.harvard.iq.dataverse.PermissionsWrapper;
import edu.harvard.iq.dataverse.RoleAssignment;
import edu.harvard.iq.dataverse.UserNotification;
import edu.harvard.iq.dataverse.UserNotificationServiceBean;
import edu.harvard.iq.dataverse.ThemeWidgetFragment;
import edu.harvard.iq.dataverse.*;

import static edu.harvard.iq.dataverse.api.Datasets.handleVersion;

Expand All @@ -55,15 +30,7 @@
import edu.harvard.iq.dataverse.engine.command.Command;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.impl.AssignRoleCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetDatasetCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetDraftDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetLatestAccessibleDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetLatestPublishedDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetSpecificPublishedDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.RequestAccessCommand;
import edu.harvard.iq.dataverse.engine.command.impl.RevokeRoleCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.*;
import edu.harvard.iq.dataverse.export.DDIExportServiceBean;
import edu.harvard.iq.dataverse.makedatacount.MakeDataCountLoggingServiceBean;
import edu.harvard.iq.dataverse.makedatacount.MakeDataCountLoggingServiceBean.MakeDataCountEntry;
Expand All @@ -88,7 +55,6 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import jakarta.inject.Inject;
import jakarta.json.Json;
Expand Down Expand Up @@ -133,7 +99,6 @@

import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
Expand Down Expand Up @@ -199,6 +164,8 @@ public class Access extends AbstractApiBean {
PermissionsWrapper permissionsWrapper;
@Inject
MakeDataCountLoggingServiceBean mdcLogService;
@Inject
DataverseFeaturedItemServiceBean dataverseFeaturedItemServiceBean;

//@EJB

Expand Down Expand Up @@ -2015,4 +1982,23 @@ private URI handleCustomZipDownload(User user, String customZipServiceUrl, Strin
}
return redirectUri;
}

@Path("dataverseFeatureItemImage/{itemId}")
@GET
@Produces({"image/png"})
public InputStream getDataverseFeatureItemImage(@Context ContainerRequestContext crc, @PathParam("itemId") Long itemId) {
DataverseFeaturedItem dataverseFeaturedItem;
try {
dataverseFeaturedItem = execCommand(new GetDataverseFeaturedItemCommand(createDataverseRequest(getRequestUser(crc)), dataverseFeaturedItemServiceBean.findById(itemId)));
} catch (WrappedResponse wr) {
logger.warning("Cannot locate a dataverse featured item with id " + itemId);
return null;
}
try {
return dataverseFeaturedItemServiceBean.getImageFileAsInputStream(dataverseFeaturedItem);
} catch (IOException e) {
logger.warning("Error while obtaining the input stream for the image file associated with the dataverse featured item with id " + itemId);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ private void setImageIfAvailable(DataverseFeaturedItem featuredItem) throws Inva
}

private void prepareUploadedImageFile() throws IOException, InvalidCommandArgumentsException {
// Step 1: Create a temporary directory to store the uploaded image
Path tempDir = createTempDir();
File uploadedFile = new File(tempDir.toFile(), newDataverseFeaturedItemDTO.getImageFileName());
// Step 1: Create a directory to store the uploaded image
Path imageDir = createImageDir();
File uploadedFile = new File(imageDir.toFile(), newDataverseFeaturedItemDTO.getImageFileName());

if (!uploadedFile.exists()) {
uploadedFile.createNewFile();
Expand All @@ -74,10 +74,10 @@ private void prepareUploadedImageFile() throws IOException, InvalidCommandArgume
Files.copy(tempFile.toPath(), uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

private Path createTempDir() throws IOException {
Path tempRoot = Path.of(JvmSettings.DOCROOT_DIRECTORY.lookup(), JvmSettings.FEATURED_ITEMS_IMAGE_UPLOADS_DIRECTORY.lookup(), dataverse.getId().toString(), String.valueOf(newDataverseFeaturedItemDTO.getDisplayOrder()));
Files.createDirectories(tempRoot);
return tempRoot;
private Path createImageDir() throws IOException {
Path imagePath = Path.of(JvmSettings.DOCROOT_DIRECTORY.lookup(), JvmSettings.FEATURED_ITEMS_IMAGE_UPLOADS_DIRECTORY.lookup(), dataverse.getId().toString());
Files.createDirectories(imagePath);
return imagePath;
}

private void validateFile(File file) throws IOException, InvalidCommandArgumentsException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.harvard.iq.dataverse.engine.command.impl;

import edu.harvard.iq.dataverse.DataverseFeaturedItem;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
* Retrieves a particular featured item {@link DataverseFeaturedItem}.
*/
public class GetDataverseFeaturedItemCommand extends AbstractCommand<DataverseFeaturedItem> {

private final DataverseFeaturedItem dataverseFeaturedItem;

public GetDataverseFeaturedItemCommand(DataverseRequest request, DataverseFeaturedItem dataverseFeaturedItem) {
super(request, dataverseFeaturedItem.getDataverse());
this.dataverseFeaturedItem = dataverseFeaturedItem;
}

@Override
public DataverseFeaturedItem execute(CommandContext ctxt) throws CommandException {
return dataverseFeaturedItem;
}

@Override
public Map<String, Set<Permission>> getRequiredPermissions() {
return Collections.singletonMap("",
dataverseFeaturedItem.getDataverse().isReleased() ? Collections.emptySet()
: Collections.singleton(Permission.ViewUnpublishedDataverse));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Set;

/**
* List the featured items {@link DataverseFeaturedItem} of a {@link Dataverse}.
* Lists the featured items {@link DataverseFeaturedItem} of a {@link Dataverse}.
*/
public class ListDataverseFeaturedItemsCommand extends AbstractCommand<List<DataverseFeaturedItem>> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,7 @@ public static JsonObjectBuilder json(DataverseFeaturedItem dataverseFeaturedItem
.add("id", dataverseFeaturedItem.getId())
.add("content", dataverseFeaturedItem.getContent())
.add("imageFileName", dataverseFeaturedItem.getImageFileName())
.add("imageFileUrl", dataverseFeaturedItem.getImageFileUrl())
.add("displayOrder", dataverseFeaturedItem.getDisplayOrder());
}
}

0 comments on commit ec3b6f2

Please sign in to comment.