diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-28 17:44:08 +0800 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2020-01-28 09:44:08 +0000 |
commit | c58edc7c7884a44436be7e215aed06a86e44dab8 (patch) | |
tree | 2336e54e2c0d2d615f3395fd56711eb1a2eef20c /routers/repo | |
parent | a08175ac559267478c76dd2392067f8d4674cd19 (diff) | |
download | gitea-c58edc7c7884a44436be7e215aed06a86e44dab8.tar.gz gitea-c58edc7c7884a44436be7e215aed06a86e44dab8.zip |
Fix wiki raw view on sub path (#10002)
* Fix wiki raw view on sub path
* Add test for subpath wiki raw file
* Fix bug
Diffstat (limited to 'routers/repo')
-rw-r--r-- | routers/repo/wiki.go | 28 | ||||
-rw-r--r-- | routers/repo/wiki_test.go | 1 |
2 files changed, 11 insertions, 18 deletions
diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index b0bd8047b8..f18625a599 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -66,27 +66,20 @@ type PageMeta struct { // findEntryForFile finds the tree entry for a target filepath. func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) { - entries, err := commit.ListEntries() - if err != nil { + entry, err := commit.GetTreeEntryByPath(target) + if err != nil && !git.IsErrNotExist(err) { return nil, err } - // The longest name should be checked first - for _, entry := range entries { - if entry.IsRegular() && entry.Name() == target { - return entry, nil - } + if entry != nil { + return entry, nil } + // Then the unescaped, shortest alternative var unescapedTarget string if unescapedTarget, err = url.QueryUnescape(target); err != nil { return nil, err } - for _, entry := range entries { - if entry.IsRegular() && entry.Name() == unescapedTarget { - return entry, nil - } - } - return nil, nil + return commit.GetTreeEntryByPath(unescapedTarget) } func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) { @@ -123,10 +116,9 @@ func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte { // wikiContentsByName returns the contents of a wiki page, along with a boolean // indicating whether the page exists. Writes to ctx if an error occurs. func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName string) ([]byte, *git.TreeEntry, string, bool) { - var entry *git.TreeEntry - var err error pageFilename := wiki_service.NameToFilename(wikiName) - if entry, err = findEntryForFile(commit, pageFilename); err != nil { + entry, err := findEntryForFile(commit, pageFilename) + if err != nil && !git.IsErrNotExist(err) { ctx.ServerError("findEntryForFile", err) return nil, nil, "", false } else if entry == nil { @@ -518,7 +510,7 @@ func WikiRaw(ctx *context.Context) { if commit != nil { // Try to find a file with that name entry, err = findEntryForFile(commit, providedPath) - if err != nil { + if err != nil && !git.IsErrNotExist(err) { ctx.ServerError("findFile", err) return } @@ -531,7 +523,7 @@ func WikiRaw(ctx *context.Context) { wikiPath := wiki_service.NameToFilename(providedPath) entry, err = findEntryForFile(commit, wikiPath) - if err != nil { + if err != nil && !git.IsErrNotExist(err) { ctx.ServerError("findFile", err) return } diff --git a/routers/repo/wiki_test.go b/routers/repo/wiki_test.go index 843a8ddf2b..cc79c808f5 100644 --- a/routers/repo/wiki_test.go +++ b/routers/repo/wiki_test.go @@ -192,6 +192,7 @@ func TestDeleteWikiPagePost(t *testing.T) { func TestWikiRaw(t *testing.T) { for filepath, filetype := range map[string]string{ "jpeg.jpg": "image/jpeg", + "images/jpeg.jpg": "image/jpeg", "Page With Spaced Name": "text/plain; charset=utf-8", "Page-With-Spaced-Name": "text/plain; charset=utf-8", "Page With Spaced Name.md": "text/plain; charset=utf-8", |