| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | // determine repository and resource from url |
| | | String repository = ""; |
| | | String repository = path; |
| | | Repository r = null; |
| | | int offset = 0; |
| | | while (r == null) { |
| | | int slash = path.indexOf('/', offset); |
| | | if (slash == -1) { |
| | | repository = path; |
| | | } else { |
| | | repository = path.substring(0, slash); |
| | | } |
| | | offset = ( slash + 1 ); |
| | | int terminator = repository.length(); |
| | | do { |
| | | repository = repository.substring(0, terminator); |
| | | r = repositoryManager.getRepository(repository, false); |
| | | if (repository.equals(path)) { |
| | | // either only repository in url or no repository found |
| | | break; |
| | | } |
| | | } |
| | | terminator = repository.lastIndexOf('/'); |
| | | } while (r == null && terminator > -1 ); |
| | | |
| | | ServletContext context = request.getSession().getServletContext(); |
| | | |
| | |
| | | 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 { |
| | | String contentType; |
| | | |
| | | List<String> exts = runtimeManager.getSettings().getStrings(Keys.web.prettyPrintExtensions); |
| | | String ext = StringUtils.getFileExtension(file).toLowerCase(); |
| | | 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); |
| | | // We can't parse out an extension for classic "dotfiles", so make a general assumption that |
| | | // they're text files to allow presenting them in browser instead of only for download. |
| | | // |
| | | // However, that only holds for files with no other extension included, for files that happen |
| | | // to start with a dot but also include an extension, process the extension normally. |
| | | // This logic covers .gitattributes, .gitignore, .zshrc, etc., but does not cover .mongorc.js, .zshrc.bak |
| | | boolean isExtensionlessDotfile = file.charAt(0) == '.' && (file.length() == 1 || file.indexOf('.', 1) < 0); |
| | | String contentType = isExtensionlessDotfile ? "text/plain" : 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) { |
| | |
| | | served = true; |
| | | } |
| | | } finally { |
| | | tw.release(); |
| | | tw.close(); |
| | | rw.dispose(); |
| | | } |
| | | |