Search in sources :

Example 41 with XWikiAttachment

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

the class ZipExplorerTest method testDownloadAttachment.

public void testDownloadAttachment() throws Exception {
    String zipFileContent = "File.txt content";
    Mock mockDocument = mock(XWikiDocument.class);
    mockDocument.stubs().method("setContentDirty");
    mockDocument.stubs().method("setMetaDataDirty");
    mockDocument.stubs().method("getDocumentReference").will(returnValue(new DocumentReference("wiki", "Main", "Document")));
    XWikiAttachment originalAttachment = createAttachment("zipfile.zip", createZipFile(zipFileContent), (XWikiDocument) mockDocument.proxy());
    XWikiContext context = createXWikiContext("http://server/xwiki/bin/download/Main/Document/zipfile.zip/Directory/File.txt");
    XWikiAttachment newAttachment = this.plugin.downloadAttachment(originalAttachment, context);
    Assert.assertEquals("Directory/File.txt", newAttachment.getFilename());
    Assert.assertEquals(zipFileContent.length(), newAttachment.getLongSize());
    Assert.assertEquals(zipFileContent.length(), newAttachment.getContentSize(context));
    Assert.assertEquals(zipFileContent, new String(newAttachment.getContent(context)));
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 42 with XWikiAttachment

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

the class ImagePlugin method downloadImage.

/**
 * Transforms the given image (i.e. shrinks the image and changes its quality) before it is downloaded.
 *
 * @param image the image to be downloaded
 * @param width the desired image width; this value is taken into account only if it is greater than zero and less
 *            than the current image width
 * @param height the desired image height; this value is taken into account only if it is greater than zero and less
 *            than the current image height
 * @param quality the desired compression quality
 * @param context the XWiki context
 * @return the transformed image
 * @throws Exception if transforming the image fails
 */
private XWikiAttachment downloadImage(XWikiAttachment image, int width, int height, float quality, XWikiContext context) throws Exception {
    initCache(context);
    boolean keepAspectRatio = Boolean.valueOf(context.getRequest().getParameter("keepAspectRatio"));
    XWikiAttachment thumbnail = (this.imageCache == null) ? shrinkImage(image, width, height, keepAspectRatio, quality, context) : downloadImageFromCache(image, width, height, keepAspectRatio, quality, context);
    // If the image has been transformed, update the file name extension to match the image format.
    String fileName = thumbnail.getFilename();
    String extension = StringUtils.lowerCase(StringUtils.substringAfterLast(fileName, String.valueOf('.')));
    if (thumbnail != image && !Arrays.asList("jpeg", "jpg", "png").contains(extension)) {
        // The scaled image is PNG, so correct the extension in order to output the correct MIME type.
        thumbnail.setFilename(StringUtils.substringBeforeLast(fileName, ".") + ".png");
    }
    return thumbnail;
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 43 with XWikiAttachment

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

the class ImagePlugin method downloadImageFromCache.

/**
 * Downloads the given image from cache.
 *
 * @param image the image to be downloaded
 * @param width the desired image width; this value is taken into account only if it is greater than zero and less
 *            than the current image width
 * @param height the desired image height; this value is taken into account only if it is greater than zero and less
 *            than the current image height
 * @param keepAspectRatio {@code true} to preserve aspect ratio when resizing the image, {@code false} otherwise
 * @param quality the desired compression quality
 * @param context the XWiki context
 * @return the transformed image
 * @throws Exception if transforming the image fails
 */
private XWikiAttachment downloadImageFromCache(XWikiAttachment image, int width, int height, boolean keepAspectRatio, float quality, XWikiContext context) throws Exception {
    String key = String.format("%s;%s;%s;%s;%s;%s", image.getId(), image.getVersion(), width, height, keepAspectRatio, quality);
    XWikiAttachment thumbnail = this.imageCache.get(key);
    if (thumbnail == null) {
        thumbnail = shrinkImage(image, width, height, keepAspectRatio, quality, context);
        this.imageCache.set(key, thumbnail);
    }
    return thumbnail;
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 44 with XWikiAttachment

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

the class ImagePluginTest method testDownloadAttachmentWithUnsupportedFileType.

@Test
public void testDownloadAttachmentWithUnsupportedFileType() {
    XWikiAttachment attachment = Mockito.mock(XWikiAttachment.class);
    Mockito.when(attachment.getMimeType()).thenReturn("image/notsupported");
    Assert.assertSame(attachment, plugin.downloadAttachment(attachment, new XWikiContext()));
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Example 45 with XWikiAttachment

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

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