Search in sources :

Example 1 with MCRThumbnailGenerator

use of org.mycore.media.services.MCRThumbnailGenerator in project mycore by MyCoRe-Org.

the class MCRThumbnailResource method getThumbnail.

private Response getThumbnail(String documentId, int size, String ext) {
    List<MCRObjectID> derivateIds = MCRMetadataManager.getDerivateIds(MCRJerseyUtil.getID(documentId), 1, TimeUnit.MINUTES);
    for (MCRObjectID derivateId : derivateIds) {
        if (MCRAccessManager.checkPermissionForReadingDerivate(derivateId.toString())) {
            String nameOfMainFile = MCRMetadataManager.retrieveMCRDerivate(derivateId).getDerivate().getInternals().getMainDoc();
            if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
                MCRPath mainFile = MCRPath.getPath(derivateId.toString(), '/' + nameOfMainFile);
                try {
                    String mimeType = Files.probeContentType(mainFile);
                    String generators = MCRConfiguration.instance().getString("MCR.Media.Thumbnail.Generators");
                    for (String generator : generators.split(",")) {
                        Class<MCRThumbnailGenerator> classObject = (Class<MCRThumbnailGenerator>) Class.forName(generator);
                        Constructor<MCRThumbnailGenerator> constructor = classObject.getConstructor();
                        MCRThumbnailGenerator thumbnailGenerator = constructor.newInstance();
                        if (thumbnailGenerator.matchesFileType(mimeType, mainFile)) {
                            FileTime lastModified = Files.getLastModifiedTime(mainFile);
                            Date lastModifiedDate = new Date(lastModified.toMillis());
                            Response.ResponseBuilder resp = request.evaluatePreconditions(lastModifiedDate);
                            if (resp == null) {
                                Optional<BufferedImage> thumbnail = thumbnailGenerator.getThumbnail(mainFile, size);
                                if (thumbnail.isPresent()) {
                                    CacheControl cc = new CacheControl();
                                    cc.setMaxAge((int) TimeUnit.DAYS.toSeconds(1));
                                    String type = "image/png";
                                    if ("jpg".equals(ext) || "jpeg".equals(ext)) {
                                        type = "image/jpeg";
                                    }
                                    return Response.ok(thumbnail.get()).cacheControl(cc).lastModified(lastModifiedDate).type(type).build();
                                }
                                return Response.status(Response.Status.NOT_FOUND).build();
                            }
                            return resp.build();
                        }
                    }
                } catch (IOException | ReflectiveOperationException e) {
                    throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
                }
            }
        }
    }
    return Response.status(Response.Status.NOT_FOUND).build();
}
Also used : MCRThumbnailGenerator(org.mycore.media.services.MCRThumbnailGenerator) WebApplicationException(javax.ws.rs.WebApplicationException) FileTime(java.nio.file.attribute.FileTime) IOException(java.io.IOException) Date(java.util.Date) BufferedImage(java.awt.image.BufferedImage) Response(javax.ws.rs.core.Response) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) CacheControl(javax.ws.rs.core.CacheControl) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 FileTime (java.nio.file.attribute.FileTime)1 Date (java.util.Date)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 CacheControl (javax.ws.rs.core.CacheControl)1 Response (javax.ws.rs.core.Response)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRThumbnailGenerator (org.mycore.media.services.MCRThumbnailGenerator)1