forked from kevadesu/forgejo
Add github compatible tarball download API endpoints (#32572)
Fix #29654 Fix #32481 (cherry picked from commit 703be6bf307ed19ce8dc8cd311d24aeb6e5b9861) Conflicts: routers/api/v1/repo/file.go routers/web/repo/repo.go services/repository/archiver/archiver.go services/repository/archiver/archiver_test.go trivial context conflicts add missing function PathParam skipped in a very large refactor
This commit is contained in:
parent
3135e146f9
commit
3973f1022d
7 changed files with 152 additions and 31 deletions
53
routers/api/v1/repo/download.go
Normal file
53
routers/api/v1/repo/download.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/gitrepo"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
archiver_service "code.gitea.io/gitea/services/repository/archiver"
|
||||
)
|
||||
|
||||
func DownloadArchive(ctx *context.APIContext) {
|
||||
var tp git.ArchiveType
|
||||
switch ballType := ctx.Params("ball_type"); ballType {
|
||||
case "tarball":
|
||||
tp = git.TARGZ
|
||||
case "zipball":
|
||||
tp = git.ZIP
|
||||
case "bundle":
|
||||
tp = git.BUNDLE
|
||||
default:
|
||||
ctx.Error(http.StatusBadRequest, "", fmt.Sprintf("Unknown archive type: %s", ballType))
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.Repo.GitRepo == nil {
|
||||
gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.GitRepo = gitRepo
|
||||
defer gitRepo.Close()
|
||||
}
|
||||
|
||||
r, err := archiver_service.NewRequest(ctx, ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.Params("*"), tp)
|
||||
if err != nil {
|
||||
ctx.ServerError("NewRequest", err)
|
||||
return
|
||||
}
|
||||
|
||||
archive, err := r.Await(ctx)
|
||||
if err != nil {
|
||||
ctx.ServerError("archive.Await", err)
|
||||
return
|
||||
}
|
||||
|
||||
download(ctx, r.GetArchiveName(), archive)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue