Search in sources :

Example 1 with PSTAttachment

use of com.pff.PSTAttachment in project Xponents by OpenSextant.

the class OutlookPSTCrawler method processAttachments.

/**
     * REFERENCE:  libpst home page, https://code.google.com/p/java-libpst/
     * @author com.pff
     * @author ubaldino -- cleaned up name checks.
     * @param msg  PST API message
     * @param msgFolder output target
     * @return list of attachment filenames
     * @throws PSTException err
     * @throws IOException err
     * @throws ConfigException err
     */
public List<String> processAttachments(PSTMessage msg, File msgFolder) throws PSTException, IOException, ConfigException {
    int numberOfAttachments = msg.getNumberOfAttachments();
    List<String> attachmentFilenames = new ArrayList<String>();
    for (int x = 0; x < numberOfAttachments; x++) {
        PSTAttachment attach = msg.getAttachment(x);
        // both long and short filenames can be used for attachments
        String filename = attach.getLongFilename();
        if (StringUtils.isBlank(filename)) {
            filename = attach.getFilename();
            if (StringUtils.isBlank(filename)) {
                filename = String.format("attachment%d", x + 1);
            }
        }
        File attPath = new File(String.format("%s/%s", msgFolder.getAbsolutePath(), filename));
        savePSTFile(attach.getFileInputStream(), attPath.getAbsolutePath());
        attachmentFilenames.add(filename);
        if (listener != null) {
            listener.collected(attPath);
        }
        if (converter != null) {
            converter.convert(attPath);
        }
    }
    return attachmentFilenames;
}
Also used : ArrayList(java.util.ArrayList) PSTAttachment(com.pff.PSTAttachment) File(java.io.File) PSTFile(com.pff.PSTFile)

Example 2 with PSTAttachment

use of com.pff.PSTAttachment in project tika by apache.

the class OutlookPSTParser method parseMailAttachments.

private void parseMailAttachments(XHTMLContentHandler xhtml, PSTMessage email, EmbeddedDocumentExtractor embeddedExtractor) throws TikaException {
    int numberOfAttachments = email.getNumberOfAttachments();
    for (int i = 0; i < numberOfAttachments; i++) {
        File tempFile = null;
        try {
            PSTAttachment attach = email.getAttachment(i);
            // Get the filename; both long and short filenames can be used for attachments
            String filename = attach.getLongFilename();
            if (filename.isEmpty()) {
                filename = attach.getFilename();
            }
            xhtml.element("p", filename);
            Metadata attachMeta = new Metadata();
            attachMeta.set(Metadata.RESOURCE_NAME_KEY, filename);
            attachMeta.set(Metadata.EMBEDDED_RELATIONSHIP_ID, filename);
            AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
            attributes.addAttribute("", "id", "id", "CDATA", filename);
            xhtml.startElement("div", attributes);
            if (embeddedExtractor.shouldParseEmbedded(attachMeta)) {
                TemporaryResources tmp = new TemporaryResources();
                try {
                    TikaInputStream tis = TikaInputStream.get(attach.getFileInputStream(), tmp);
                    embeddedExtractor.parseEmbedded(tis, xhtml, attachMeta, true);
                } finally {
                    tmp.dispose();
                }
            }
            xhtml.endElement("div");
        } catch (Exception e) {
            throw new TikaException("Unable to unpack document stream", e);
        } finally {
            if (tempFile != null)
                tempFile.delete();
        }
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) TikaException(org.apache.tika.exception.TikaException) Metadata(org.apache.tika.metadata.Metadata) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) PSTAttachment(com.pff.PSTAttachment) File(java.io.File) PSTFile(com.pff.PSTFile) TikaException(org.apache.tika.exception.TikaException) IOException(java.io.IOException) PSTException(com.pff.PSTException) SAXException(org.xml.sax.SAXException)

Aggregations

PSTAttachment (com.pff.PSTAttachment)2 PSTFile (com.pff.PSTFile)2 File (java.io.File)2 PSTException (com.pff.PSTException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TikaException (org.apache.tika.exception.TikaException)1 TemporaryResources (org.apache.tika.io.TemporaryResources)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 Metadata (org.apache.tika.metadata.Metadata)1 SAXException (org.xml.sax.SAXException)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1