1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00

Implement showing actions

This commit is contained in:
Timur Ismagilov 2020-06-14 13:43:49 +05:00
parent 822a122f1d
commit aef4f1dd09
2 changed files with 54 additions and 23 deletions

View File

@ -3,6 +3,8 @@ package main
import (
"errors"
"fmt"
"log"
"net/http"
"strconv"
)
@ -38,21 +40,23 @@ func (h Hypha) String() string {
revbuf)
}
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string) (Revision, error) {
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string, w http.ResponseWriter) (Revision, bool) {
for name, _ := range hyphae {
if name == hyphaName {
for _, r := range hyphae[name].Revisions {
id, err := strconv.Atoi(rev)
if err != nil {
return Revision{}, err
log.Println("No such revision", rev, "at hypha", hyphaName)
w.WriteHeader(http.StatusNotFound)
return Revision{}, false
}
if r.Id == id {
return r, nil
return r, true
}
}
}
}
return Revision{}, errors.New("Some error idk")
return Revision{}, false
}
// `rev` is the id of revision to render. If it = 0, the last one is rendered. If the revision is not found, an error is returned.

65
main.go
View File

@ -23,46 +23,73 @@ func RevInMap(m map[string]string) string {
func HandlerGetBinary(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
revno := RevInMap(vars)
rev, err := GetRevision(hyphae, vars["hypha"], revno)
if err != nil {
log.Println("Failed to show image of", rev.FullName)
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
if !ok {
return
}
fileContents, err := ioutil.ReadFile(rev.BinaryPath)
if err != nil {
log.Println("Failed to show image of", rev.FullName)
log.Println("Failed to load binary data of", rev.FullName, rev.Id)
w.WriteHeader(http.StatusNotFound)
return
}
log.Println("Contents:", fileContents[:10], "...")
w.Header().Set("Content-Type", rev.MimeType)
// w.Header().Set("Content-Length", strconv.Itoa(len(fileContents)))
w.WriteHeader(http.StatusOK)
w.Write(fileContents)
log.Println("Showing image of", rev.FullName)
log.Println("Showing image of", rev.FullName, rev.Id)
}
func HandlerRaw(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
log.Println("Attempt to access an unimplemented thing")
vars := mux.Vars(r)
revno := RevInMap(vars)
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
if !ok {
return
}
fileContents, err := ioutil.ReadFile(rev.TextPath)
if err != nil {
log.Println("Failed to load text data of", rev.FullName, rev.Id)
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write(fileContents)
log.Println("Serving text data of", rev.FullName, rev.Id)
}
func HandlerZen(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
log.Println("Attempt to access an unimplemented thing")
}
func HandlerView(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
rev, err := GetRevision(hyphae, vars["hypha"], "0")
if err != nil {
log.Println("Failed to show image of", rev.FullName)
w.WriteHeader(http.StatusInternalServerError)
revno := RevInMap(vars)
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
if !ok {
return
}
html, err := rev.Render(hyphae)
if err != nil {
log.Println("Failed to show image of", rev.FullName)
log.Println("Failed to render", rev.FullName)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, html)
}
func HandlerView(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
revno := RevInMap(vars)
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
if !ok {
return
}
html, err := rev.Render(hyphae)
if err != nil {
log.Println("Failed to render", rev.FullName)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, HyphaPage(hyphae, rev, html))
log.Println("Rendering", rev.FullName)