implement /raw/[0-9]+ endpoint

This commit is contained in:
mappu 2017-07-09 13:18:18 +12:00
parent 187f8b8417
commit cceffc93ad
2 changed files with 29 additions and 0 deletions

5
DB.go
View File

@ -99,6 +99,11 @@ func (this *WikiDB) GetArticleById(articleId int) (*Article, error) {
return this.parseArticle(row)
}
func (this *WikiDB) GetRevision(revId int) (*ArticleWithTitle, error) {
row := this.db.QueryRow(`SELECT articles.*, titles.title FROM articles JOIN titles ON articles.article=titles.id WHERE articles.id = ?`, revId)
return this.parseArticleWithTitle(row)
}
func (this *WikiDB) GetLatestVersion(title string) (*Article, error) {
row := this.db.QueryRow(`SELECT articles.* FROM articles WHERE article = (SELECT id FROM titles WHERE title = ?) ORDER BY modified DESC LIMIT 1`, title)
return this.parseArticle(row)

View File

@ -8,6 +8,7 @@ import (
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
@ -147,6 +148,28 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.servePageResponse(w, r, pto)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") {
revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
}
a, err := this.db.GetRevision(revId)
if err != nil {
if err == sql.ErrNoRows {
this.serveErrorMessage(w, errors.New("No such revision."))
return
}
this.serveErrorMessage(w, err)
return
}
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
w.WriteHeader(200)
w.Write(a.Body)
return
}
}
@ -179,6 +202,7 @@ func (this *WikiServer) serveRedirect(w http.ResponseWriter, location string) {
}
func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) {
w.WriteHeader(200)
pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..)
err := this.pageTmp.Execute(w, pto)