Dispose ObjectLoader certainty

This commit is contained in:
Naoki Takezoe
2015-05-06 10:30:53 +09:00
parent 8853264808
commit 4e78f01a09
2 changed files with 27 additions and 22 deletions

View File

@@ -290,16 +290,18 @@ trait RepositoryViewerControllerBase extends ControllerBase {
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id)) val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))
val lastModifiedCommit = JGitUtil.getLastModifiedCommit(git, revCommit, path)
getPathObjectId(git, path, revCommit).map { objectId => getPathObjectId(git, path, revCommit).map { objectId =>
if(raw){ if(raw){
// Download // Download
JGitUtil.getContentFromId(git, objectId, true).map {bytes => JGitUtil.getContentFromId(git, objectId, true).map { bytes =>
RawData(FileUtil.getContentType(path, bytes), bytes) RawData(FileUtil.getContentType(path, bytes), bytes)
} getOrElse NotFound } getOrElse NotFound
} else { } else {
html.blob(id, repository, path.split("/").toList, JGitUtil.getContentInfo(git, path, objectId), html.blob(id, repository, path.split("/").toList,
new JGitUtil.CommitInfo(lastModifiedCommit), hasWritePermission(repository.owner, repository.name, context.loginAccount)) JGitUtil.getContentInfo(git, path, objectId),
new JGitUtil.CommitInfo(JGitUtil.getLastModifiedCommit(git, revCommit, path)),
hasWritePermission(repository.owner, repository.name, context.loginAccount)
)
} }
} getOrElse NotFound } getOrElse NotFound
} }

View File

@@ -639,7 +639,9 @@ object JGitUtil {
def getContentInfo(git: Git, path: String, objectId: ObjectId): ContentInfo = { def getContentInfo(git: Git, path: String, objectId: ObjectId): ContentInfo = {
// Viewer // Viewer
val large = FileUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize) using(git.getRepository.getObjectDatabase){ db =>
val loader = db.open(objectId)
val large = FileUtil.isLarge(loader.getSize)
val viewer = if(FileUtil.isImage(path)) "image" else if(large) "large" else "other" val viewer = if(FileUtil.isImage(path)) "image" else if(large) "large" else "other"
val bytes = if(viewer == "other") JGitUtil.getContentFromId(git, objectId, false) else None val bytes = if(viewer == "other") JGitUtil.getContentFromId(git, objectId, false) else None
@@ -656,6 +658,7 @@ object JGitUtil {
ContentInfo(viewer, None, None) ContentInfo(viewer, None, None)
} }
} }
}
/** /**
* Get object content of the given object id as byte array from the Git repository. * Get object content of the given object id as byte array from the Git repository.
@@ -666,12 +669,12 @@ object JGitUtil {
* @return the byte array of content or None if object does not exist * @return the byte array of content or None if object does not exist
*/ */
def getContentFromId(git: Git, id: ObjectId, fetchLargeFile: Boolean): Option[Array[Byte]] = try { def getContentFromId(git: Git, id: ObjectId, fetchLargeFile: Boolean): Option[Array[Byte]] = try {
val loader = git.getRepository.getObjectDatabase.open(id) using(git.getRepository.getObjectDatabase){ db =>
val loader = db.open(id)
if(fetchLargeFile == false && FileUtil.isLarge(loader.getSize)){ if(fetchLargeFile == false && FileUtil.isLarge(loader.getSize)){
None None
} else { } else {
using(git.getRepository.getObjectDatabase){ db => Some(loader.getBytes)
Some(db.open(id).getBytes)
} }
} }
} catch { } catch {