func Download(ctx *context.Context) { var ( uri = ctx.Params("*") refName string ext string archivePath string archiveType git.ArchiveType ) switch { case strings.HasSuffix(uri, ".zip"): ext = ".zip" archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") archiveType = git.ZIP case strings.HasSuffix(uri, ".tar.gz"): ext = ".tar.gz" archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") archiveType = git.TARGZ default: log.Trace("Unknown format: %s", uri) ctx.Error(404) return } refName = strings.TrimSuffix(uri, ext) if !com.IsDir(archivePath) { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err) return } } // Get corresponding commit. var ( commit *git.Commit err error ) gitRepo := ctx.Repo.GitRepo if gitRepo.IsBranchExist(refName) { commit, err = gitRepo.GetBranchCommit(refName) if err != nil { ctx.Handle(500, "GetBranchCommit", err) return } } else if gitRepo.IsTagExist(refName) { commit, err = gitRepo.GetTagCommit(refName) if err != nil { ctx.Handle(500, "GetTagCommit", err) return } } else if len(refName) == 40 { commit, err = gitRepo.GetCommit(refName) if err != nil { ctx.Handle(404, "GetCommit", nil) return } } else { ctx.Handle(404, "Download", nil) return } archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext) if !com.IsFile(archivePath) { if err := commit.CreateArchive(archivePath, archiveType); err != nil { ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) return } } ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+refName+ext) }