Search in sources :

Example 26 with Attachment

use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.

the class MailTemplateImageAttachmentsExtractor method getImages.

/**
 * @param documentReference the reference of the document
 * @return all images contained in the attachments of the given document
 * @throws Exception if an error occurs
 */
public Collection<Attachment> getImages(DocumentReference documentReference) throws Exception {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    Document document = new Document(xwiki.getDocument(documentReference, context), context);
    return document.getAttachmentList().stream().filter(att -> att.isImage()).collect(Collectors.toList());
}
Also used : Inject(javax.inject.Inject) XWiki(com.xpn.xwiki.XWiki) Attachment(com.xpn.xwiki.api.Attachment) Provider(javax.inject.Provider) Component(org.xwiki.component.annotation.Component) DocumentReference(org.xwiki.model.reference.DocumentReference) Collection(java.util.Collection) XWikiContext(com.xpn.xwiki.XWikiContext) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) Document(com.xpn.xwiki.api.Document) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Document(com.xpn.xwiki.api.Document)

Example 27 with Attachment

use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.

the class WatchListEventMimeMessageIterator method updateFactoryParameters.

/**
 * Update the factory's parameters with the values specific to the message we are going to send right now.
 *
 * @param watchListMessageData the messageData to use for the new message
 * @param factoryParameters the factory parameters we wish to update
 */
private void updateFactoryParameters(Map<String, Object> factoryParameters, WatchListMessageData watchListMessageData) {
    Map<String, Object> velocityVariables = (Map<String, Object>) factoryParameters.get("velocityVariables");
    // Set the list of events, containing 1 event per document.
    List<WatchListEvent> events = watchListMessageData.getEvents();
    velocityVariables.put("events", events);
    // Compute the list of modified documents.
    List<String> modifiedDocuments = new ArrayList<>();
    for (WatchListEvent event : events) {
        if (!modifiedDocuments.contains(event.getPrefixedFullName())) {
            modifiedDocuments.add(event.getPrefixedFullName());
        }
    }
    velocityVariables.put("modifiedDocuments", modifiedDocuments);
    velocityVariables.put(XWIKI_USER_CLASS_FIRST_NAME_PROP, watchListMessageData.getFirstName());
    velocityVariables.put(XWIKI_USER_CLASS_LAST_NAME_PROP, watchListMessageData.getLastName());
    velocityVariables.put(SUBSCRIBER_REFERENCE, watchListMessageData.getUserReference());
    // Attach the avatars of the authors of the events we are notifying about.
    if (parameters.get(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER) == Boolean.TRUE) {
        List<Attachment> templateExtraAttachments = getTemplateExtraAttachments(factoryParameters, events);
        factoryParameters.put(TEMPLATE_FACTORY_ATTACHMENTS_PARAMETER, templateExtraAttachments);
    }
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) ArrayList(java.util.ArrayList) Attachment(com.xpn.xwiki.api.Attachment) Map(java.util.Map)

Example 28 with Attachment

use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.

the class WatchListEventMimeMessageIterator method getTemplateExtraAttachments.

private List<Attachment> getTemplateExtraAttachments(Map<String, Object> factoryParameters, List<WatchListEvent> events) {
    // Append to any existing list of extra attachments specified by the caller.
    List<Attachment> templateExtraAttachments = new ArrayList<>();
    if (originalTemplateExtraParameters != null) {
        templateExtraAttachments.addAll(originalTemplateExtraParameters);
    }
    Set<DocumentReference> processedAuthors = new HashSet<DocumentReference>();
    for (WatchListEvent event : events) {
        for (DocumentReference authorReference : event.getAuthorReferences()) {
            // instead since that would also work across messages and would be much more useful.
            if (!processedAuthors.contains(authorReference)) {
                Attachment avatarAttachment = avatarExtractor.getUserAvatar(authorReference);
                if (avatarAttachment != null) {
                    templateExtraAttachments.add(avatarAttachment);
                }
                processedAuthors.add(authorReference);
            }
        }
    }
    return templateExtraAttachments;
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) ArrayList(java.util.ArrayList) Attachment(com.xpn.xwiki.api.Attachment) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 29 with Attachment

use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.

the class ZipExplorerPlugin method getFileList.

/**
 * @param document the document containing the ZIP file as an attachment
 * @param attachmentName the name under which the ZIP file is attached in the document
 * @param context not used
 * @return the list of file entries in the ZIP file attached under the passed attachment name inside the passed
 *         document
 * @see com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPluginAPI#getFileList
 */
public List<String> getFileList(Document document, String attachmentName, XWikiContext context) {
    List<String> zipList = new ArrayList<String>();
    Attachment attachment = document.getAttachment(attachmentName);
    InputStream stream = null;
    try {
        stream = new ByteArrayInputStream(attachment.getContent());
        if (isZipFile(stream)) {
            ZipInputStream zis = new ZipInputStream(stream);
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                zipList.add(entry.getName());
            }
        }
    } catch (XWikiException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return zipList;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException)

Example 30 with Attachment

use of com.xpn.xwiki.api.Attachment in project celements-blog by celements.

the class NewsletterAttachmentService method addAttachment.

@Override
public void addAttachment(String attFullname) {
    Attachment att = getAttachmentForFullname(attFullname);
    extendAttachmentList(att, DEFAULT_NL_ATTACHMENT_LIST);
    extendAttachmentList(att, DEFAULT_NL_NO_IMG_ATT_LIST);
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment)

Aggregations

Attachment (com.xpn.xwiki.api.Attachment)38 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)24 Test (org.junit.Test)21 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 Document (com.xpn.xwiki.api.Document)14 DocumentReference (org.xwiki.model.reference.DocumentReference)14 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)8 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)6 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 InputStream (java.io.InputStream)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)3 File (java.io.File)3 IOException (java.io.IOException)3