Fix download archive issue

This commit is contained in:
Unknwon 2014-09-24 17:43:33 -04:00
parent 612fdb98df
commit 25268577a5
6 changed files with 53 additions and 23 deletions

View file

@ -343,6 +343,7 @@ func runWeb(*cli.Context) {
r.Get("/issues/:index", repo.ViewIssue) r.Get("/issues/:index", repo.ViewIssue)
r.Get("/pulls", repo.Pulls) r.Get("/pulls", repo.Pulls)
r.Get("/branches", repo.Branches) r.Get("/branches", repo.Branches)
r.Get("/archive/*", repo.Download)
}, ignSignIn, middleware.RepoAssignment(true)) }, ignSignIn, middleware.RepoAssignment(true))
m.Group("/:username/:reponame", func(r *macaron.Router) { m.Group("/:username/:reponame", func(r *macaron.Router) {
@ -355,8 +356,6 @@ func runWeb(*cli.Context) {
r.Get("/commit/:branchname", repo.Diff) r.Get("/commit/:branchname", repo.Diff)
r.Get("/commit/:branchname/*", repo.Diff) r.Get("/commit/:branchname/*", repo.Diff)
r.Get("/releases", repo.Releases) r.Get("/releases", repo.Releases)
r.Get("/archive/:branchname/*.*", repo.Download)
r.Get("/archive/*.*", repo.Download)
r.Get("/compare/:before([a-z0-9]+)...:after([a-z0-9]+)", repo.CompareDiff) r.Get("/compare/:before([a-z0-9]+)...:after([a-z0-9]+)", repo.CompareDiff)
}, ignSignIn, middleware.RepoAssignment(true, true)) }, ignSignIn, middleware.RepoAssignment(true, true))

View file

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.5.4.0923 Beta" const APP_VER = "0.5.4.0924 Beta"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View file

@ -200,7 +200,7 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler {
ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName) ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
if err != nil { if err != nil {
ctx.Handle(404, "RepoAssignment invalid branch", nil) ctx.Handle(500, "RepoAssignment invalid branch", err)
return return
} }
ctx.Repo.CommitId = ctx.Repo.Commit.Id.String() ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
@ -209,14 +209,9 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler {
ctx.Repo.IsTag = true ctx.Repo.IsTag = true
ctx.Repo.BranchName = refName ctx.Repo.BranchName = refName
ctx.Repo.Tag, err = gitRepo.GetTag(refName) ctx.Repo.Commit, err = gitRepo.GetCommitOfTag(refName)
if err != nil { if err != nil {
ctx.Handle(404, "RepoAssignment invalid tag", nil) ctx.Handle(500, "RepoAssignment invalid tag", err)
return
}
ctx.Repo.Commit, err = ctx.Repo.Tag.Commit()
if err != nil {
ctx.Handle(500, "RepoAssignment", fmt.Errorf("fail to get tag commit(%s): %v", refName, err))
return return
} }
ctx.Repo.CommitId = ctx.Repo.Commit.Id.String() ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()

View file

@ -244,18 +244,25 @@ func Action(ctx *middleware.Context) {
} }
func Download(ctx *middleware.Context) { func Download(ctx *middleware.Context) {
ext := "." + ctx.Params(":ext") var (
uri = ctx.Params("*")
refName string
ext string
archivePath string
)
var archivePath string switch {
switch ext { case strings.HasSuffix(uri, ".zip"):
case ".zip": ext = ".zip"
archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
case ".tar.gz": case strings.HasSuffix(uri, ".tar.gz"):
ext = ".tar.gz"
archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
default: default:
ctx.Error(404) ctx.Error(404)
return return
} }
refName = strings.TrimSuffix(uri, ext)
if !com.IsDir(archivePath) { if !com.IsDir(archivePath) {
if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
@ -264,13 +271,42 @@ func Download(ctx *middleware.Context) {
} }
} }
// Get corresponding commit.
var (
commit *git.Commit
err error
)
gitRepo := ctx.Repo.GitRepo
if gitRepo.IsBranchExist(refName) {
commit, err = gitRepo.GetCommitOfBranch(refName)
if err != nil {
ctx.Handle(500, "Download", err)
return
}
} else if gitRepo.IsTagExist(refName) {
commit, err = gitRepo.GetCommitOfTag(refName)
if err != nil {
ctx.Handle(500, "Download", err)
return
}
} else if len(refName) == 40 {
commit, err = gitRepo.GetCommit(refName)
if err != nil {
ctx.Handle(404, "Download", nil)
return
}
} else {
ctx.Error(404)
return
}
archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext) archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext)
if !com.IsFile(archivePath) { if !com.IsFile(archivePath) {
if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil { if err := commit.CreateArchive(archivePath, git.ZIP); err != nil {
ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
return return
} }
} }
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext) ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext)
} }

View file

@ -1 +1 @@
0.5.4.0923 Beta 0.5.4.0924 Beta

View file

@ -36,8 +36,8 @@
{{str2html .Note}} {{str2html .Note}}
</div> </div>
<p class="download"> <p class="download">
<a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-download"></i>Source Code (ZIP)</a> <a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}.zip" rel="nofollow"><i class="fa fa-download"></i>Source Code (ZIP)</a>
<a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.tar.gz"><i class="fa fa-download"></i>Source Code (TAR.GZ)</a> <a class="btn btn-default" href="{{$.RepoLink}}/archive/{{.TagName}}.tar.gz"><i class="fa fa-download"></i>Source Code (TAR.GZ)</a>
</p> </p>
<span class="dot">&nbsp;</span> <span class="dot">&nbsp;</span>
</div> </div>
@ -48,8 +48,8 @@
<div class="col-md-10"> <div class="col-md-10">
<h5 class="title"><a href="{{$.RepoLink}}/src/{{.TagName}}" rel="nofollow">{{.TagName}}</a><i class="fa fa-tag"></i></h5> <h5 class="title"><a href="{{$.RepoLink}}/src/{{.TagName}}" rel="nofollow">{{.TagName}}</a><i class="fa fa-tag"></i></h5>
<p class="download"> <p class="download">
<a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-download"></i>zip</a> <a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}.zip" rel="nofollow"><i class="fa fa-download"></i>zip</a>
<a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}/{{$.Repository.Name}}.tar.gz"><i class="fa fa-download"></i>tar.gz</a> <a class="download-link" href="{{$.RepoLink}}/archive/{{.TagName}}.tar.gz"><i class="fa fa-download"></i>tar.gz</a>
</p> </p>
<span class="dot">&nbsp;</span> <span class="dot">&nbsp;</span>
</div> </div>