You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageDiffHandler.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2014 Tom <tw201207@gmail.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.nio.charset.StandardCharsets;
  18. import java.util.List;
  19. import org.apache.wicket.protocol.http.WebApplication;
  20. import org.apache.wicket.protocol.http.WicketURLEncoder;
  21. import org.eclipse.jgit.diff.DiffEntry;
  22. import org.eclipse.jgit.diff.DiffEntry.Side;
  23. import org.jsoup.nodes.Element;
  24. import com.gitblit.servlet.RawServlet;
  25. import com.gitblit.utils.DiffUtils;
  26. import com.gitblit.utils.HtmlBuilder;
  27. /**
  28. * A {@link DiffUtils.BinaryDiffHandler BinaryDiffHandler} for images.
  29. *
  30. * @author Tom <tw201207@gmail.com>
  31. */
  32. public class ImageDiffHandler implements DiffUtils.BinaryDiffHandler {
  33. private final String oldCommitId;
  34. private final String newCommitId;
  35. private final String repositoryName;
  36. private final BasePage page;
  37. private final List<String> imageExtensions;
  38. private int imgDiffCount = 0;
  39. public ImageDiffHandler(final BasePage page, final String repositoryName, final String oldCommitId, final String newCommitId,
  40. final List<String> imageExtensions) {
  41. this.page = page;
  42. this.repositoryName = repositoryName;
  43. this.oldCommitId = oldCommitId;
  44. this.newCommitId = newCommitId;
  45. this.imageExtensions = imageExtensions;
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public String renderBinaryDiff(DiffEntry diffEntry) {
  50. switch (diffEntry.getChangeType()) {
  51. case MODIFY:
  52. case RENAME:
  53. case COPY:
  54. // TODO: for very small images such as icons, the slider doesn't really help. Two possible
  55. // approaches: either upscale them for display (may show blurry upscaled images), or show
  56. // them side by side (may still be too small to really make out the differences).
  57. String oldUrl = getImageUrl(diffEntry, Side.OLD);
  58. String newUrl = getImageUrl(diffEntry, Side.NEW);
  59. if (oldUrl != null && newUrl != null) {
  60. imgDiffCount++;
  61. String id = "imgdiff" + imgDiffCount;
  62. HtmlBuilder builder = new HtmlBuilder("div");
  63. Element wrapper = builder.root().attr("class", "imgdiff-container").attr("id", "imgdiff-" + id);
  64. Element container = wrapper.appendElement("div").attr("class", "imgdiff-ovr-slider").appendElement("div").attr("class", "imgdiff");
  65. Element old = container.appendElement("div").attr("class", "imgdiff-left");
  66. // style='max-width:640px;' is necessary for ensuring that the browser limits large images
  67. // to some reasonable width, and to override the "img { max-width: 100%; }" from bootstrap.css,
  68. // which would scale the left image to the width of its resizeable container, which isn't what
  69. // we want here. Note that the max-width must be defined directly as inline style on the element,
  70. // otherwise browsers ignore it if the image is larger, and we end up with an image display that
  71. // is too wide.
  72. // XXX: Maybe add a max-height, too, to limit portrait-oriented images to some reasonable height?
  73. // (Like a 300x10000px image...)
  74. old.appendElement("img").attr("class", "imgdiff-old").attr("id", id).attr("style", "max-width:640px;").attr("src", oldUrl);
  75. container.appendElement("img").attr("class", "imgdiff").attr("style", "max-width:640px;").attr("src", newUrl);
  76. wrapper.appendElement("br");
  77. Element controls = wrapper.appendElement("div");
  78. // Opacity slider
  79. controls.appendElement("div").attr("class", "imgdiff-opa-container").appendElement("a").attr("class", "imgdiff-opa-slider")
  80. .attr("href", "#").attr("title", page.getString("gb.opacityAdjust"));
  81. // Blink comparator: find Pluto!
  82. controls.appendElement("a").attr("class", "imgdiff-link imgdiff-blink").attr("href", "#")
  83. .attr("title", page.getString("gb.blinkComparator"))
  84. .appendElement("img").attr("src", getStaticResourceUrl("blink32.png")).attr("width", "20");
  85. // Pixel subtraction, initially not displayed, will be shown by imgdiff.js depending on feature test.
  86. // (Uses CSS mix-blend-mode, which isn't supported on all browsers yet).
  87. controls.appendElement("a").attr("class", "imgdiff-link imgdiff-subtract").attr("href", "#")
  88. .attr("title", page.getString("gb.imgdiffSubtract")).attr("style", "display:none;")
  89. .appendElement("img").attr("src", getStaticResourceUrl("sub32.png")).attr("width", "20");
  90. return builder.toString();
  91. }
  92. break;
  93. case ADD:
  94. String url = getImageUrl(diffEntry, Side.NEW);
  95. if (url != null) {
  96. return new HtmlBuilder("img").root().attr("class", "diff-img").attr("src", url).toString();
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. return null;
  103. }
  104. /** Returns the number of image diffs generated so far by this {@link ImageDiffHandler}. */
  105. public int getImgDiffCount() {
  106. return imgDiffCount;
  107. }
  108. /**
  109. * Constructs a URL that will fetch the designated resource in the git repository. The returned string will
  110. * contain the URL fully URL-escaped, but note that it may still contain unescaped ampersands, so the result
  111. * must still be run through HTML escaping if it is to be used in HTML.
  112. *
  113. * @return the URL to the image, if the given {@link DiffEntry} and {@link Side} refers to an image, or {@code null} otherwise.
  114. */
  115. protected String getImageUrl(DiffEntry entry, Side side) {
  116. String path = entry.getPath(side);
  117. int i = path.lastIndexOf('.');
  118. if (i > 0) {
  119. String extension = path.substring(i + 1);
  120. for (String ext : imageExtensions) {
  121. if (ext.equalsIgnoreCase(extension)) {
  122. String commitId = Side.NEW.equals(side) ? newCommitId : oldCommitId;
  123. if (commitId != null) {
  124. return RawServlet.asLink(page.getContextUrl(), urlencode(repositoryName), commitId, urlencode(path));
  125. } else {
  126. return null;
  127. }
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * Returns a URL that will fetch the designated static resource from within GitBlit.
  135. */
  136. protected String getStaticResourceUrl(String contextRelativePath) {
  137. return WebApplication.get().getRequestCycleProcessor().getRequestCodingStrategy().rewriteStaticRelativeUrl(contextRelativePath);
  138. }
  139. /**
  140. * Encode a URL component of a {@link RawServlet} URL in the special way that the servlet expects it. Note that
  141. * the %-encoding used does not encode '&amp;' or '&lt;'. Slashes are not encoded in the result.
  142. *
  143. * @param component
  144. * to encode using %-encoding
  145. * @return the encoded component
  146. */
  147. protected String urlencode(final String component) {
  148. // RawServlet handles slashes itself. Note that only the PATH_INSTANCE fits the bill here: it encodes
  149. // spaces as %20, and we just have to correct for encoded slashes. Java's standard URLEncoder would
  150. // encode spaces as '+', and I don't know what effects that would have on other parts of GitBlit. It
  151. // would also be wrong for path components (but fine for a query part), so we'd have to correct it, too.
  152. //
  153. // Actually, this should be done in RawServlet.asLink(). As it is now, this may be incorrect if that
  154. // operation ever uses query parameters instead of paths, or if it is fixed to urlencode its path
  155. // components. But I don't want to touch that static method in RawServlet.
  156. return WicketURLEncoder.PATH_INSTANCE.encode(component, StandardCharsets.UTF_8.name()).replaceAll("%2[fF]", "/");
  157. }
  158. }