mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 10:04:21 +00:00
Multiple updates.
This commit is contained in:
@@ -50,8 +50,7 @@ public class CanvasDimensionCLI {
|
|||||||
|
|
||||||
boolean iiifEnabled = configurationService.getBooleanProperty("iiif.enabled");
|
boolean iiifEnabled = configurationService.getBooleanProperty("iiif.enabled");
|
||||||
if (!iiifEnabled) {
|
if (!iiifEnabled) {
|
||||||
System.out.println("IIIF is not enabled on this DSpace server.");
|
System.out.println("WARNING: IIIF is not enabled on this DSpace server.");
|
||||||
System.exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// default to not updating existing dimensions
|
// default to not updating existing dimensions
|
||||||
|
@@ -17,11 +17,11 @@ import java.net.URL;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.dspace.app.canvasdimension.service.IIIFApiQueryService;
|
import org.dspace.app.canvasdimension.service.IIIFApiQueryService;
|
||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.services.ConfigurationService;
|
import org.dspace.iiif.IIIFSharedUtils;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,14 +32,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
*/
|
*/
|
||||||
public class IIIFApiQueryServiceImpl implements IIIFApiQueryService, InitializingBean {
|
public class IIIFApiQueryServiceImpl implements IIIFApiQueryService, InitializingBean {
|
||||||
|
|
||||||
@Autowired()
|
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(IIIFApiQueryServiceImpl.class);
|
||||||
protected ConfigurationService configurationService;
|
|
||||||
|
|
||||||
String iiifImageServer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
iiifImageServer = configurationService.getProperty("iiif.image.server");
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,29 +51,39 @@ public class IIIFApiQueryServiceImpl implements IIIFApiQueryService, Initializin
|
|||||||
*/
|
*/
|
||||||
private int[] getIiifImageDimensions(Bitstream bitstream) {
|
private int[] getIiifImageDimensions(Bitstream bitstream) {
|
||||||
int[] arr = new int[2];
|
int[] arr = new int[2];
|
||||||
String path = iiifImageServer + bitstream.getID() + "/info.json";
|
String path = IIIFSharedUtils.getInfoJsonPath(bitstream);
|
||||||
URL url;
|
URL url;
|
||||||
|
BufferedReader in = null;
|
||||||
try {
|
try {
|
||||||
url = new URL(path);
|
url = new URL(path);
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
con.setRequestMethod("GET");
|
con.setRequestMethod("GET");
|
||||||
BufferedReader in = new BufferedReader(
|
in = new BufferedReader(
|
||||||
new InputStreamReader(con.getInputStream()));
|
new InputStreamReader(con.getInputStream()));
|
||||||
String inputLine;
|
String inputLine;
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder response = new StringBuilder();
|
||||||
while ((inputLine = in.readLine()) != null) {
|
while ((inputLine = in.readLine()) != null) {
|
||||||
response.append(inputLine);
|
response.append(inputLine);
|
||||||
}
|
}
|
||||||
in.close();
|
|
||||||
JsonNode parent = new ObjectMapper().readTree(response.toString());
|
JsonNode parent = new ObjectMapper().readTree(response.toString());
|
||||||
arr[0] = parent.get("width").asInt();
|
// return dimensions if found.
|
||||||
arr[1] = parent.get("height").asInt();
|
if (parent.has("width") && parent.has("height")) {
|
||||||
return checkDimensions(arr);
|
arr[0] = parent.get("width").asInt();
|
||||||
|
arr[1] = parent.get("height").asInt();
|
||||||
|
return checkDimensions(arr);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
if (in != null) {
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -7,13 +7,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.app.canvasdimension;
|
package org.dspace.app.canvasdimension;
|
||||||
|
|
||||||
|
import static org.dspace.iiif.IIIFSharedUtils.METADATA_IIIF_HEIGHT;
|
||||||
|
import static org.dspace.iiif.IIIFSharedUtils.METADATA_IIIF_IMAGE;
|
||||||
|
import static org.dspace.iiif.IIIFSharedUtils.METADATA_IIIF_SCHEMA;
|
||||||
|
import static org.dspace.iiif.IIIFSharedUtils.METADATA_IIIF_WIDTH;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.dspace.app.canvasdimension.service.IIIFApiQueryService;
|
import org.dspace.app.canvasdimension.service.IIIFApiQueryService;
|
||||||
import org.dspace.app.canvasdimension.service.IIIFCanvasDimensionService;
|
import org.dspace.app.canvasdimension.service.IIIFCanvasDimensionService;
|
||||||
|
import org.dspace.authorize.AuthorizeException;
|
||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.Bundle;
|
import org.dspace.content.Bundle;
|
||||||
import org.dspace.content.Collection;
|
import org.dspace.content.Collection;
|
||||||
@@ -149,9 +157,10 @@ public class IIIFCanvasDimensionServiceImpl implements IIIFCanvasDimensionServic
|
|||||||
* @return
|
* @return
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private boolean processBitstream(Context context, Bitstream bitstream) throws Exception {
|
private boolean processBitstream(Context context, Bitstream bitstream) throws SQLException, AuthorizeException,
|
||||||
|
IOException {
|
||||||
|
|
||||||
boolean processed = false;
|
boolean processed = false;
|
||||||
boolean isUnsupported = bitstream.getFormat(context).getMIMEType().contains("image/jp2");
|
|
||||||
boolean isImage = bitstream.getFormat(context).getMIMEType().contains("image/");
|
boolean isImage = bitstream.getFormat(context).getMIMEType().contains("image/");
|
||||||
if (isImage) {
|
if (isImage) {
|
||||||
Optional<MetadataValue> op = bitstream.getMetadata().stream()
|
Optional<MetadataValue> op = bitstream.getMetadata().stream()
|
||||||
@@ -162,12 +171,25 @@ public class IIIFCanvasDimensionServiceImpl implements IIIFCanvasDimensionServic
|
|||||||
System.out.println("Force processing for bitstream: " + bitstream.getID());
|
System.out.println("Force processing for bitstream: " + bitstream.getID());
|
||||||
}
|
}
|
||||||
int[] dims;
|
int[] dims;
|
||||||
if (isUnsupported) {
|
InputStream stream = null;
|
||||||
dims = iiifApiQuery.getImageDimensions(bitstream);
|
try {
|
||||||
} else {
|
stream = bitstreamService.retrieve(context, bitstream);
|
||||||
InputStream stream = bitstreamService.retrieve(context, bitstream);
|
try {
|
||||||
dims = ImageDimensionReader.getImageDimensions(stream);
|
dims = ImageDimensionReader.getImageDimensions(stream);
|
||||||
|
if (dims == null) {
|
||||||
|
// If image dimensions are not available try the iiif image server.
|
||||||
|
dims = iiifApiQuery.getImageDimensions(bitstream);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// If an exception was raised by ImageIO, try the iiif image server.
|
||||||
|
dims = iiifApiQuery.getImageDimensions(bitstream);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (stream != null) {
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dims != null) {
|
if (dims != null) {
|
||||||
processed = setBitstreamMetadata(context, bitstream, dims);
|
processed = setBitstreamMetadata(context, bitstream, dims);
|
||||||
// update the bitstream
|
// update the bitstream
|
||||||
@@ -185,15 +207,15 @@ public class IIIFCanvasDimensionServiceImpl implements IIIFCanvasDimensionServic
|
|||||||
* @param dims
|
* @param dims
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private boolean setBitstreamMetadata(Context context, Bitstream bitstream, int[] dims) throws Exception {
|
private boolean setBitstreamMetadata(Context context, Bitstream bitstream, int[] dims) throws SQLException {
|
||||||
dSpaceObjectService.clearMetadata(context, bitstream, "iiif",
|
dSpaceObjectService.clearMetadata(context, bitstream, METADATA_IIIF_SCHEMA,
|
||||||
"image", "width", Item.ANY);
|
METADATA_IIIF_IMAGE, METADATA_IIIF_WIDTH, Item.ANY);
|
||||||
dSpaceObjectService.setMetadataSingleValue(context, bitstream, "iiif",
|
dSpaceObjectService.setMetadataSingleValue(context, bitstream, METADATA_IIIF_SCHEMA,
|
||||||
"image", "width", Item.ANY, String.valueOf(dims[0]));
|
METADATA_IIIF_IMAGE, METADATA_IIIF_WIDTH, null, String.valueOf(dims[0]));
|
||||||
dSpaceObjectService.clearMetadata(context, bitstream, "iiif",
|
dSpaceObjectService.clearMetadata(context, bitstream, METADATA_IIIF_SCHEMA,
|
||||||
"image", "height", Item.ANY);
|
METADATA_IIIF_IMAGE, METADATA_IIIF_HEIGHT, Item.ANY);
|
||||||
dSpaceObjectService.setMetadataSingleValue(context, bitstream, "iiif",
|
dSpaceObjectService.setMetadataSingleValue(context, bitstream, METADATA_IIIF_SCHEMA,
|
||||||
"image", "height", Item.ANY, String.valueOf(dims[1]));
|
METADATA_IIIF_IMAGE, METADATA_IIIF_HEIGHT, null, String.valueOf(dims[1]));
|
||||||
if (!isQuiet) {
|
if (!isQuiet) {
|
||||||
System.out.println("Added IIIF canvas metadata to bitstream: " + bitstream.getID());
|
System.out.println("Added IIIF canvas metadata to bitstream: " + bitstream.getID());
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@ package org.dspace.app.canvasdimension;
|
|||||||
import static org.dspace.app.canvasdimension.Util.checkDimensions;
|
import static org.dspace.app.canvasdimension.Util.checkDimensions;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
@@ -25,18 +26,20 @@ public class ImageDimensionReader {
|
|||||||
/**
|
/**
|
||||||
* Uses ImageIO to read height and width dimensions.
|
* Uses ImageIO to read height and width dimensions.
|
||||||
* @param image inputstream for dspace image
|
* @param image inputstream for dspace image
|
||||||
* @return image dimensions
|
* @return image dimensions or null if the image format cannot be read.
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static int[] getImageDimensions(InputStream image) throws Exception {
|
public static int[] getImageDimensions(InputStream image) throws IOException {
|
||||||
int[] dims = new int[2];
|
int[] dims = new int[2];
|
||||||
BufferedImage buf = ImageIO.read(image);
|
BufferedImage buf = ImageIO.read(image);
|
||||||
int width = buf.getWidth(null);
|
if (buf != null) {
|
||||||
int height = buf.getHeight(null);
|
int width = buf.getWidth(null);
|
||||||
if (width > 0 && height > 0) {
|
int height = buf.getHeight(null);
|
||||||
dims[0] = width;
|
if (width > 0 && height > 0) {
|
||||||
dims[1] = height;
|
dims[0] = width;
|
||||||
return checkDimensions(dims);
|
dims[1] = height;
|
||||||
|
return checkDimensions(dims);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -12,10 +12,13 @@ import java.util.List;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.Bundle;
|
import org.dspace.content.Bundle;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.core.Constants;
|
import org.dspace.core.Constants;
|
||||||
import org.dspace.license.CreativeCommonsServiceImpl;
|
import org.dspace.license.CreativeCommonsServiceImpl;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
import org.dspace.services.factory.DSpaceServicesFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shared utilities for IIIF processing.
|
* Shared utilities for IIIF processing.
|
||||||
@@ -29,6 +32,15 @@ public class IIIFSharedUtils {
|
|||||||
public static final String METADATA_IIIF_ENABLED = "dspace.iiif.enabled";
|
public static final String METADATA_IIIF_ENABLED = "dspace.iiif.enabled";
|
||||||
// The DSpace bundle for other content related to item.
|
// The DSpace bundle for other content related to item.
|
||||||
protected static final String OTHER_CONTENT_BUNDLE = "OtherContent";
|
protected static final String OTHER_CONTENT_BUNDLE = "OtherContent";
|
||||||
|
// The IIIF image server url from configuration
|
||||||
|
protected static final String IMAGE_SERVER_PATH = "iiif.image.server";
|
||||||
|
public static final String METADATA_IIIF_SCHEMA = "iiif";
|
||||||
|
public static final String METADATA_IIIF_IMAGE = "image";
|
||||||
|
public static final String METADATA_IIIF_HEIGHT = "height";
|
||||||
|
public static final String METADATA_IIIF_WIDTH = "width";
|
||||||
|
|
||||||
|
protected static final ConfigurationService configurationService
|
||||||
|
= DSpaceServicesFactory.getInstance().getConfigurationService();
|
||||||
|
|
||||||
private IIIFSharedUtils() {}
|
private IIIFSharedUtils() {}
|
||||||
|
|
||||||
@@ -87,4 +99,14 @@ public class IIIFSharedUtils {
|
|||||||
.filter(m -> m.getMetadataField().toString('.').contentEquals(METADATA_IIIF_ENABLED))
|
.filter(m -> m.getMetadataField().toString('.').contentEquals(METADATA_IIIF_ENABLED))
|
||||||
.noneMatch(m -> m.getValue().equalsIgnoreCase("false") || m.getValue().equalsIgnoreCase("no"));
|
.noneMatch(m -> m.getValue().equalsIgnoreCase("false") || m.getValue().equalsIgnoreCase("no"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns url for retrieving info.json metadata from the image server.
|
||||||
|
* @param bitstream
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getInfoJsonPath(Bitstream bitstream) {
|
||||||
|
String iiifImageServer = configurationService.getProperty(IMAGE_SERVER_PATH);
|
||||||
|
return iiifImageServer + bitstream.getID() + "/info.json";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user