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