235 lines
6.3 KiB
Go
235 lines
6.3 KiB
Go
package files
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
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"
|
|
)
|
|
|
|
func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (interface{}, error) {
|
|
if repo.IsEmpty {
|
|
return make([]interface{}, 0), nil
|
|
}
|
|
|
|
if ref == "" {
|
|
ref = repo.DefaultBranch
|
|
}
|
|
|
|
origRef := ref
|
|
|
|
cleanTreePath := gitea_files_service.CleanUploadFileName(treePath)
|
|
if cleanTreePath == "" && treePath != "" {
|
|
return nil, models.ErrFilenameInvalid{
|
|
Path: treePath,
|
|
}
|
|
}
|
|
treePath = cleanTreePath
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer closer.Close()
|
|
|
|
commit, err := gitRepo.GetCommit(ref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
entry, err := commit.GetTreeEntryByPath(treePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if entry.Type() != "tree" {
|
|
return GetContents(ctx, repo, treePath, origRef, false)
|
|
}
|
|
|
|
var fileList []*hat_api.ContentsResponse
|
|
|
|
gitTree, err := commit.SubTree(treePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries, err := gitTree.ListEntries()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, e := range entries {
|
|
subTreePath := path.Join(treePath, e.Name())
|
|
fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fileList = append(fileList, fileContentResponse)
|
|
}
|
|
|
|
return fileList, nil
|
|
}
|
|
|
|
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*hat_api.ContentsResponse, error) {
|
|
if ref == "" {
|
|
ref = repo.DefaultBranch
|
|
}
|
|
origRef := ref
|
|
|
|
cleanTreePath := gitea_files_service.CleanUploadFileName(treePath)
|
|
if cleanTreePath == "" && treePath != "" {
|
|
return nil, models.ErrFilenameInvalid{
|
|
Path: treePath,
|
|
}
|
|
}
|
|
|
|
treePath = cleanTreePath
|
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer closer.Close()
|
|
|
|
commit, err := gitRepo.GetCommit(ref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commitID := commit.ID.String()
|
|
if len(ref) >= 4 && strings.HasPrefix(commitID, ref) {
|
|
ref = commit.ID.String()
|
|
}
|
|
|
|
entry, err := commit.GetTreeEntryByPath(treePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
refType := gitRepo.GetRefType(ref)
|
|
if refType == "invalid" {
|
|
return nil, fmt.Errorf("no commit found for the ref [ref %s]", ref)
|
|
}
|
|
|
|
selfURL, err := url.Parse(repo.APIURL() + "/contents/" + util.PathEscapeSegments(treePath) + "?ref=" + url.QueryEscape(origRef))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selfURLString := selfURL.String()
|
|
|
|
err = gitRepo.AddLastCommitCache(repo.GetCommitsCountCacheKey(ref, refType != git.ObjectCommit), repo.FullName(), commitID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lastCommit, err := commit.GetCommitByPath(treePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var isTextFile = false
|
|
|
|
if !entry.IsSubModule() {
|
|
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(),
|
|
Path: treePath,
|
|
SHA: entry.ID.String(),
|
|
LastCommitSHA: lastCommit.ID.String(),
|
|
Size: entry.Size(),
|
|
URL: &selfURLString,
|
|
Links: &gitea_api.FileLinksResponse{
|
|
Self: &selfURLString,
|
|
},
|
|
},
|
|
IsTextFile: isTextFile,
|
|
LatestCommit: hat_api.ContentsResponseCommit{
|
|
Message: lastCommit.CommitMessage,
|
|
Sha: lastCommit.ID.String(),
|
|
CreatedAt: lastCommit.Author.When.Unix(),
|
|
},
|
|
}
|
|
|
|
// Now populate the rest of the ContentsResponse based on entry type
|
|
if entry.IsRegular() || entry.IsExecutable() {
|
|
contentsResponse.Type = string(gitea_files_service.ContentTypeRegular)
|
|
if blobResponse, err := gitea_files_service.GetBlobBySHA(ctx, repo, gitRepo, entry.ID.String()); err != nil {
|
|
return nil, err
|
|
} else if !forList {
|
|
// We don't show the content if we are getting a list of FileContentResponses
|
|
contentsResponse.Encoding = &blobResponse.Encoding
|
|
contentsResponse.Content = &blobResponse.Content
|
|
}
|
|
} else if entry.IsDir() {
|
|
contentsResponse.Type = string(gitea_files_service.ContentTypeDir)
|
|
} else if entry.IsLink() {
|
|
contentsResponse.Type = string(gitea_files_service.ContentTypeLink)
|
|
// The target of a symlink file is the content of the file
|
|
targetFromContent, err := entry.Blob().GetBlobContent()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
contentsResponse.Target = &targetFromContent
|
|
} else if entry.IsSubModule() {
|
|
contentsResponse.Type = string(gitea_files_service.ContentTypeSubmodule)
|
|
submodule, err := commit.GetSubModule(treePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if submodule != nil {
|
|
contentsResponse.SubmoduleGitURL = &submodule.URL
|
|
}
|
|
}
|
|
// Handle links
|
|
if entry.IsRegular() || entry.IsLink() {
|
|
downloadURL, err := url.Parse(repo.HTMLURL() + "/raw/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
downloadURLString := downloadURL.String()
|
|
contentsResponse.DownloadURL = &downloadURLString
|
|
}
|
|
if !entry.IsSubModule() {
|
|
htmlURL, err := url.Parse(repo.HTMLURL() + "/src/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
htmlURLString := htmlURL.String()
|
|
contentsResponse.HTMLURL = &htmlURLString
|
|
contentsResponse.Links.HTMLURL = &htmlURLString
|
|
|
|
gitURL, err := url.Parse(repo.APIURL() + "/git/blobs/" + url.PathEscape(entry.ID.String()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
gitURLString := gitURL.String()
|
|
contentsResponse.GitURL = &gitURLString
|
|
contentsResponse.Links.GitURL = &gitURLString
|
|
}
|
|
|
|
return contentsResponse, nil
|
|
}
|