@Test public void createAttachmentURLWithWhitespacesInSpaceAndPageNames() throws Exception { XWikiDocument doc = new XWikiDocument( new DocumentReference("Wiki", Arrays.asList(" Space1 ", "Space2"), "Pa ge")); this.exportContext.setDocParentLevels(4); XWikiAttachment attachment = new XWikiAttachment(doc, "img .jpg"); attachment.setContent(new ByteArrayInputStream("test".getBytes())); doc.getAttachmentList().add(attachment); this.oldcoreRule.getSpyXWiki().saveDocument(doc, this.oldcoreRule.getXWikiContext()); URL url = this.urlFactory.createAttachmentURL("img .jpg", " Space1 .Space2", "Pa ge", "view", "", "Wiki", this.oldcoreRule.getXWikiContext()); assertEquals(new URL("file: File generatedFile = new File(this.tmpDir, "attachment/Wiki/+Space1+/Space2/Pa+ge/img+.jpg"); assertTrue(generatedFile.exists()); assertEquals("img+.jpg", generatedFile.getName()); }
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(); try (InputStream stream = attachment.getContentInputStream(context)) { FileUtils.copyInputStreamToFile(stream, file); } } StringBuilder newPath = new StringBuilder("file: adjustCSSPath(newPath); String relativeURLPath = new File("").toURI().relativize(new File(path).toURI()).toString(); newPath.append(relativeURLPath); return new URL(newPath.toString()); }
ExportURLFactory extends XWikiServletURLFactory { 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(); try (InputStream stream = attachment.getContentInputStream(context)) { FileUtils.copyInputStreamToFile(stream, file); } } StringBuilder newPath = new StringBuilder("file: adjustCSSPath(newPath); String relativeURLPath = new File("").toURI().relativize(new File(path).toURI()).toString(); newPath.append(relativeURLPath); return new URL(newPath.toString()); } }
ExportURLFactory extends XWikiServletURLFactory { 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(); try (InputStream stream = attachment.getContentInputStream(context)) { FileUtils.copyInputStreamToFile(stream, file); } } StringBuilder newPath = new StringBuilder("file: adjustCSSPath(newPath); String relativeURLPath = new File("").toURI().relativize(new File(path).toURI()).toString(); newPath.append(relativeURLPath); return new URL(newPath.toString()); } ExportURLFactory(); }
ExportURLFactory extends XWikiServletURLFactory { 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(); try (InputStream stream = attachment.getContentInputStream(context)) { FileUtils.copyInputStreamToFile(stream, file); } } StringBuilder newPath = new StringBuilder("file: adjustCSSPath(newPath); String relativeURLPath = new File("").toURI().relativize(new File(path).toURI()).toString(); newPath.append(relativeURLPath); return new URL(newPath.toString()); } ExportURLFactory(); FilesystemExportContext getFilesystemExportContext(); @Deprecated Collection<String> getNeededSkins(); @Deprecated Collection<String> getExportedSkinFiles(); void init(Collection<DocumentReference> exportedPages, File exportDir, FilesystemExportContext exportContext,
        XWikiContext context); @Deprecated void init(Collection<String> exportedPages, File exportDir, XWikiContext context); @Override URL createSkinURL(String filename, String skin, XWikiContext context,
        Map<String, java.lang.Object> queryParameters); @Override URL createSkinURL(String filename, String skin, XWikiContext context); @Override URL createSkinURL(String filename, String spaces, String name, XWikiContext context); URL createSkinURL(String filename, String spaces, String name, XWikiContext context,
        boolean skipSkinDirectory); @Override URL createSkinURL(String fileName, String spaces, String name, String wikiId, XWikiContext context); @Override URL createSkinURL(String filename, String spaces, String name, String xwikidb, XWikiContext context,
        Map<String, Object> queryParameters); URL createSkinURL(String fileName, String spaces, String name, String wikiId, XWikiContext context,
        boolean skipSkinDirectory); @Override URL createResourceURL(String filename, boolean forceSkinAction, XWikiContext context,
        Map<String, Object> queryParameters); @Override URL createResourceURL(String filename, boolean forceSkinAction, XWikiContext context); @Override URL createURL(String spaces, String name, String action, String querystring, String anchor, String xwikidb,
        XWikiContext context); @Override URL createAttachmentURL(String filename, String spaces, String name, String action, String querystring,
        String xwikidb, XWikiContext context); @Override URL createAttachmentRevisionURL(String filename, String spaces, String name, String revision, String xwikidb,
        XWikiContext context); @Override String getURL(URL url, XWikiContext context); }
ExportURLFactory extends XWikiServletURLFactory { 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(); try (InputStream stream = attachment.getContentInputStream(context)) { FileUtils.copyInputStreamToFile(stream, file); } } StringBuilder newPath = new StringBuilder("file: adjustCSSPath(newPath); String relativeURLPath = new File("").toURI().relativize(new File(path).toURI()).toString(); newPath.append(relativeURLPath); return new URL(newPath.toString()); } ExportURLFactory(); FilesystemExportContext getFilesystemExportContext(); @Deprecated Collection<String> getNeededSkins(); @Deprecated Collection<String> getExportedSkinFiles(); void init(Collection<DocumentReference> exportedPages, File exportDir, FilesystemExportContext exportContext,
        XWikiContext context); @Deprecated void init(Collection<String> exportedPages, File exportDir, XWikiContext context); @Override URL createSkinURL(String filename, String skin, XWikiContext context,
        Map<String, java.lang.Object> queryParameters); @Override URL createSkinURL(String filename, String skin, XWikiContext context); @Override URL createSkinURL(String filename, String spaces, String name, XWikiContext context); URL createSkinURL(String filename, String spaces, String name, XWikiContext context,
        boolean skipSkinDirectory); @Override URL createSkinURL(String fileName, String spaces, String name, String wikiId, XWikiContext context); @Override URL createSkinURL(String filename, String spaces, String name, String xwikidb, XWikiContext context,
        Map<String, Object> queryParameters); URL createSkinURL(String fileName, String spaces, String name, String wikiId, XWikiContext context,
        boolean skipSkinDirectory); @Override URL createResourceURL(String filename, boolean forceSkinAction, XWikiContext context,
        Map<String, Object> queryParameters); @Override URL createResourceURL(String filename, boolean forceSkinAction, XWikiContext context); @Override URL createURL(String spaces, String name, String action, String querystring, String anchor, String xwikidb,
        XWikiContext context); @Override URL createAttachmentURL(String filename, String spaces, String name, String action, String querystring,
        String xwikidb, XWikiContext context); @Override URL createAttachmentRevisionURL(String filename, String spaces, String name, String revision, String xwikidb,
        XWikiContext context); @Override String getURL(URL url, XWikiContext context);  }