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)));
}
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;
}
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;
}
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()));
}
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);
}
Aggregations