Search in sources :

Example 1 with XWikiAttachmentContent

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

the class FilesystemAttachmentStore method getAttachmentContentSaveRunnable.

/**
 * Get a TransactionRunnable for saving the attachment content. If {@link XWikiAttachment#getAttachment_content()}
 * yields null, this runnable will do nothing.
 *
 * @param attachment the XWikiAttachment whose content should be saved.
 * @param updateDocument whether or not to update the document at the same time.
 * @param context the XWikiContext for the request.
 * @return a TransactionRunnable for saving the attachment content in an XWikiHibernateTransaction.
 * @throws XWikiException if thrown by AttachmentSaveTransactionRunnable()
 */
private TransactionRunnable<XWikiHibernateTransaction> getAttachmentContentSaveRunnable(final XWikiAttachment attachment, final boolean updateDocument, final XWikiContext context) throws XWikiException {
    final XWikiAttachmentContent content = attachment.getAttachment_content();
    if (content == null) {
        // If content does not exist we should not blank the attachment.
        return new TransactionRunnable<>();
    }
    // This is the permanent location where the attachment content will go.
    final File attachFile = this.fileTools.getAttachmentFileProvider(attachment.getReference()).getAttachmentContentFile();
    return new AttachmentSaveTransactionRunnable(attachment, updateDocument, context, attachFile, this.fileTools.getTempFile(attachFile), this.fileTools.getBackupFile(attachFile), this.fileTools.getLockForFile(attachFile));
}
Also used : File(java.io.File) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) TransactionRunnable(org.xwiki.store.TransactionRunnable) FileDeleteTransactionRunnable(org.xwiki.store.FileDeleteTransactionRunnable) FileSaveTransactionRunnable(org.xwiki.store.FileSaveTransactionRunnable)

Example 2 with XWikiAttachmentContent

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

the class FilesystemAttachmentVersioningStoreTest method saveArchiveTest.

@Test
public void saveArchiveTest() throws Exception {
    final XWikiAttachmentContent content = this.archive.getAttachment().getAttachment_content();
    final XWikiAttachment attach = this.archive.getAttachment();
    Assert.assertFalse(this.provider.getAttachmentVersioningMetaFile().exists());
    Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.1").exists());
    Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.2").exists());
    Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.3").exists());
    // Because the context is only used by the legacy implementation, it is safe to pass null.
    this.versionStore.saveArchive(this.archive, null, false);
    Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().exists());
    // Make sure it's not just:
    // <?xml version="1.0" encoding="UTF-8"?>
    // <attachment-list serializer="attachment-list-meta/1.0">
    // </attachment-list>
    Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().length() > 120);
    Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.1").exists());
    Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.2").exists());
    Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.3").exists());
    // Prove that the attachment and attachment content are the same after saving.
    Assert.assertSame(attach, this.archive.getAttachment());
    Assert.assertSame(content, this.archive.getAttachment().getAttachment_content());
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) Test(org.junit.Test)

Example 3 with XWikiAttachmentContent

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

the class XWikiHibernateAttachmentStore method deleteXWikiAttachment.

@Override
public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    String currentWiki = context.getWikiId();
    try {
        // Switch context wiki to attachment wiki
        String attachmentWiki = (attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference().getWikiReference().getName();
        if (attachmentWiki != null) {
            context.setWikiId(attachmentWiki);
        }
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        // Delete the three attachment entries
        try {
            session.delete(new XWikiAttachmentContent(attachment));
        } catch (Exception e) {
            this.logger.warn("Error deleting attachment content [{}] of document [{}]", attachment.getFilename(), attachment.getDoc().getDocumentReference());
        }
        AttachmentVersioningStore store = resolveAttachmentVersioningStore(attachment, context);
        store.deleteArchive(attachment, context, false);
        try {
            session.delete(attachment);
        } catch (Exception e) {
            this.logger.warn("Error deleting attachment meta data [{}] of document [{}]", attachment.getFilename(), attachment.getDoc().getDocumentReference());
        }
        try {
            if (parentUpdate) {
                List<XWikiAttachment> list = attachment.getDoc().getAttachmentList();
                for (int i = 0; i < list.size(); i++) {
                    XWikiAttachment attach = list.get(i);
                    if (attachment.getFilename().equals(attach.getFilename())) {
                        list.remove(i);
                        break;
                    }
                }
                context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, false);
            }
        } catch (Exception e) {
            this.logger.warn("Error updating document when deleting attachment [{}] of document [{}]", attachment.getFilename(), attachment.getDoc().getDocumentReference());
        }
        if (bTransaction) {
            endTransaction(context, true);
        }
    } catch (Exception e) {
        Object[] args = { attachment.getReference() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_ATTACHMENT, "Exception while deleting attachment {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
        }
        // Restore context wiki
        context.setWikiId(currentWiki);
        restoreExecutionXContext();
    }
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 4 with XWikiAttachmentContent

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

the class ImagePluginTest method testCacheOfScaledAttachment.

@Test
public void testCacheOfScaledAttachment() throws Exception {
    XWikiContext xcontext = this.oldCore.getXWikiContext();
    XWikiAttachment attachment = Mockito.mock(XWikiAttachment.class);
    Mockito.when(attachment.getMimeType(xcontext)).thenReturn("image/png");
    InputStream attachmentInputStream = new ByteArrayInputStream(testPngImageContent);
    Mockito.when(attachment.getContentInputStream(xcontext)).thenReturn(attachmentInputStream);
    Mockito.when(attachment.clone()).thenReturn(attachment);
    XWikiAttachmentContent attachmentContent = Mockito.mock(XWikiAttachmentContent.class);
    Mockito.when(attachment.getAttachment_content()).thenReturn(attachmentContent);
    Mockito.when(attachmentContent.getContentInputStream()).thenReturn(attachmentInputStream);
    OutputStream attachmentOutputStream = Mockito.mock(OutputStream.class);
    Mockito.when(attachmentContent.getContentOutputStream()).thenReturn(attachmentOutputStream);
    CacheManager cacheManager = this.oldCore.getMocker().getInstance(CacheManager.class);
    Cache<Object> imageCache = Mockito.mock(Cache.class);
    Mockito.when(cacheManager.createNewLocalCache(ArgumentMatchers.any())).thenReturn(imageCache);
    XWikiServletRequest request = Mockito.mock(XWikiServletRequest.class);
    Mockito.when(request.getParameter("width")).thenReturn("30");
    Mockito.when(request.getParameter("height")).thenReturn("30");
    xcontext.setRequest(request);
    Image image = Mockito.mock(Image.class);
    Mockito.when(image.getWidth(null)).thenReturn(400);
    Mockito.when(image.getHeight(null)).thenReturn(300);
    Mockito.when(imageProcessor.readImage(attachmentInputStream)).thenReturn(image);
    RenderedImage renderedImage = Mockito.mock(RenderedImage.class);
    Mockito.when(imageProcessor.scaleImage(image, 30, 30)).thenReturn(renderedImage);
    XWikiAttachment scaled = plugin.downloadAttachment(attachment, xcontext);
    String cacheKey = "0;null;30;30;false;-1.0";
    Mockito.when(imageCache.get(cacheKey)).thenReturn(scaled);
    // Load again, this time from cache.
    Assert.assertSame(scaled, plugin.downloadAttachment(attachment, xcontext));
    Mockito.verify(imageProcessor, Mockito.times(1)).writeImage(renderedImage, "image/png", .5F, attachmentOutputStream);
    Mockito.verify(imageCache, Mockito.times(1)).set(cacheKey, attachment);
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Image(java.awt.Image) RenderedImage(java.awt.image.RenderedImage) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) ByteArrayInputStream(java.io.ByteArrayInputStream) CacheManager(org.xwiki.cache.CacheManager) RenderedImage(java.awt.image.RenderedImage) Test(org.junit.Test)

Example 5 with XWikiAttachmentContent

use of com.xpn.xwiki.doc.XWikiAttachmentContent 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)

Aggregations

XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)10 XWikiContext (com.xpn.xwiki.XWikiContext)7 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)7 InputStream (java.io.InputStream)4 Session (org.hibernate.Session)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiException (com.xpn.xwiki.XWikiException)3 Attachment (com.xpn.xwiki.api.Attachment)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 Document (com.xpn.xwiki.api.Document)2 File (java.io.File)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 AttachmentNameChanged (com.xpn.xwiki.doc.XWikiAttachment.AttachmentNameChanged)1 XWikiAttachmentArchive (com.xpn.xwiki.doc.XWikiAttachmentArchive)1 AttachmentVersioningStore (com.xpn.xwiki.store.AttachmentVersioningStore)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1