From 5fc9cd0e502c240e6a30422c0154ccd2b273df4e Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 10 Oct 2023 14:00:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=87=E4=BB=B6=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/structs/repo_file.go | 1 + services/repository/files/content.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/modules/structs/repo_file.go b/modules/structs/repo_file.go index 5e8bc3c..5059a17 100644 --- a/modules/structs/repo_file.go +++ b/modules/structs/repo_file.go @@ -22,6 +22,7 @@ type BatchFileResponse struct { type ContentsResponse struct { *gitea_api.ContentsResponse + IsTextFile bool `json:"is_text_file"` LatestCommit ContentsResponseCommit `json:"latest_commit"` } diff --git a/services/repository/files/content.go b/services/repository/files/content.go index a92262f..0705b20 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -11,6 +11,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" gitea_api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/typesniffer" "code.gitea.io/gitea/modules/util" gitea_files_service "code.gitea.io/gitea/services/repository/files" hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" @@ -134,6 +135,21 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref return nil, err } + blob, err := gitRepo.GetBlob(entry.ID.String()) + if err != nil { + return nil, err + } + dataRc, err := blob.DataAsync() + if err != nil { + return nil, err + } + buf := make([]byte, 1024) + n, _ := util.ReadAtMost(dataRc, buf) + buf = buf[:n] + + st := typesniffer.DetectContentType(buf) + isTextFile := st.IsText() + contentsResponse := &hat_api.ContentsResponse{ ContentsResponse: &gitea_api.ContentsResponse{ Name: entry.Name(), @@ -146,6 +162,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref Self: &selfURLString, }, }, + IsTextFile: isTextFile, LatestCommit: hat_api.ContentsResponseCommit{ Message: lastCommit.CommitMessage, Sha: lastCommit.ID.String(),