]> source.dussan.org Git - gitblit.git/commitdiff
Real raw page for text, images, and binaries (issue 6, issue 114)
authorJames Moger <james.moger@gitblit.com>
Wed, 25 Jul 2012 21:37:39 +0000 (17:37 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 25 Jul 2012 21:37:39 +0000 (17:37 -0400)
docs/04_releases.mkd
src/com/gitblit/wicket/pages/BlobPage.html
src/com/gitblit/wicket/pages/BlobPage.java
src/com/gitblit/wicket/pages/RawPage.html [deleted file]
src/com/gitblit/wicket/pages/RawPage.java

index 89671c72cc1aaae196f63a6c0dad2c0ce372d01e..f0cacbfa3a09e6bfcb68d19ea7ed639409476855 100644 (file)
@@ -11,11 +11,14 @@ If you are updating from an 0.9.x release AND you have indexed branches with the
 \r
 #### fixes\r
 \r
+- Output real RAW content, not simulated RAW content (issue 114)\r
 - Fixed Lucene charset encoding bug when reindexing a repository (issue 112)\r
 - Fixed null pointer in LdapUserSerivce if account has a null email address (issue 110)\r
 \r
 #### changes\r
 \r
+- Blob page now supports displaying images (issue 6)\r
+- Non-image binary files can now be downloaded using the RAW link\r
 - Updated Polish translation\r
 \r
 **1.0.0** *released 2012-07-14*\r
index 3c8514196c863eb94d08a48be898b1bc6216b78e..80f061fdc5fcac68bcdd93d196abdf186cfae852 100644 (file)
@@ -29,6 +29,9 @@
                \r
                <!--  blob content -->\r
                <pre style="border:0px;" wicket:id="blobText">[blob content]</pre>\r
+\r
+               <!--  blob image -->\r
+               <img wicket:id="blobImage" style="padding-top:5px;"></img>\r
        \r
 </body>\r
 </wicket:extend>\r
index fb5a962be740c52574aed76fc2f67dc5f7796d46..e2683ae14f4a3b7511126bde1f67ccb71e2b8029 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Map;
 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
@@ -30,6 +31,7 @@ import com.gitblit.GitBlit;
 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
@@ -108,34 +110,41 @@ public class BlobPage extends RepositoryPage {
                                        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
diff --git a/src/com/gitblit/wicket/pages/RawPage.html b/src/com/gitblit/wicket/pages/RawPage.html
deleted file mode 100644 (file)
index 30dd316..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\r
-<html xmlns="http://www.w3.org/1999/xhtml"  \r
-      xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd"  \r
-      xml:lang="en"  \r
-      lang="en">\r
-      \r
-<body>\r
-\r
-               <!-- raw content -->\r
-               <pre style="border:0px;" wicket:id="rawText">[raw content]</pre>\r
-       \r
-</body>\r
-</html>
\ No newline at end of file
index 00cc5bf7d89f39359d3dd8ab3b1d7567ddb5d170..7f6ed139074e42c22fec4871e398026c1c50f7e9 100644 (file)
  */\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
@@ -33,78 +37,125 @@ import com.gitblit.wicket.WicketUtils;
 \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