Search in sources :

Example 51 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class AttachmentSolrMetadataExtractor method setFieldsInternal.

@Override
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) throws Exception {
    AttachmentReference attachmentReference = new AttachmentReference(entityReference);
    XWikiDocument originalDocument = getDocument(attachmentReference.getDocumentReference());
    XWikiAttachment attachment = originalDocument.getAttachment(attachmentReference.getName());
    if (attachment == null) {
        return false;
    }
    XWikiContext xcontext = xcontextProvider.get();
    solrDocument.setField(FieldUtils.FILENAME, attachment.getFilename());
    solrDocument.setField(FieldUtils.FILENAME_SORT, attachment.getFilename());
    solrDocument.setField(FieldUtils.MIME_TYPE, attachment.getMimeType(xcontext));
    solrDocument.setField(FieldUtils.ATTACHMENT_DATE, attachment.getDate());
    // We need to add a dedicated sort field because the corresponding field is multiValued and thus cannot be used
    // for sorting (the reason it is multiValued is because it is 'reused' on document rows and documents can have
    // multiple attachments).
    solrDocument.setField(FieldUtils.ATTACHMENT_DATE_SORT, attachment.getDate());
    solrDocument.setField(FieldUtils.ATTACHMENT_SIZE, attachment.getLongSize());
    solrDocument.setField(FieldUtils.ATTACHMENT_SIZE_SORT, attachment.getLongSize());
    // We need to index the attachment version (revision) to be able to detect when the search index is out of date
    // (not in sync with the database).
    solrDocument.setField(FieldUtils.ATTACHMENT_VERSION, attachment.getVersion());
    // Index the full author reference for exact matching (faceting).
    String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference());
    solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference);
    try {
        // Index the author display name for free text search and results sorting.
        String authorDisplayName = xcontext.getWiki().getPlainUserName(attachment.getAuthorReference(), xcontext);
        solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY, authorDisplayName);
        solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY_SORT, authorDisplayName);
    } catch (Exception e) {
        this.logger.error("Failed to get author display name for attachment [{}]", attachment.getReference(), e);
    }
    setLocaleAndContentFields(attachment, solrDocument);
    return true;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 52 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class LogoAttachmentExtractor method getLogo.

/**
 * @return an attachment holding the logo of the wiki
 * @throws Exception if an error happens
 */
public Attachment getLogo() throws Exception {
    XWikiContext context = xwikiContextProvider.get();
    String colorTheme = configurationSource.getProperty("colorTheme");
    if (StringUtils.isNotBlank(colorTheme)) {
        DocumentReference colorThemeRef = documentReferenceResolver.resolve(colorTheme);
        XWiki xwiki = context.getWiki();
        XWikiDocument doc = xwiki.getDocument(colorThemeRef, context);
        String logo = doc.getStringValue(LOGO);
        if (StringUtils.isBlank(logo)) {
            logo = doc.getStringValue("logoImage");
        }
        if (isLogoAttachementValid(doc, logo)) {
            XWikiAttachment attachment = doc.getAttachment(logo);
            attachment.setFilename(LOGO);
            return new Attachment(new Document(doc, context), attachment, context);
        }
    }
    String skin = configurationSource.getProperty("skin");
    if (StringUtils.isNotBlank(skin)) {
        DocumentReference skinRef = documentReferenceResolver.resolve(skin);
        XWiki xwiki = context.getWiki();
        XWikiDocument doc = xwiki.getDocument(skinRef, context);
        String logo = doc.getStringValue(LOGO);
        if (isLogoAttachementValid(doc, logo)) {
            XWikiAttachment attachment = doc.getAttachment(logo);
            attachment.setFilename(LOGO);
            return new Attachment(new Document(doc, context), attachment, context);
        }
    }
    XWikiAttachment fakeAttachment = new XWikiAttachment();
    Resource sourceImageIS = internalSkinManager.getCurrentSkin(true).getResource("logo.png");
    InputStream inputStream = environment.getResourceAsStream(sourceImageIS.getPath());
    fakeAttachment.setAttachment_content(new XWikiAttachmentContent());
    fakeAttachment.getAttachment_content().setContent(inputStream);
    fakeAttachment.setFilename(LOGO);
    return new Attachment(null, fakeAttachment, context);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) InputStream(java.io.InputStream) Resource(org.xwiki.skin.Resource) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent)

Example 53 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class UserAvatarAttachmentExtractor method getUserAvatar.

/**
 * @param userReference reference of the user
 * @param size the wanted size for the image
 * @return a fake attachment holding a resized and square-cropped version of the avatar of the given user
 * @throws Exception if an error happens
 */
public Attachment getUserAvatar(DocumentReference userReference, int size) throws Exception {
    InputStream imageStream = null;
    try {
        imageStream = getUserAvatarStream(userReference);
        XWikiAttachment fakeAttachment = new XWikiAttachment();
        XWikiAttachmentContent content = new XWikiAttachmentContent(fakeAttachment);
        resizeImage(imageStream, size, content.getContentOutputStream());
        fakeAttachment.setAttachment_content(content);
        fakeAttachment.setFilename(String.format("%s.jpg", userReference != null ? userReference.getName() : "XWikiGuest"));
        return new Attachment(null, fakeAttachment, xwikiContextProvider.get());
    } catch (Exception e) {
        throw new Exception(String.format("Failed to resize the avatar of [%s].", userReference), e);
    } finally {
        IOUtils.closeQuietly(imageStream);
    }
}
Also used : InputStream(java.io.InputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) IOException(java.io.IOException)

Example 54 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class UserAvatarAttachmentExtractor method getUserAvatarStream.

private InputStream getUserAvatarStream(DocumentReference userReference) {
    if (userReference != null) {
        try {
            XWikiContext context = xwikiContextProvider.get();
            XWiki xwiki = context.getWiki();
            XWikiDocument userProfileDocument = xwiki.getDocument(userReference, context);
            DocumentReference usersClassReference = xwiki.getUserClass(context).getDocumentReference();
            String avatarFileName = userProfileDocument.getStringValue(usersClassReference, "avatar");
            XWikiAttachment attachment = userProfileDocument.getAttachment(avatarFileName);
            if (attachment != null && attachment.isImage(context)) {
                return attachment.getContentInputStream(context);
            }
        } catch (Exception e) {
            logger.warn("Failed to get the avatar of [{}]. Fallback to default one.", userReference, e);
        }
    }
    return getDefaultAvatarStream();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) IOException(java.io.IOException)

Example 55 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class DocumentInstanceOutputFilterStream method maybeSaveDocument.

private void maybeSaveDocument() throws FilterException {
    XWikiDocument inputDocument = this.documentListener.getEntity();
    this.documentListener.setEntity(null);
    if (this.currentRevisionParameters == null) {
        return;
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument document = xcontext.getWiki().getDocument(inputDocument.getDocumentReferenceWithLocale(), xcontext);
        if (!this.documentDeleted && !document.isNew() && this.properties.isPreviousDeleted()) {
            XWikiDocument originalDocument = document;
            // Save current context wiki
            WikiReference currentWiki = xcontext.getWikiReference();
            try {
                // Make sure the store is executed in the right context
                xcontext.setWikiReference(document.getDocumentReference().getWikiReference());
                // Put previous version in recycle bin
                if (xcontext.getWiki().hasRecycleBin(xcontext)) {
                    xcontext.getWiki().getRecycleBinStore().saveToRecycleBin(document, xcontext.getUser(), new Date(), xcontext, true);
                }
                // Make sure to not generate DocumentDeletedEvent since from listener point of view it's not
                xcontext.getWiki().getStore().deleteXWikiDoc(document, xcontext);
                this.documentDeleted = true;
            } finally {
                // Restore current context wiki
                xcontext.setWikiReference(currentWiki);
            }
            document = xcontext.getWiki().getDocument(inputDocument.getDocumentReferenceWithLocale(), xcontext);
            // Remember deleted document as the actual previous version of the document (to simulate an update
            // instead of a creation)
            document.setOriginalDocument(originalDocument);
        } else {
            // Make sure to remember that the document should not be deleted anymore
            this.documentDeleted = true;
        }
        // Remember if it's a creation or an update
        boolean isnew = document.isNew();
        // Safer to clone for thread safety and in case the save fail
        document = document.clone();
        document.loadAttachmentsContentSafe(xcontext);
        document.apply(inputDocument);
        // Get the version from the input document
        document.setMinorEdit(inputDocument.isMinorEdit());
        if (!this.properties.isAuthorPreserved()) {
            if (this.properties.isAuthorSet()) {
                setAuthorReference(document, this.properties.getAuthor());
            } else {
                setAuthorReference(document, xcontext.getUserReference());
            }
            document.setContentAuthorReference(document.getAuthorReference());
            if (document.isNew()) {
                document.setCreatorReference(document.getAuthorReference());
            }
        } else {
            setAuthors(document, inputDocument);
        }
        if (this.properties.isVersionPreserved()) {
            // Make sure to use metadata coming from the input document
            document.setVersion(inputDocument.getVersion());
            document.setDate(inputDocument.getDate());
            document.setContentUpdateDate(inputDocument.getContentUpdateDate());
            for (XWikiAttachment attachment : document.getAttachmentList()) {
                attachment.setVersion(inputDocument.getAttachment(attachment.getFilename()).getVersion());
            }
            if (document.isNew()) {
                document.setCreationDate(inputDocument.getCreationDate());
                document.setDocumentArchive(inputDocument.getDocumentArchive());
            }
            // Make sure the document won't be modified by the store
            document.setMetaDataDirty(false);
            document.setContentDirty(false);
            xcontext.getWiki().saveDocument(document, inputDocument.getComment(), inputDocument.isMinorEdit(), xcontext);
        } else {
            // Forget the input history to let the store do its standard job
            document.setDocumentArchive((XWikiDocumentArchive) null);
            xcontext.getWiki().saveDocument(document, this.properties.getSaveComment(), xcontext);
        }
        if (this.properties.isVerbose()) {
            if (isnew) {
                this.logger.info(LOG_DOCUMENT_CREATED, "Created document [{}]", document.getDocumentReferenceWithLocale());
            } else {
                this.logger.info(LOG_DOCUMENT_UPDATED, "Updated document [{}]", document.getDocumentReferenceWithLocale());
            }
        }
    } catch (Exception e) {
        this.logger.error(LOG_DOCUMENT_FAILSAVE, "Failed to save document [{}]", inputDocument.getDocumentReferenceWithLocale(), e);
        if (this.properties.isStoppedWhenSaveFail()) {
            throw new FilterException("Failed to save document", e);
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) WikiReference(org.xwiki.model.reference.WikiReference) Date(java.util.Date) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)133 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)71 DocumentReference (org.xwiki.model.reference.DocumentReference)51 Test (org.junit.Test)40 XWikiContext (com.xpn.xwiki.XWikiContext)35 XWikiException (com.xpn.xwiki.XWikiException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Attachment (com.xpn.xwiki.api.Attachment)18 Date (java.util.Date)17 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Document (com.xpn.xwiki.api.Document)14 XWiki (com.xpn.xwiki.XWiki)13 BaseObject (com.xpn.xwiki.objects.BaseObject)13 AttachmentReference (org.xwiki.model.reference.AttachmentReference)13 InputStream (java.io.InputStream)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 File (java.io.File)10 URL (java.net.URL)7 List (java.util.List)7