diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 7c72572..f39c910 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -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()) }) diff --git a/routers/hat/repo/file.go b/routers/hat/repo/file.go new file mode 100644 index 0000000..e3f9a17 --- /dev/null +++ b/routers/hat/repo/file.go @@ -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) +}