Esempio n. 1
0
 /* 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");
       }
     }
   }
 }
Esempio n. 2
0
  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;
  }