Skip to content

Commit 3b7f41f

Browse files
gabrielsimoestechknowlogick
authored andcommitted
Fix serving of raw wiki files other than .md (#5814)
* Fix serving of raw wiki files other than .md Closes #4690. Closes #4395. Signed-off-by: Gabriel Silva Simões <[email protected]> * Simplify code at routers/repo/wiki.go Signed-off-by: Gabriel Silva Simões <[email protected]> * Add more files to user2/repo1.wiki for testing Signed-off-by: Gabriel Silva Simões <[email protected]> * Update macaron to v1.3.2 Signed-off-by: Gabriel Silva Simões <[email protected]> * Add tests for WikiRaw Signed-off-by: Gabriel Silva Simões <[email protected]> * Fix NewResponseWriter usage due to macaron update Signed-off-by: Gabriel Silva Simões <[email protected]> * Add raw to reserved wiki names Signed-off-by: Gabriel Silva Simões <[email protected]>
1 parent 4a747ae commit 3b7f41f

17 files changed

+77
-31
lines changed

Gopkg.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x���m� D�M��Y����(�J�`�5�ɜ-�K*Ki,Hi!?��<�i�Vki0Z��XH�D(Z6ĨG�Sb��3�JD�h��!�uB��DaJp� ���F�Lƹ4+~��v�;���
2+
e����[Nx>K�����_s�q�/�]09MHpѤ��k���_d�-%�풇۞�� v�_�]��^�/�I[t
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x�M�@ ��M����r�›�6�&&&��9Le�św����t<#���͡�mv-��0w�b��jy̖��ڗ~݋[�����=H� �.�"������ǁ=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x���m�0���)n���t2�S����`ņ���e�,VY�/H�#�[)��E��@N�q��툎�r2�)D��0�j�C���L��aC��&�4B�v]$E����Iӑe����P�r�I�s�e�z�˳~_
2+
���[y��v��W��V=헛�˘�H vZ~s�@݉%����?T�ZH
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2c54faec6c45d31c1abfaecdab471eac6633738a
1+
0cf15c3f66ec8384480ed9c3cf87c9e97fbb0ec3

models/wiki.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
var (
25-
reservedWikiNames = []string{"_pages", "_new", "_edit"}
25+
reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"}
2626
wikiWorkingPool = sync.NewExclusivePool()
2727
)
2828

routers/api/v1/misc/markdown_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func createContext(req *http.Request) (*macaron.Context, *httptest.ResponseRecor
2727
c := &macaron.Context{
2828
Injector: inject.New(),
2929
Req: macaron.Request{Request: req},
30-
Resp: macaron.NewResponseWriter(resp),
30+
Resp: macaron.NewResponseWriter(req.Method, resp),
3131
Render: &macaron.DummyRender{ResponseWriter: resp},
3232
Data: make(map[string]interface{}),
3333
}

routers/repo/wiki.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -295,26 +295,41 @@ func WikiRaw(ctx *context.Context) {
295295
return
296296
}
297297
}
298+
298299
providedPath := ctx.Params("*")
299-
if strings.HasSuffix(providedPath, ".md") {
300-
providedPath = providedPath[:len(providedPath)-3]
301-
}
302-
wikiPath := models.WikiNameToFilename(providedPath)
300+
303301
var entry *git.TreeEntry
304302
if commit != nil {
305-
entry, err = findEntryForFile(commit, wikiPath)
303+
// Try to find a file with that name
304+
entry, err = findEntryForFile(commit, providedPath)
305+
if err != nil {
306+
ctx.ServerError("findFile", err)
307+
return
308+
}
309+
310+
if entry == nil {
311+
// Try to find a wiki page with that name
312+
if strings.HasSuffix(providedPath, ".md") {
313+
providedPath = providedPath[:len(providedPath)-3]
314+
}
315+
316+
wikiPath := models.WikiNameToFilename(providedPath)
317+
entry, err = findEntryForFile(commit, wikiPath)
318+
if err != nil {
319+
ctx.ServerError("findFile", err)
320+
return
321+
}
322+
}
306323
}
307-
if err != nil {
308-
ctx.ServerError("findFile", err)
309-
return
310-
} else if entry == nil {
311-
ctx.NotFound("findEntryForFile", nil)
324+
325+
if entry != nil {
326+
if err = ServeBlob(ctx, entry.Blob()); err != nil {
327+
ctx.ServerError("ServeBlob", err)
328+
}
312329
return
313330
}
314331

315-
if err = ServeBlob(ctx, entry.Blob()); err != nil {
316-
ctx.ServerError("ServeBlob", err)
317-
}
332+
ctx.NotFound("findEntryForFile", nil)
318333
}
319334

320335
// NewWiki render wiki create page

routers/repo/wiki_test.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestWiki(t *testing.T) {
7777
Wiki(ctx)
7878
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
7979
assert.EqualValues(t, "Home", ctx.Data["Title"])
80-
assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
80+
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
8181
}
8282

8383
func TestWikiPages(t *testing.T) {
@@ -87,7 +87,7 @@ func TestWikiPages(t *testing.T) {
8787
test.LoadRepo(t, ctx, 1)
8888
WikiPages(ctx)
8989
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
90-
assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
90+
assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
9191
}
9292

9393
func TestNewWiki(t *testing.T) {
@@ -185,3 +185,23 @@ func TestDeleteWikiPagePost(t *testing.T) {
185185
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
186186
assertWikiNotExists(t, ctx.Repo.Repository, "Home")
187187
}
188+
189+
func TestWikiRaw(t *testing.T) {
190+
for filepath, filetype := range map[string]string{
191+
"jpeg.jpg": "image/jpeg",
192+
"Page With Spaced Name": "text/plain; charset=utf-8",
193+
"Page-With-Spaced-Name": "text/plain; charset=utf-8",
194+
"Page With Spaced Name.md": "text/plain; charset=utf-8",
195+
"Page-With-Spaced-Name.md": "text/plain; charset=utf-8",
196+
} {
197+
models.PrepareTestEnv(t)
198+
199+
ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+filepath)
200+
ctx.SetParams("*", filepath)
201+
test.LoadUser(t, ctx, 2)
202+
test.LoadRepo(t, ctx, 1)
203+
WikiRaw(ctx)
204+
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
205+
assert.EqualValues(t, filetype, ctx.Resp.Header().Get("Content-Type"))
206+
}
207+
}

vendor/gopkg.in/macaron.v1/context.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/macaron.v1/macaron.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/macaron.v1/response_writer.go

+8-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/macaron.v1/router.go

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)