]> source.dussan.org Git - gitea.git/commitdiff
image display in diff page
authorslene <vslene@gmail.com>
Thu, 27 Mar 2014 17:17:09 +0000 (01:17 +0800)
committerslene <vslene@gmail.com>
Thu, 27 Mar 2014 17:17:09 +0000 (01:17 +0800)
routers/repo/commit.go
templates/repo/diff.tmpl

index 4a126d236b0ed39313c6f2b2b1f6c55579672b6e..afc1ffda29e638cd0d83cc413a1fac22871e27fd 100644 (file)
@@ -6,12 +6,12 @@ package repo
 
 import (
        "container/list"
-       "fmt"
        "path"
 
        "github.com/codegangsta/martini"
 
        "github.com/gogits/gogs/models"
+       "github.com/gogits/gogs/modules/base"
        "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) {
-       fmt.Println(params["branchname"])
-       commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"])
+       userName := params["username"]
+       repoName := params["reponame"]
+       branchName := params["branchname"]
+       commitId := params["commitid"]
+
+       commit, err := models.GetCommit(userName, repoName, branchName, commitId)
        if err != nil {
                ctx.Handle(404, "repo.Diff", err)
                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 {
                ctx.Handle(404, "repo.Diff", err)
                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]
+       ctx.Data["IsImageFile"] = isImageFile
        ctx.Data["Title"] = commit.Message() + " ยท " + shortSha
        ctx.Data["Commit"] = commit
        ctx.Data["ShortSha"] = shortSha
        ctx.Data["Diff"] = diff
        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")
 }
index 809a4873c3a222309bc4fecd9997a5b58ccedc81..e58f2d664e8a38ea2e967b917cde4d35c412bd1e 100644 (file)
                 <a class="btn btn-default btn-sm pull-right" href="{{$.SourcePath}}/{{.Name}}">View File</a>
                 <span class="file">{{.Name}}</span>
             </div>
+            {{$isImage := (call $.IsImageFile .Name)}}
             <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>
                     <tbody>
                         {{range .Sections}}
                         </tr> -->
                     </tbody>
                 </table>
+                {{end}}
             </div>
         </div>
         {{end}}