/* 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 BufferedImage getImage(final Rendition rendition) throws IOException { ExtractedMetadata metadata = extractMetadata(rendition.getAsset()); String text = (String) metadata.getMetaDataProperty("Content"); if (text != null && text.length() > 0) { // create text layer final Layer layer = new Layer(500, 600, Color.WHITE); layer.setPaint(Color.black); Font font = new Font("Arial", 12); String displayText = getDisplayText(text, 600, 12); layer.drawText(10, 10, 500, 600, displayText, font, Font.ALIGN_LEFT, 0, 0); layer.crop(new Rectangle(510, 600)); return layer.getImage(); } else { return null; } }
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; }