Skip to content

Commit a6b75d4

Browse files
committed
fix
1 parent f7d2b5b commit a6b75d4

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

routers/web/repo/file.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
3+
// SPDX-License-Identifier: MIT
4+
5+
package repo
6+
7+
import (
8+
"net/http"
9+
10+
"code.gitea.io/gitea/models/unit"
11+
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/services/context"
13+
files_service "code.gitea.io/gitea/services/repository/files"
14+
)
15+
16+
// canReadFiles returns true if repository is readable and user has proper access level.
17+
func canReadFiles(r *context.Repository) bool {
18+
return r.Permission.CanRead(unit.TypeCode)
19+
}
20+
21+
// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
22+
func GetContents(ctx *context.Context) {
23+
24+
if !canReadFiles(ctx.Repo) {
25+
ctx.NotFound("Invalid FilePath", nil)
26+
return
27+
}
28+
29+
treePath := ctx.PathParam("*")
30+
ref := ctx.FormTrim("ref")
31+
32+
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
33+
if git.IsErrNotExist(err) {
34+
ctx.NotFound("GetContentsOrList", err)
35+
return
36+
}
37+
ctx.ServerError("Repo.GitRepo.GetCommit", err)
38+
} else {
39+
ctx.JSON(http.StatusOK, fileList)
40+
}
41+
}
42+
43+
// GetContentsList Get the metadata of all the entries of the root dir
44+
func GetContentsList(ctx *context.Context) {
45+
GetContents(ctx)
46+
}

routers/web/web.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,10 @@ func registerRoutes(m *web.Router) {
11611161
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.TreeList)
11621162
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.TreeList)
11631163
})
1164+
m.Group("/contents", func() {
1165+
m.Get("", repo.GetContentsList)
1166+
m.Get("/*", repo.GetContents)
1167+
})
11641168
m.Get("/compare", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff)
11651169
m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists).
11661170
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).

web_src/js/components/ViewFileTree.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function updateState(visible) {
4646
async function loadChildren(item?) {
4747
const el = document.querySelector('#view-file-tree');
4848
const apiBaseUrl = el.getAttribute('data-api-base-url');
49-
const response = await GET(`/api/v1/repos/${apiBaseUrl}/contents/${item ? item.file.path : ''}`);
49+
const response = await GET(`${apiBaseUrl}/contents/${item ? item.file.path : ''}`);
5050
const json = await response.json();
5151
return json.map((i) => ({
5252
file: i,

0 commit comments

Comments
 (0)