@@ -138,18 +138,41 @@ func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
138138 return content
139139}
140140
141- // wikiContentsByName returns the contents of a wiki page, along with a boolean
142- // indicating whether the page exists. Writes to ctx if an error occurs.
143- func wikiContentsByName (ctx * context.Context , commit * git.Commit , wikiName wiki_service.WebPath ) ([]byte , * git.TreeEntry , string , bool ) {
141+ // wikiEntryByName returns the entry of a wiki page, along with a boolean
142+ // indicating whether the entry exists. Writes to ctx if an error occurs.
143+ // The last return value indicates whether the file should be returned as a raw file
144+ func wikiEntryByName (ctx * context.Context , commit * git.Commit , wikiName wiki_service.WebPath ) (* git.TreeEntry , string , bool , bool ) {
145+ isRaw := false
144146 gitFilename := wiki_service .WebPathToGitPath (wikiName )
145147 entry , err := findEntryForFile (commit , gitFilename )
146148 if err != nil && ! git .IsErrNotExist (err ) {
147149 ctx .ServerError ("findEntryForFile" , err )
148- return nil , nil , "" , false
149- } else if entry == nil {
150+ return nil , "" , false , false
151+ }
152+ if entry == nil {
153+ // check if the file without ".md" suffix exists
154+ gitFilename := strings .TrimSuffix (gitFilename , ".md" )
155+ entry , err = findEntryForFile (commit , gitFilename )
156+ if err != nil && ! git .IsErrNotExist (err ) {
157+ ctx .ServerError ("findEntryForFile" , err )
158+ return nil , "" , false , false
159+ }
160+ isRaw = true
161+ }
162+ if entry == nil {
163+ return nil , "" , true , false
164+ }
165+ return entry , gitFilename , false , isRaw
166+ }
167+
168+ // wikiContentsByName returns the contents of a wiki page, along with a boolean
169+ // indicating whether the page exists. Writes to ctx if an error occurs.
170+ func wikiContentsByName (ctx * context.Context , commit * git.Commit , wikiName wiki_service.WebPath ) ([]byte , * git.TreeEntry , string , bool ) {
171+ entry , gitFilename , noEntry , _ := wikiEntryByName (ctx , commit , wikiName )
172+ if entry == nil {
150173 return nil , nil , "" , true
151174 }
152- return wikiContentsByEntry (ctx , entry ), entry , gitFilename , false
175+ return wikiContentsByEntry (ctx , entry ), entry , gitFilename , noEntry
153176}
154177
155178func renderViewPage (ctx * context.Context ) (* git.Repository , * git.TreeEntry ) {
@@ -215,18 +238,30 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
215238 isSideBar := pageName == "_Sidebar"
216239 isFooter := pageName == "_Footer"
217240
218- // lookup filename in wiki - get filecontent, gitTree entry , real filename
219- data , entry , pageFilename , noEntry := wikiContentsByName (ctx , commit , pageName )
241+ // lookup filename in wiki - get gitTree entry , real filename
242+ entry , pageFilename , noEntry , isRaw := wikiEntryByName (ctx , commit , pageName )
220243 if noEntry {
221244 ctx .Redirect (ctx .Repo .RepoLink + "/wiki/?action=_pages" )
222245 }
246+ if isRaw {
247+ ctx .Redirect (util .URLJoin (ctx .Repo .RepoLink , "wiki/raw" , string (pageName )))
248+ }
223249 if entry == nil || ctx .Written () {
224250 if wikiRepo != nil {
225251 wikiRepo .Close ()
226252 }
227253 return nil , nil
228254 }
229255
256+ // get filecontent
257+ data := wikiContentsByEntry (ctx , entry )
258+ if ctx .Written () {
259+ if wikiRepo != nil {
260+ wikiRepo .Close ()
261+ }
262+ return nil , nil
263+ }
264+
230265 var sidebarContent []byte
231266 if ! isSideBar {
232267 sidebarContent , _ , _ , _ = wikiContentsByName (ctx , commit , "_Sidebar" )
@@ -442,15 +477,24 @@ func renderEditPage(ctx *context.Context) {
442477 ctx .Data ["Title" ] = displayName
443478 ctx .Data ["title" ] = displayName
444479
445- // lookup filename in wiki - get filecontent, gitTree entry , real filename
446- data , entry , _ , noEntry := wikiContentsByName (ctx , commit , pageName )
480+ // lookup filename in wiki - gitTree entry , real filename
481+ entry , _ , noEntry , isRaw := wikiEntryByName (ctx , commit , pageName )
447482 if noEntry {
448483 ctx .Redirect (ctx .Repo .RepoLink + "/wiki/?action=_pages" )
449484 }
485+ if isRaw {
486+ ctx .Error (http .StatusForbidden , "Editing of raw wiki files is not allowed" )
487+ }
450488 if entry == nil || ctx .Written () {
451489 return
452490 }
453491
492+ // get filecontent
493+ data := wikiContentsByEntry (ctx , entry )
494+ if ctx .Written () {
495+ return
496+ }
497+
454498 ctx .Data ["content" ] = string (data )
455499 ctx .Data ["sidebarPresent" ] = false
456500 ctx .Data ["sidebarContent" ] = ""
0 commit comments