获取文件内容增加文件是否文本标识

This commit is contained in:
xxq250 2023-10-10 14:00:09 +08:00
parent f6ce389d6c
commit 5fc9cd0e50
2 changed files with 18 additions and 0 deletions

View File

@ -22,6 +22,7 @@ type BatchFileResponse struct {
type ContentsResponse struct {
*gitea_api.ContentsResponse
IsTextFile bool `json:"is_text_file"`
LatestCommit ContentsResponseCommit `json:"latest_commit"`
}

View File

@ -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(),