use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.
the class ViewAttachRevAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiDocument doc = context.getDoc();
String filename;
if (context.getMode() == XWikiContext.MODE_PORTLET) {
filename = request.getParameter("filename");
} else {
filename = getFileName();
}
XWikiAttachment attachment;
if (context.getWiki().hasAttachmentRecycleBin(context) && request.getParameter("rid") != null) {
int recycleId = Integer.parseInt(request.getParameter("rid"));
attachment = new XWikiAttachment(doc, filename);
attachment = context.getWiki().getAttachmentRecycleBinStore().restoreFromRecycleBin(attachment, recycleId, context, true);
} else if (request.getParameter("id") != null) {
int id = Integer.parseInt(request.getParameter("id"));
attachment = doc.getAttachmentList().get(id);
} else {
attachment = doc.getAttachment(filename);
if (attachment == null) {
context.put("message", "attachmentdoesnotexist");
return "exception";
}
}
ScriptContext scriptContext = getCurrentScriptContext();
scriptContext.setAttribute("attachment", new Attachment((Document) scriptContext.getAttribute("doc"), attachment, context), ScriptContext.ENGINE_SCOPE);
return "viewattachrev";
}
use of com.xpn.xwiki.api.Attachment 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);
}
use of com.xpn.xwiki.api.Attachment 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.api.Attachment 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.api.Attachment in project xwiki-platform by xwiki.
the class DefaultWatchListNotifier method getTemplateFactoryParameters.
private Map<String, Object> getTemplateFactoryParameters(Map<String, Object> notificationData) {
Map<String, Object> parameters = new HashMap<String, Object>();
XWikiContext context = contextProvider.get();
// Prepare email template (wiki page) context
Map<String, Object> velocityVariables = new HashMap<>();
Date previousFireTime = (Date) notificationData.get(PREVIOUS_FIRE_TIME_VARIABLE);
if (previousFireTime != null) {
velocityVariables.put(PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime);
}
// Note: The remaining bindings / variables that are context dependent will be updated for each subscriber by
// the iterator, since they are different for each subscriber.
// Add to parameters
parameters.put("velocityVariables", velocityVariables);
// Get the wiki's default language (default en).
String language = context.getWiki().getXWikiPreference("default_language", "en", context);
parameters.put("language", language);
// Add the template document's attachments to the email.
parameters.put("includeTemplateAttachments", true);
List<Attachment> attachments = (List<Attachment>) notificationData.get(TEMPLATE_ATTACHMENTS);
if (attachments != null) {
parameters.put(TEMPLATE_ATTACHMENTS, attachments);
}
// Set the mail's type to "watchlist".
parameters.put("type", "watchlist");
// Get from email address from the configuration (default : mailer@xwiki.localdomain.com)
String from = getFromAddress();
parameters.put("from", from);
return parameters;
}
Aggregations