public Rendition getRendition(Asset asset) { List<Rendition> renditions = asset.getRenditions(); for (Rendition rendition : renditions) { if (rendition.getName().startsWith("cq5dam.web.")) { return rendition; } } return asset.getOriginal(); }
public BufferedImage getImage(final Rendition rendition, final Dimension dim) throws IOException { final Asset asset = rendition.getAsset(); final byte[] picture = (byte[]) extractMetadata(asset).getProperty(META_KEY_THUMBNAIL); // if we have an embedded image if (picture != null) { return new Layer(new ByteArrayInputStream(picture), dim).getImage(); } // according to the spec, a thumbnail representation of a document should be generated by // default when the // file is saved. log.warn("Failed to retrieve thumbnail for {}", asset.getPath()); return null; }
public ExtractedMetadata extractMetadata(final Asset asset) { final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(asset.getOriginal().getStream())); final ExtractedMetadata metadata = new ExtractedMetadata(); try { readEntries(zis, metadata, asset); } catch (Exception e) { log.warn("Failed to extract metadata for {} reason: {}", asset.getPath(), e.getMessage()); log.debug("Stack Trace: ", e); } finally { IOUtils.closeQuietly(zis); } setMimetype(metadata, asset); return metadata; }
/* all private methods */ private void readEntries( final ZipInputStream zis, final ExtractedMetadata metadata, final Asset asset) throws Exception { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { final String name = entry.getName(); if (name.equals(ENTRY_CORE) || name.equals(ENTRY_APP)) { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(zis, out); // build xml document to extract meta info DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); Document document = documentBuilderFactory .newDocumentBuilder() .parse(new ByteArrayInputStream(out.toByteArray())); IOUtils.closeQuietly(out); DocumentTraversal dt = (DocumentTraversal) document; NodeIterator nit = dt.createNodeIterator(document, NodeFilter.SHOW_ELEMENT, null, true); nit.nextNode(); // skip first node Element next; while ((next = (Element) nit.nextNode()) != null) { metadata.setMetaDataProperty(next.getLocalName(), next.getTextContent()); } } else if (name.equals(ENTRY_THUMBNAIL)) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { IOUtils.copy(zis, out); metadata.setProperty(META_KEY_THUMBNAIL, out.toByteArray()); } finally { IOUtils.closeQuietly(out); } } else if (name.equals(ENTRY_THUMBNAIL_EMF)) { String mimeType = mimeTypeService.getMimeType(name); AssetHandler handler = store.getAssetHandler(mimeType); Rendition rend = asset.addRendition("thumbnail.emf", zis, mimeType); BufferedImage img = handler.getImage(rend); if (img != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(img, JPEG_FORMAT, baos); baos.flush(); metadata.setProperty(META_KEY_THUMBNAIL, baos.toByteArray()); } finally { IOUtils.closeQuietly(baos); } } else { log.info("Cannot extract image from EMF format"); } } } }
public ExtractedMetadata extractMetadata(final Asset asset) { ExtractedMetadata metadata = new ExtractedMetadata(); log.debug("extractMetadata: start extracting asset [{}]", asset.getPath()); // extract metadata final InputStream is = asset.getOriginal().getStream(); final ZipInputStream zis = new ZipInputStream(is); final StringBuffer buffer = new StringBuffer(); int count = 0; try { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory()) { buffer.append(entry.getName()).append('\n'); count++; } zis.closeEntry(); } metadata.setMetaDataProperty("Content", buffer.toString()); metadata.setMetaDataProperty("File Count", count); } catch (IOException e) { log.warn("extractMetadata: error while reading ZIP archive [{}]: ", asset.getPath(), e); } catch (IllegalArgumentException iae) { // bug#28534 log.warn("extractMetadata: error while reading ZIP entry [{}]: ", asset.getPath(), iae); } finally { IOUtils.closeQuietly(zis); } // Get XMP execGenericProcessor(asset.getOriginal().getStream(), metadata); setMimetype(metadata, asset); return metadata; }