From e41e8f8c3bc9f5edab1d271464364f95620ece8c Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Thu, 19 Nov 2015 17:55:38 -0500 Subject: [PATCH] Create filestore directory on startup --- src/main/java/com/gitblit/servlet/RawServlet.java | 69 +++++++++++++++++++++++----------- 1 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/gitblit/servlet/RawServlet.java b/src/main/java/com/gitblit/servlet/RawServlet.java index 7504b4b..1d2724b 100644 --- a/src/main/java/com/gitblit/servlet/RawServlet.java +++ b/src/main/java/com/gitblit/servlet/RawServlet.java @@ -24,12 +24,14 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import javax.servlet.ServletContext; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -48,7 +50,6 @@ import com.gitblit.Constants; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.PathModel; @@ -56,8 +57,8 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; - -import dagger.ObjectGraph; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Serves the content of a branch. @@ -65,20 +66,24 @@ * @author James Moger * */ -public class RawServlet extends DaggerServlet { +@Singleton +public class RawServlet extends HttpServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(RawServlet.class); - private IRuntimeManager runtimeManager; + private final IRuntimeManager runtimeManager; - private IRepositoryManager repositoryManager; + private final IRepositoryManager repositoryManager; - @Override - protected void inject(ObjectGraph dagger) { - this.runtimeManager = dagger.get(IRuntimeManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); + @Inject + public RawServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; } /** @@ -104,9 +109,8 @@ branch = Repository.shortenRefName(branch).replace('/', fsc); } - String encodedPath = path == null ? "" : path.replace(' ', '-'); - encodedPath = encodedPath.replace('/', fsc); - return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + (path == null ? "" : encodedPath))); + String encodedPath = path == null ? "" : path.replace('/', fsc); + return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath)); } protected String getBranch(String repository, HttpServletRequest request) { @@ -172,10 +176,7 @@ } else { repository = path.substring(0, slash); } - offset += slash; - if (offset == 0) { - offset++; - } + offset = ( slash + 1 ); r = repositoryManager.getRepository(repository, false); if (repository.equals(path)) { // either only repository in url or no repository found @@ -228,15 +229,32 @@ return; } + Map<String, String> quickContentTypes = new HashMap<>(); + quickContentTypes.put("html", "text/html"); + quickContentTypes.put("htm", "text/html"); + quickContentTypes.put("xml", "application/xml"); + quickContentTypes.put("json", "application/json"); List<PathModel> pathEntries = JGitUtils.getFilesInPath(r, requestedPath, commit); if (pathEntries.isEmpty()) { // requested a specific resource String file = StringUtils.getLastPathElement(requestedPath); try { - // query Tika for the content type - Tika tika = new Tika(); - String contentType = tika.detect(file); + + String ext = StringUtils.getFileExtension(file).toLowerCase(); + String contentType = quickContentTypes.get(ext); + + if (contentType == null) { + List<String> exts = runtimeManager.getSettings().getStrings(Keys.web.prettyPrintExtensions); + if (exts.contains(ext)) { + // extension is a registered text type for pretty printing + contentType = "text/plain"; + } else { + // query Tika for the content type + Tika tika = new Tika(); + contentType = tika.detect(file); + } + } if (contentType == null) { // ask the container for the content type @@ -248,7 +266,7 @@ } } - if (isTextType(contentType)) { + if (isTextType(contentType) || isTextDataType(contentType)) { // load, interpret, and serve text content as UTF-8 String [] encodings = runtimeManager.getSettings().getStrings(Keys.web.blobEncodings).toArray(new String[0]); @@ -382,6 +400,13 @@ return false; } + protected boolean isTextDataType(String contentType) { + if ("image/svg+xml".equals(contentType)) { + return true; + } + return false; + } + /** * Override all text types to be plain text. * @@ -443,7 +468,7 @@ served = true; } } finally { - tw.release(); + tw.close(); rw.dispose(); } -- Gitblit v1.9.1