1
0
Fork 0

image display in diff page

This commit is contained in:
slene 2014-03-28 01:17:09 +08:00
parent 06d3c87b23
commit 6b43067e1b
2 changed files with 36 additions and 5 deletions

View file

@ -6,12 +6,12 @@ package repo
import ( import (
"container/list" "container/list"
"fmt"
"path" "path"
"github.com/codegangsta/martini" "github.com/codegangsta/martini"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
) )
@ -50,25 +50,49 @@ func Commits(ctx *middleware.Context, params martini.Params) {
} }
func Diff(ctx *middleware.Context, params martini.Params) { func Diff(ctx *middleware.Context, params martini.Params) {
fmt.Println(params["branchname"]) userName := params["username"]
commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"]) repoName := params["reponame"]
branchName := params["branchname"]
commitId := params["commitid"]
commit, err := models.GetCommit(userName, repoName, branchName, commitId)
if err != nil { if err != nil {
ctx.Handle(404, "repo.Diff", err) ctx.Handle(404, "repo.Diff", err)
return return
} }
diff, err := models.GetDiff(models.RepoPath(params["username"], params["reponame"]), params["commitid"]) diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
if err != nil { if err != nil {
ctx.Handle(404, "repo.Diff", err) ctx.Handle(404, "repo.Diff", err)
return return
} }
isImageFile := func(name string) bool {
repoFile, err := models.GetTargetFile(userName, repoName,
branchName, commitId, name)
if err != nil {
return false
}
blob, err := repoFile.LookupBlob()
if err != nil {
return false
}
data := blob.Contents()
_, isImage := base.IsImageFile(data)
return isImage
}
shortSha := params["commitid"][:10] shortSha := params["commitid"][:10]
ctx.Data["IsImageFile"] = isImageFile
ctx.Data["Title"] = commit.Message() + " · " + shortSha ctx.Data["Title"] = commit.Message() + " · " + shortSha
ctx.Data["Commit"] = commit ctx.Data["Commit"] = commit
ctx.Data["ShortSha"] = shortSha ctx.Data["ShortSha"] = shortSha
ctx.Data["Diff"] = diff ctx.Data["Diff"] = diff
ctx.Data["IsRepoToolbarCommits"] = true ctx.Data["IsRepoToolbarCommits"] = true
ctx.Data["SourcePath"] = "/" + path.Join(params["username"], params["reponame"], "src", params["commitid"]) ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
ctx.HTML(200, "repo/diff") ctx.HTML(200, "repo/diff")
} }

View file

@ -60,7 +60,13 @@
<a class="btn btn-default btn-sm pull-right" href="{{$.SourcePath}}/{{.Name}}">View File</a> <a class="btn btn-default btn-sm pull-right" href="{{$.SourcePath}}/{{.Name}}">View File</a>
<span class="file">{{.Name}}</span> <span class="file">{{.Name}}</span>
</div> </div>
{{$isImage := (call $.IsImageFile .Name)}}
<div class="panel-body file-body file-code code-view code-diff"> <div class="panel-body file-body file-code code-view code-diff">
{{if $isImage}}
<div class="text-center">
<img src="{{$.RawPath}}/{{.Name}}">
</div>
{{else}}
<table> <table>
<tbody> <tbody>
{{range .Sections}} {{range .Sections}}
@ -201,6 +207,7 @@
</tr> --> </tr> -->
</tbody> </tbody>
</table> </table>
{{end}}
</div> </div>
</div> </div>
{{end}} {{end}}