Add API to get a file content

This commit is contained in:
Keiichi Watanabe
2016-10-10 00:50:38 +09:00
parent 56d68c6145
commit 9c5d3edc72
2 changed files with 31 additions and 7 deletions

View File

@@ -1,11 +1,18 @@
package gitbucket.core.api
import gitbucket.core.util.JGitUtil.FileInfo
import org.apache.commons.codec.binary.Base64
case class ApiContents(`type`: String, name: String)
case class ApiContents(`type`: String, name: String, content: Option[String], encoding: Option[String])
object ApiContents{
def apply(fileInfo: FileInfo): ApiContents =
if(fileInfo.isDirectory) ApiContents("dir", fileInfo.name)
else ApiContents("file", fileInfo.name)
def apply(fileInfo: FileInfo, content: Option[Array[Byte]]): ApiContents = {
if(fileInfo.isDirectory) {
ApiContents("dir", fileInfo.name, None, None)
} else {
content.map(arr =>
ApiContents("file", fileInfo.name, Some(Base64.encodeBase64String(arr)), Some("base64"))
).getOrElse(ApiContents("file", fileInfo.name, None, None))
}
}
}

View File

@@ -7,7 +7,7 @@ import gitbucket.core.service.PullRequestService._
import gitbucket.core.service._
import gitbucket.core.util.ControlUtil._
import gitbucket.core.util.Directory._
import gitbucket.core.util.JGitUtil.{CommitInfo, getFileList, getBranches, getDefaultBranch}
import gitbucket.core.util.JGitUtil._
import gitbucket.core.util._
import gitbucket.core.util.Implicits._
import org.eclipse.jgit.api.Git
@@ -109,13 +109,30 @@ trait ApiControllerBase extends ControllerBase {
* https://developer.github.com/v3/repos/contents/#get-contents
*/
get("/api/v3/repos/:owner/:repo/contents/*")(referrersOnly { repository =>
def getFileInfo(git: Git, revision: String, pathStr: String): Option[FileInfo] = {
val path = new java.io.File(pathStr)
val dirName = path.getParent match {
case null => "."
case s => s
}
getFileList(git, revision, dirName).find(f => f.name.equals(path.getName))
}
val path = multiParams("splat").head match {
case s if s.isEmpty => "."
case s => s
}
val refStr = params.getOrElse("ref", repository.repository.defaultBranch)
using(Git.open(getRepositoryDir(params("owner"), params("repo")))){ git =>
JsonFormat(getFileList(git, refStr, path).map{f => ApiContents(f)})
val fileList = getFileList(git, refStr, path)
if (fileList.isEmpty) { // file or NotFound
getFileInfo(git, refStr, path).map(f => {
JsonFormat(ApiContents(f, getContentFromId(git, f.id, true)))
}).getOrElse(NotFound())
} else { // directory
JsonFormat(fileList.map{f => ApiContents(f, None)})
}
}
})