use of com.xpn.xwiki.doc.XWikiAttachmentContent 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);
}
}
use of com.xpn.xwiki.doc.XWikiAttachmentContent in project xwiki-platform by xwiki.
the class DefaultUserAvatarAttachmentExtractor method getUserAvatar.
@Override
public Attachment getUserAvatar(DocumentReference userReference, int width, int height, String fileName) {
// FIXME: Unfortunately, the ImagePlugin is too much request-oriented and not generic enough to be reused
// without rewriting. In the end, that might be the right way to go and rewriting it might be inevitable.
Attachment result = null;
InputStream sourceImageInputStream = null;
try {
XWikiContext context = xwikiContextProvider.get();
XWiki wiki = context.getWiki();
XWikiAttachment realAvatarAttachment;
XWikiAttachment fakeAvatarAttachment = null;
// Use a second variable to be able to reassign it on the else branch below.
DocumentReference actualUserReference = userReference;
if (actualUserReference != null) {
// Registered user.
XWikiDocument userProfileDocument = wiki.getDocument(userReference, context);
DocumentReference usersClassReference = wiki.getUserClass(context).getDocumentReference();
String avatarFileName = userProfileDocument.getStringValue(usersClassReference, "avatar");
realAvatarAttachment = userProfileDocument.getAttachment(avatarFileName);
if (realAvatarAttachment != null && realAvatarAttachment.isImage(context)) {
// Valid avatar, use the real attachment (and image), but make sure to use a clone as to not impact
// the
// real document.
fakeAvatarAttachment = (XWikiAttachment) realAvatarAttachment.clone();
sourceImageInputStream = realAvatarAttachment.getContentInputStream(context);
result = new Attachment(new Document(userProfileDocument, context), fakeAvatarAttachment, context);
} else {
// No or invalid avatar, treat the user reference as it did not exist (guest user) so it can be
// handled below for both cases.
actualUserReference = null;
}
}
if (actualUserReference == null) {
// Guest user.
// No avatar. Return a fake attachment with the "noavatar.png" standard image.
fakeAvatarAttachment = new XWikiAttachment();
sourceImageInputStream = environment.getResourceAsStream("/resources/icons/xwiki/noavatar.png");
result = new Attachment(null, fakeAvatarAttachment, context);
}
// In both cases, set an empty attachment content that will be filled with the resized image. This way we
// also avoid a request to the DB for the attachment content, since it will already be available.
fakeAvatarAttachment.setAttachment_content(new XWikiAttachmentContent(fakeAvatarAttachment));
// Resize the image and write it to the fake attachment.
int resizedWidth = 50;
int resizedHeight = 50;
resizeImageToAttachment(sourceImageInputStream, resizedWidth, resizedHeight, fakeAvatarAttachment);
// Set a fixed name for the user avatar file so that it is easy to work with in a template, for example.
fakeAvatarAttachment.setFilename(fileName);
} catch (Exception e) {
logger.error("Failed to retrieve the avatar for the user {}", userReference, e);
return null;
} finally {
// Close the source image input stream since we are done reading from it.
if (sourceImageInputStream != null) {
IOUtils.closeQuietly(sourceImageInputStream);
}
}
return result;
}
use of com.xpn.xwiki.doc.XWikiAttachmentContent in project xwiki-platform by xwiki.
the class XWikiHibernateAttachmentStore method loadAttachmentContent.
@Override
public void loadAttachmentContent(XWikiAttachment attachment, 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(false, context);
}
Session session = getSession(context);
XWikiAttachmentContent content = new XWikiAttachmentContent(attachment);
session.load(content, Long.valueOf(content.getId()));
// Hibernate calls setContent which causes isContentDirty to be true. This is not what we want.
content.setContentDirty(false);
attachment.setAttachment_content(content);
attachment.setContentStore(null);
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
Object[] args = { attachment.getReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT, "Exception while loading attachment {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, 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 XWikiHibernateAttachmentStore method saveAttachmentContent.
@Override
public void saveAttachmentContent(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
XWikiAttachmentContent content = attachment.getAttachment_content();
// table but not listed in the Attachment Content table! In this case don't save anything.
if (content != null) {
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);
Query query = session.createQuery("select attach.id from XWikiAttachmentContent as attach where attach.id = :id");
query.setLong("id", content.getId());
boolean exist = query.uniqueResult() != null;
AttachmentVersioningStore store = resolveAttachmentVersioningStore(attachment, context);
if (exist) {
session.update(content);
} else {
session.save(content);
}
if (attachment.getAttachment_archive() == null) {
attachment.loadArchive(context);
}
// The archive has been updated in XWikiHibernateStore.saveAttachment()
store.saveArchive(attachment.getAttachment_archive(), context, false);
if (parentUpdate) {
context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, true);
}
if (bTransaction) {
endTransaction(context, true);
}
} catch (Exception e) {
Object[] args = { attachment.getReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, "Exception while saving attachment {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
// Restore context wiki
context.setWikiId(currentWiki);
restoreExecutionXContext();
}
} else {
this.logger.warn("Failed to save the Attachment content for [{}] at [{}] since no content could be found!", attachment.getFilename(), attachment.getDoc());
}
}
use of com.xpn.xwiki.doc.XWikiAttachmentContent in project xwiki-platform by xwiki.
the class FilesystemAttachmentStoreTest method setUp.
@Before
@Override
public void setUp() throws Exception {
super.setUp();
getMockery().setImposteriser(ClassImposteriser.INSTANCE);
Utils.setComponentManager(this.getComponentManager());
this.mockContext = getMockery().mock(XWikiContext.class);
final XWiki mockXWiki = getMockery().mock(XWiki.class);
this.mockHibernate = getMockery().mock(XWikiHibernateStore.class);
final XWikiAttachmentContent mockDirtyContent = getMockery().mock(XWikiAttachmentContent.class);
this.mockAttachVersionStore = getMockery().mock(AttachmentVersioningStore.class);
this.mockArchive = getMockery().mock(XWikiAttachmentArchive.class);
this.mockHibernateSession = getMockery().mock(Session.class);
this.doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
this.mockAttachReference = new AttachmentReference("file.name", doc.getDocumentReference());
this.mockAttach = getMockery().mock(XWikiAttachment.class);
getMockery().checking(new Expectations() {
{
allowing(mockContext).getWiki();
will(returnValue(mockXWiki));
allowing(mockXWiki).getStore();
will(returnValue(mockHibernate));
allowing(mockXWiki).getHibernateStore();
will(returnValue(mockHibernate));
allowing(mockHibernate).checkHibernate(mockContext);
allowing(mockHibernate).beginTransaction(mockContext);
allowing(mockHibernate).getSession(mockContext);
will(returnValue(mockHibernateSession));
allowing(mockXWiki).getDefaultAttachmentArchiveStore();
will(returnValue(mockAttachVersionStore));
allowing(mockAttachVersionStore).saveArchive(mockArchive, mockContext, false);
allowing(mockAttach).getContentInputStream(mockContext);
will(returnValue(HELLO_STREAM));
allowing(mockAttach).setDoc(doc);
allowing(mockAttach).getDoc();
will(returnValue(doc));
allowing(mockAttach).getFilename();
will(returnValue(mockAttachReference.getName()));
allowing(mockAttach).getReference();
will(returnValue(mockAttachReference));
allowing(mockAttach).updateContentArchive(mockContext);
allowing(mockAttach).getAttachment_archive();
will(returnValue(mockArchive));
allowing(mockAttach).isArchiveStoreSet();
will(returnValue(false));
allowing(mockAttach).getArchiveStore();
will(returnValue("file"));
allowing(mockAttach).getAttachment_content();
will(returnValue(mockDirtyContent));
allowing(mockAttach).isContentStoreSet();
will(returnValue(false));
allowing(mockAttach).getContentStore();
will(returnValue("file"));
allowing(mockAttach).isContentDirty();
will(returnValue(true));
allowing(mockDirtyContent).isContentDirty();
will(returnValue(true));
allowing(mockAttach).addNameModifiedListener(with(any(AttachmentNameChanged.class)));
allowing(mockAttach).removeNameModifiedListener(with(any(AttachmentNameChanged.class)));
}
});
final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
this.storageLocation = new File(tmpDir, "test-storage-location");
this.fileTools = new FilesystemStoreTools(new FileStringEntityReferenceSerializer(), storageLocation, new DummyLockProvider());
this.attachStore = new FilesystemAttachmentStore();
FieldUtils.writeField(this.attachStore, "fileTools", this.fileTools, true);
this.storeFile = this.fileTools.getAttachmentFileProvider(this.mockAttachReference).getAttachmentContentFile();
HELLO_STREAM.reset();
}
Aggregations