import org.apache.wicket.Component;\r
import org.apache.wicket.PageParameters;\r
import org.apache.wicket.markup.html.basic.Label;\r
+import org.apache.wicket.markup.html.image.Image;\r
import org.apache.wicket.markup.html.link.BookmarkablePageLink;\r
import org.eclipse.jgit.lib.Constants;\r
import org.eclipse.jgit.lib.Repository;\r
import com.gitblit.Keys;\r
import com.gitblit.utils.JGitUtils;\r
import com.gitblit.utils.StringUtils;\r
+import com.gitblit.wicket.ExternalImage;\r
import com.gitblit.wicket.WicketUtils;\r
import com.gitblit.wicket.panels.CommitHeaderPanel;\r
import com.gitblit.wicket.panels.PathBreadcrumbsPanel;\r
type = map.get(extension);\r
}\r
Component c = null;\r
+ Component i = null;\r
switch (type) {\r
case 1:\r
// PrettyPrint blob text\r
c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),\r
blobPath, encodings));\r
WicketUtils.setCssClass(c, "prettyprint linenums");\r
+ i = new Image("blobImage").setVisible(false);\r
break;\r
case 2:\r
- // TODO image blobs\r
- c = new Label("blobText", "Image File");\r
+ // image blobs\r
+ c = new Label("blobText").setVisible(false);\r
+ i = new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString());\r
break;\r
case 3:\r
- // TODO binary blobs\r
+ // binary blobs\r
c = new Label("blobText", "Binary File");\r
+ i = new Image("blobImage").setVisible(false);\r
break;\r
default:\r
// plain text\r
c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),\r
blobPath, encodings));\r
WicketUtils.setCssClass(c, "plainprint");\r
+ i = new Image("blobImage").setVisible(false);\r
}\r
add(c);\r
+ add(i);\r
} else {\r
// plain text\r
Label blobLabel = new Label("blobText", JGitUtils.getStringContent(r,\r
commit.getTree(), blobPath, encodings));\r
WicketUtils.setCssClass(blobLabel, "plainprint");\r
add(blobLabel);\r
+ add(new Image("blobImage").setVisible(false));\r
}\r
}\r
}\r
*/\r
package com.gitblit.wicket.pages;\r
\r
+import java.io.IOException;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
-import org.apache.wicket.Component;\r
+import org.apache.wicket.IRequestTarget;\r
import org.apache.wicket.PageParameters;\r
+import org.apache.wicket.RequestCycle;\r
import org.apache.wicket.markup.html.WebPage;\r
-import org.apache.wicket.markup.html.basic.Label;\r
+import org.apache.wicket.protocol.http.WebResponse;\r
import org.eclipse.jgit.lib.Repository;\r
import org.eclipse.jgit.revwalk.RevCommit;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
\r
import com.gitblit.GitBlit;\r
import com.gitblit.Keys;\r
\r
public class RawPage extends WebPage {\r
\r
- public RawPage(PageParameters params) {\r
+ private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());\r
+\r
+ public RawPage(final PageParameters params) {\r
super(params);\r
\r
if (!params.containsKey("r")) {\r
error(getString("gb.repositoryNotSpecified"));\r
redirectToInterceptPage(new RepositoriesPage());\r
}\r
- final String repositoryName = WicketUtils.getRepositoryName(params);\r
- final String objectId = WicketUtils.getObject(params);\r
- final String blobPath = WicketUtils.getPath(params);\r
- String [] encodings = GitBlit.getEncodings();\r
- \r
- Repository r = GitBlit.self().getRepository(repositoryName);\r
- if (r == null) {\r
- error(getString("gb.canNotLoadRepository") + " " + repositoryName);\r
- redirectToInterceptPage(new RepositoriesPage());\r
- return;\r
- }\r
\r
- if (StringUtils.isEmpty(blobPath)) {\r
- // objectid referenced raw view\r
- Label blobLabel = new Label("rawText", JGitUtils.getStringContent(r, objectId, encodings));\r
- WicketUtils.setCssClass(blobLabel, "plainprint");\r
- add(blobLabel);\r
- } else {\r
- // standard raw blob view\r
- RevCommit commit = JGitUtils.getCommit(r, objectId);\r
-\r
- String extension = null;\r
- if (blobPath.lastIndexOf('.') > -1) {\r
- extension = blobPath.substring(blobPath.lastIndexOf('.') + 1);\r
+ getRequestCycle().setRequestTarget(new IRequestTarget() {\r
+ @Override\r
+ public void detach(RequestCycle requestCycle) {\r
}\r
\r
- // Map the extensions to types\r
- Map<String, Integer> map = new HashMap<String, Integer>();\r
- for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {\r
- map.put(ext.toLowerCase(), 2);\r
- }\r
- for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {\r
- map.put(ext.toLowerCase(), 3);\r
- }\r
+ @Override\r
+ public void respond(RequestCycle requestCycle) {\r
+ WebResponse response = (WebResponse) requestCycle.getResponse();\r
+\r
+ final String repositoryName = WicketUtils.getRepositoryName(params);\r
+ final String objectId = WicketUtils.getObject(params);\r
+ final String blobPath = WicketUtils.getPath(params);\r
+ String[] encodings = GitBlit.getEncodings();\r
\r
- if (extension != null) {\r
- int type = 0;\r
- if (map.containsKey(extension)) {\r
- type = map.get(extension);\r
+ Repository r = GitBlit.self().getRepository(repositoryName);\r
+ if (r == null) {\r
+ error(getString("gb.canNotLoadRepository") + " " + repositoryName);\r
+ redirectToInterceptPage(new RepositoriesPage());\r
+ return;\r
}\r
- Component c = null;\r
- switch (type) {\r
- case 2:\r
- // TODO image blobs\r
- c = new Label("rawText", "Image File");\r
- break;\r
- case 3:\r
- // TODO binary blobs\r
- c = new Label("rawText", "Binary File");\r
- break;\r
- default:\r
- // plain text\r
- c = new Label("rawText", JGitUtils.getStringContent(r, commit.getTree(),\r
- blobPath, encodings));\r
- WicketUtils.setCssClass(c, "plainprint");\r
+\r
+ if (StringUtils.isEmpty(blobPath)) {\r
+ // objectid referenced raw view\r
+ byte [] binary = JGitUtils.getByteContent(r, objectId);\r
+ response.setContentType("application/octet-stream");\r
+ response.setContentLength(binary.length);\r
+ try {\r
+ response.getOutputStream().write(binary);\r
+ } catch (Exception e) {\r
+ logger.error("Failed to write binary response", e);\r
+ }\r
+ } else {\r
+ // standard raw blob view\r
+ RevCommit commit = JGitUtils.getCommit(r, objectId);\r
+\r
+ String filename = blobPath;\r
+ if (blobPath.indexOf('/') > -1) {\r
+ filename = blobPath.substring(blobPath.lastIndexOf('/') + 1);\r
+ }\r
+\r
+ String extension = null;\r
+ if (blobPath.lastIndexOf('.') > -1) {\r
+ extension = blobPath.substring(blobPath.lastIndexOf('.') + 1);\r
+ }\r
+\r
+ // Map the extensions to types\r
+ Map<String, Integer> map = new HashMap<String, Integer>();\r
+ for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {\r
+ map.put(ext.toLowerCase(), 2);\r
+ }\r
+ for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {\r
+ map.put(ext.toLowerCase(), 3);\r
+ }\r
+\r
+ if (extension != null) {\r
+ int type = 0;\r
+ if (map.containsKey(extension)) {\r
+ type = map.get(extension);\r
+ }\r
+ switch (type) {\r
+ case 2:\r
+ // image blobs\r
+ byte[] image = JGitUtils.getByteContent(r, commit.getTree(), blobPath);\r
+ response.setContentType("image/" + extension.toLowerCase());\r
+ response.setContentLength(image.length);\r
+ try {\r
+ response.getOutputStream().write(image);\r
+ } catch (IOException e) {\r
+ logger.error("Failed to write image response", e);\r
+ }\r
+ break;\r
+ case 3:\r
+ // binary blobs (download)\r
+ byte[] binary = JGitUtils.getByteContent(r, commit.getTree(), blobPath);\r
+ response.setContentLength(binary.length);\r
+ response.setContentType("application/octet-stream");\r
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");\r
+ try {\r
+ response.getOutputStream().write(binary);\r
+ } catch (IOException e) {\r
+ logger.error("Failed to write binary response", e);\r
+ }\r
+ break;\r
+ default:\r
+ // plain text\r
+ String content = JGitUtils.getStringContent(r, commit.getTree(),\r
+ blobPath, encodings);\r
+ response.setContentType("text/plain; charset=UTF-8");\r
+ try {\r
+ response.getOutputStream().write(content.getBytes("UTF-8"));\r
+ } catch (Exception e) {\r
+ logger.error("Failed to write text response", e);\r
+ }\r
+ }\r
+\r
+ } else {\r
+ // plain text\r
+ String content = JGitUtils.getStringContent(r, commit.getTree(), blobPath,\r
+ encodings);\r
+ response.setContentType("text/plain; charset=UTF-8");\r
+ try {\r
+ response.getOutputStream().write(content.getBytes("UTF-8"));\r
+ } catch (Exception e) {\r
+ logger.error("Failed to write text response", e);\r
+ }\r
+ }\r
}\r
- add(c);\r
- } else {\r
- // plain text\r
- Label blobLabel = new Label("rawText", JGitUtils.getStringContent(r,\r
- commit.getTree(), blobPath, encodings));\r
- WicketUtils.setCssClass(blobLabel, "plainprint");\r
- add(blobLabel);\r
+ r.close();\r
}\r
- }\r
- r.close();\r
+ });\r
}\r
}\r