From 72f40957861b51684466e30305d06c1bbac736f4 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 25 Nov 2022 17:04:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E7=9B=AE=E5=BD=95=E4=B8=8Breadme=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/hat/hat.go | 1 + routers/hat/repo/file.go | 62 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/routers/hat/hat.go b/routers/hat/hat.go index f39c910..161040c 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -72,6 +72,7 @@ func Routers() *web.Route { }) m.Group("/readme", func() { m.Get("", repo.GetReadmeContents) + m.Get("/*", repo.GetReadmeContentsByPath) }) m.Get("/commits_slice", repo.GetAllCommitsSliceByTime) }, repoAssignment()) diff --git a/routers/hat/repo/file.go b/routers/hat/repo/file.go index e3f9a17..96def2d 100644 --- a/routers/hat/repo/file.go +++ b/routers/hat/repo/file.go @@ -22,10 +22,10 @@ func GetReadmeContents(ctx *context.APIContext) { return } - treePath := ctx.Params("*") ref := ctx.Params(":ref") readmePath := "README.md" - filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) + readmezhPath := "" + filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref) if err != nil { ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) return @@ -35,12 +35,18 @@ func GetReadmeContents(ctx *context.APIContext) { for _, file := range filesList { if strings.ToLower(file.Name) == "readme.md" { readmePath = file.Name - break + } + if strings.ToLower(file.Name) == "readme_zh.md" { + readmezhPath = file.Name } } } - newTreePath := treePath + "/" + readmePath - if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil { + + if readmezhPath != "" { + readmePath = readmezhPath + } + + if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref); err != nil { if git.IsErrNotExist(err) { ctx.NotFound("GetContentsOrList", err) return @@ -55,3 +61,49 @@ func GetReadmeContents(ctx *context.APIContext) { func canReadFiles(r *context.Repository) bool { return r.Permission.CanRead(unit_model.UnitCode.Type) } + +func GetReadmeContentsByPath(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" + readmezhPath := "" + 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 + } + if strings.ToLower(file.Name) == "readme_zh.md" { + readmezhPath = file.Name + } + } + } + + if readmezhPath != "" { + readmePath = readmezhPath + } + + 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) + } +}