Skip to content
This repository was archived by the owner on Sep 18, 2024. It is now read-only.

Commit 75eb68b

Browse files
author
Lei Da
committed
add browse url and download url
1 parent 68afabe commit 75eb68b

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

modules/structs/repo_tree.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import "time"
77

88
// GitEntry represents a git tree
99
type GitEntry struct {
10-
Name string `json:"name"`
11-
Path string `json:"path"`
12-
Mode string `json:"mode"`
13-
Type string `json:"type"`
14-
Size int64 `json:"size"`
15-
SHA string `json:"sha"`
16-
URL string `json:"url"`
17-
CommitMsg string `json:"commit_msg"`
18-
CommitterDate time.Time `json:"committer_date"`
19-
IsLfs bool `json:"is_lfs"`
10+
Name string `json:"name"`
11+
Path string `json:"path"`
12+
Mode string `json:"mode"`
13+
Type string `json:"type"`
14+
Size int64 `json:"size"`
15+
SHA string `json:"sha"`
16+
URL string `json:"url"`
17+
CommitMsg string `json:"commit_msg"`
18+
CommitterDate time.Time `json:"committer_date"`
19+
IsLfs bool `json:"is_lfs"`
20+
LfsRelativePath string `json:"lfs_relative_path"`
21+
DownloadUrl string `json:"download_url"`
2022
}
2123

2224
// GitTreeResponse returns a git tree

routers/api/v1/repo/tree.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package repo
66
import (
77
"fmt"
88
"net/http"
9+
"net/url"
10+
"strings"
911

1012
"code.gitea.io/gitea/modules/base"
1113
"code.gitea.io/gitea/modules/context"
@@ -27,15 +29,15 @@ func GetDirInfos(ctx *context.APIContext) {
2729
return
2830
}
2931
path := ctx.Req.URL.Query().Get("path")
30-
entries, err := getDirectoryEntries(ctx, path)
32+
entries, err := getDirectoryEntries(ctx, branch, path)
3133
if err != nil {
3234
ctx.JSON(http.StatusInternalServerError, "failed to get directry entries, error: "+err.Error())
3335
return
3436
}
3537
ctx.JSON(http.StatusOK, entries)
3638
}
3739

38-
func getDirectoryEntries(ctx *context.APIContext, folder string) ([]structs.GitEntry, error) {
40+
func getDirectoryEntries(ctx *context.APIContext, branch, folder string) ([]structs.GitEntry, error) {
3941
tree, err := ctx.Repo.Commit.SubTree(folder)
4042
if err != nil {
4143
return nil, fmt.Errorf("failed to exec SubTree, cause:%w", err)
@@ -52,27 +54,30 @@ func getDirectoryEntries(ctx *context.APIContext, folder string) ([]structs.GitE
5254
if err != nil {
5355
return nil, fmt.Errorf("failed to exec GetCommitsInfo, cause:%w", err)
5456
}
57+
5558
var ges = make([]structs.GitEntry, 0, len(commits))
5659
for _, c := range commits {
57-
5860
e := structs.GitEntry{
5961
Name: c.Entry.Name(),
6062
Path: folder + "/" + c.Entry.Name(),
6163
Mode: c.Entry.Mode().String(),
6264
Type: c.Entry.Type(),
6365
Size: c.Entry.Size(),
6466
SHA: c.Commit.ID.String(),
65-
URL: "",
6667
CommitMsg: c.Commit.CommitMessage,
6768
CommitterDate: c.Commit.Committer.When,
6869
}
70+
e.URL = ctx.Repo.Repository.HTMLURL() + "/raw/branch/" + url.PathEscape(branch) + "/" + url.PathEscape(strings.TrimPrefix(e.Path, "/"))
71+
e.DownloadUrl = e.URL
6972
//lfs pointer size is less than 1024
7073
if c.Entry.Size() <= 1024 {
7174
content, _ := c.Entry.Blob().GetBlobContent(1024)
7275
p, _ := lfs.ReadPointerFromBuffer([]byte(content))
7376
if p.IsValid() {
7477
e.Size = p.Size
7578
e.IsLfs = true
79+
e.LfsRelativePath = p.RelativePath()
80+
e.DownloadUrl = ctx.Repo.Repository.HTMLURL() + "/media/branch/" + url.PathEscape(branch) + "/" + url.PathEscape(strings.TrimPrefix(e.Path, "/"))
7681
}
7782
}
7883
ges = append(ges, e)

0 commit comments

Comments
 (0)