use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class DocumentTreeElement method getAttachmentNodeId.
private String getAttachmentNodeId(String... path) {
if (path.length > 2) {
List<String> pathElements = Arrays.asList(path);
List<String> spaces = pathElements.subList(0, path.length - 2);
String document = path[path.length - 2];
String fileName = path[path.length - 1];
return getNodeId(new AttachmentReference(fileName, new DocumentReference("xwiki", spaces, document)));
} else {
throw new IllegalArgumentException("Incomplete path: it should have at least 3 elements (space/page/file)");
}
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class ExportURLFactory method createAttachmentURL.
/**
* Generate an url targeting attachment in provided wiki page.
*
* @param filename the name of the attachment.
* @param spaces a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If
* a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
* @param name the name of the page containing the attachment.
* @param xwikidb the wiki of the page containing the attachment.
* @param context the XWiki context.
* @return the generated url.
* @throws XWikiException error when retrieving document attachment.
* @throws IOException error when retrieving document attachment.
* @throws URISyntaxException when retrieving document attachment.
*/
private URL createAttachmentURL(String filename, String spaces, String name, String xwikidb, XWikiContext context) throws XWikiException, IOException, URISyntaxException {
String db = (xwikidb == null ? context.getWikiId() : xwikidb);
DocumentReference documentReference = new DocumentReference(db, this.legacySpaceResolver.resolve(spaces), name);
String serializedReference = this.fsPathEntityReferenceSerializer.serialize(new AttachmentReference(filename, documentReference));
String path = "attachment/" + serializedReference;
File file = new File(getFilesystemExportContext().getExportDir(), path);
if (!file.exists()) {
XWikiDocument doc = context.getWiki().getDocument(documentReference, context);
XWikiAttachment attachment = doc.getAttachment(filename);
file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
IOUtils.copy(attachment.getContentInputStream(context), fos);
fos.close();
}
StringBuilder newPath = new StringBuilder("file://");
// Adjust path for links inside CSS files (since they need to be relative to the CSS file they're in).
adjustCSSPath(newPath);
// Compute a valid relative URL from a FS path.
String relativeURLPath = new File("").toURI().relativize(new File(path).toURI()).toString();
newPath.append(relativeURLPath);
return new URL(newPath.toString());
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class GroupsClassPropertyValuesProviderTest method getValuesLocal.
@Test
public void getValuesLocal() throws Exception {
when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName())).thenReturn(UserScope.LOCAL_ONLY);
DocumentReference devsReference = new DocumentReference("wiki", "Groups", "Devs");
XWikiDocument devsProfile = mock(XWikiDocument.class, "devs");
when(this.xcontext.getWiki().getDocument(devsReference, this.xcontext)).thenReturn(devsProfile);
when(devsProfile.getRenderedTitle(Syntax.PLAIN_1_0, this.xcontext)).thenReturn("Developers");
DocumentReference adminsReference = new DocumentReference("wiki", "Groups", "Admins");
XWikiDocument adminsProfile = mock(XWikiDocument.class, "admins");
XWikiAttachment notAnImageAttachment = mock(XWikiAttachment.class, "noAnImage");
XWikiAttachment imageAttachment = mock(XWikiAttachment.class, "image");
AttachmentReference imageAttachmentReference = new AttachmentReference("admins.png", adminsReference);
when(this.xcontext.getWiki().getDocument(adminsReference, this.xcontext)).thenReturn(adminsProfile);
when(adminsProfile.getRenderedTitle(Syntax.PLAIN_1_0, this.xcontext)).thenReturn("Administrators");
when(adminsProfile.getAttachmentList()).thenReturn(Arrays.asList(notAnImageAttachment, imageAttachment));
when(imageAttachment.isImage(this.xcontext)).thenReturn(true);
when(imageAttachment.getReference()).thenReturn(imageAttachmentReference);
when(this.xcontext.getWiki().getURL(imageAttachmentReference, "download", "width=30&height=30&keepAspectRatio=true", null, this.xcontext)).thenReturn("url/to/admins/image");
when(this.allowedValuesQuery.execute()).thenReturn(Arrays.asList(devsReference, adminsReference));
PropertyValues values = this.mocker.getComponentUnderTest().getValues(this.propertyReference, 5, "foo");
assertEquals(2, values.getPropertyValues().size());
assertEquals("Developers", values.getPropertyValues().get(0).getMetaData().get("label"));
assertEquals("url/to/noavatar.png", values.getPropertyValues().get(0).getMetaData().get("icon"));
assertEquals("Administrators", values.getPropertyValues().get(1).getMetaData().get("label"));
assertEquals("url/to/admins/image", values.getPropertyValues().get(1).getMetaData().get("icon"));
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class DefaultDocumentAccessBridge method getAttachmentReferences.
@Override
public List<AttachmentReference> getAttachmentReferences(DocumentReference documentReference) throws Exception {
XWikiContext xcontext = getContext();
List<XWikiAttachment> attachments = xcontext.getWiki().getDocument(documentReference, xcontext).getAttachmentList();
List<AttachmentReference> attachmentReferences = new ArrayList<AttachmentReference>(attachments.size());
for (XWikiAttachment attachment : attachments) {
attachmentReferences.add(attachment.getReference());
}
return attachmentReferences;
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class DownloadActionTest method setRequestExpectations.
private void setRequestExpectations(String uri, String id, String forceDownload, String range, long modifiedSince, String attachmentName) {
ResourceReference rr = new EntityResourceReference(new AttachmentReference(attachmentName, this.documentReference), EntityResourceAction.VIEW);
when(this.request.getRequestURI()).thenReturn(uri);
when(this.request.getParameter("id")).thenReturn(id);
when(this.request.getDateHeader("If-Modified-Since")).thenReturn(modifiedSince);
when(this.request.getParameter("force-download")).thenReturn(forceDownload);
when(this.request.getHeader("Range")).thenReturn(range);
when(this.resourceReferenceManager.getResourceReference()).thenReturn(rr);
}
Aggregations