新增:readme获取接口

This commit is contained in:
yystopf 2022-11-24 13:57:36 +08:00
parent 2c512957e5
commit 564abb9bb8
2 changed files with 60 additions and 0 deletions

View File

@ -70,6 +70,9 @@ func Routers() *web.Route {
Delete(repo.DeleteWiki)
})
})
m.Group("/readme", func() {
m.Get("", repo.GetReadmeContents)
})
m.Get("/commits_slice", repo.GetAllCommitsSliceByTime)
}, repoAssignment())
})

57
routers/hat/repo/file.go Normal file
View File

@ -0,0 +1,57 @@
package repo
import (
"net/http"
"strings"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
files_service "code.gitea.io/gitea/services/repository/files"
)
func GetReadmeContents(ctx *context.APIContext) {
if !canReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "canReadFiles", repo_model.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.ContextUser.ID,
RepoName: ctx.Repo.Repository.LowerName,
})
return
}
treePath := ctx.Params("*")
ref := ctx.Params(":ref")
readmePath := "README.md"
filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
return
}
filesList, ok := filesListInterface.([]*api.ContentsResponse)
if ok {
for _, file := range filesList {
if strings.ToLower(file.Name) == "readme.md" {
readmePath = file.Name
break
}
}
}
newTreePath := treePath + "/" + readmePath
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
} else {
ctx.JSON(http.StatusOK, fileList)
}
}
func canReadFiles(r *context.Repository) bool {
return r.Permission.CanRead(unit_model.UnitCode.Type)
}