|
| 1 | +package gitbucket.plugin.pages |
| 2 | + |
| 3 | +import gitbucket.core.controller.ControllerBase |
| 4 | +import gitbucket.core.service.{ AccountService, RepositoryService } |
| 5 | +import gitbucket.core.util.ControlUtil._ |
| 6 | +import gitbucket.core.util.{ Directory, JGitUtil, ReferrerAuthenticator } |
| 7 | +import org.eclipse.jgit.api.Git |
| 8 | +import org.eclipse.jgit.lib.ObjectId |
| 9 | +import org.eclipse.jgit.revwalk.RevCommit |
| 10 | +import org.eclipse.jgit.treewalk.TreeWalk |
| 11 | + |
| 12 | +import scala.language.implicitConversions |
| 13 | + |
| 14 | +class PagesController |
| 15 | + extends ControllerBase |
| 16 | + with AccountService |
| 17 | + with RepositoryService |
| 18 | + with ReferrerAuthenticator |
| 19 | + |
| 20 | +trait PagesControllerBase { |
| 21 | + self: ControllerBase with AccountService with RepositoryService with ReferrerAuthenticator => |
| 22 | + |
| 23 | + val PAGES_BRANCH = "gh-pages" |
| 24 | + |
| 25 | + get("/:owner/:repository/pages/*")(referrersOnly { repository => |
| 26 | + val path = params("splat") |
| 27 | + using(Git.open(Directory.getRepositoryDir(repository.owner, repository.name))) { git => |
| 28 | + val objectId = Option(git.getRepository.resolve(PAGES_BRANCH)) |
| 29 | + .map(JGitUtil.getRevCommitFromId(git, _)) |
| 30 | + .flatMap { revCommit => |
| 31 | + getPathIndexObjectId(git, path, revCommit) |
| 32 | + } |
| 33 | + |
| 34 | + objectId match { |
| 35 | + case Some((path0, objId)) => |
| 36 | + // redirect [owner/repo/pages/path] -> [owner/repo/pages/path/] |
| 37 | + if (path0 != path && path0.startsWith(path) && !path.endsWith("/")) { |
| 38 | + redirect(s"/${repository.owner}/${repository.name}/pages/$path/") |
| 39 | + } else { |
| 40 | + JGitUtil.getObjectLoaderFromId(git, objId) { loader => |
| 41 | + contentType = Option(servletContext.getMimeType(path0)).getOrElse("application/octet-stream") |
| 42 | + response.setContentLength(loader.getSize.toInt) |
| 43 | + loader.copyTo(response.getOutputStream) |
| 44 | + } |
| 45 | + () |
| 46 | + } |
| 47 | + case None => |
| 48 | + NotFound() |
| 49 | + } |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + get("/:owner/:repository/pages")(referrersOnly { repository => |
| 54 | + redirect(s"/${repository.owner}/${repository.name}/pages/") |
| 55 | + }) |
| 56 | + |
| 57 | + def getPathIndexObjectId(git: Git, path: String, revCommit: RevCommit) = { |
| 58 | + getPathObjectId0(git, path, revCommit) |
| 59 | + .orElse(getPathObjectId0(git, appendPath(path, "index.html"), revCommit)) |
| 60 | + .orElse(getPathObjectId0(git, appendPath(path, "index.htm"), revCommit)) |
| 61 | + } |
| 62 | + |
| 63 | + def getPathObjectId0(git: Git, path: String, revCommit: RevCommit) = { |
| 64 | + getPathObjectId(git, path, revCommit).map(path -> _) |
| 65 | + } |
| 66 | + |
| 67 | + def appendPath(base: String, suffix: String): String = { |
| 68 | + if (base == "") suffix |
| 69 | + else if (base.endsWith("/")) base + suffix |
| 70 | + else base + "/" + suffix |
| 71 | + } |
| 72 | + |
| 73 | + // copy&paste from RepositoryViewerControllerBase |
| 74 | + private def getPathObjectId(git: Git, path: String, revCommit: RevCommit): Option[ObjectId] = { |
| 75 | + @scala.annotation.tailrec |
| 76 | + def _getPathObjectId(path: String, walk: TreeWalk): Option[ObjectId] = walk.next match { |
| 77 | + case true if walk.getPathString == path => Some(walk.getObjectId(0)) |
| 78 | + case true => _getPathObjectId(path, walk) |
| 79 | + case false => None |
| 80 | + } |
| 81 | + |
| 82 | + using(new TreeWalk(git.getRepository)) { treeWalk => |
| 83 | + treeWalk.addTree(revCommit.getTree) |
| 84 | + treeWalk.setRecursive(true) |
| 85 | + _getPathObjectId(path, treeWalk) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
0 commit comments