Search in sources :

Example 56 with AttachmentReference

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)");
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 57 with AttachmentReference

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) FileOutputStream(java.io.FileOutputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) URL(java.net.URL)

Example 58 with AttachmentReference

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"));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 59 with AttachmentReference

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;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 60 with AttachmentReference

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);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3