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();
}
Aggregations