shiori/cmd/serve/web-handler-ui.go

136 lines
2.8 KiB
Go
Raw Normal View History

2018-04-28 22:02:36 +08:00
package serve
import (
2018-05-22 21:13:28 +08:00
"encoding/json"
2018-04-28 22:02:36 +08:00
"fmt"
2018-05-22 21:13:28 +08:00
"html/template"
2018-04-28 22:02:36 +08:00
"io"
"mime"
"net/http"
2018-05-22 21:13:28 +08:00
nurl "net/url"
2018-05-18 17:18:38 +08:00
"os"
2018-04-28 22:02:36 +08:00
fp "path/filepath"
2018-05-19 23:43:15 +08:00
"strconv"
2018-04-28 22:02:36 +08:00
"github.com/julienschmidt/httprouter"
)
// serveFiles serve files
func (h *webHandler) serveFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
err := serveFile(w, r.URL.Path)
checkError(err)
}
// serveIndexPage is handler for GET /
func (h *webHandler) serveIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := h.checkToken(r)
if err != nil {
redirectPage(w, r, "/login")
return
}
err = serveFile(w, "index.html")
checkError(err)
}
// serveLoginPage is handler for GET /login
func (h *webHandler) serveLoginPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Check token
err := h.checkToken(r)
if err == nil {
redirectPage(w, r, "/")
return
}
err = serveFile(w, "login.html")
checkError(err)
}
// serveBookmarkCache is handler for GET /bookmark/:id
func (h *webHandler) serveBookmarkCache(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Get bookmark ID from URL
2018-05-19 23:43:15 +08:00
strID := ps.ByName("id")
id, err := strconv.Atoi(strID)
checkError(err)
2018-04-28 22:02:36 +08:00
// Get bookmarks in database
bookmarks, err := h.db.GetBookmarks(true, id)
checkError(err)
if len(bookmarks) == 0 {
panic(fmt.Errorf("No bookmark with matching index"))
}
2018-05-22 21:13:28 +08:00
// Create template
funcMap := template.FuncMap{
"html": func(s string) template.HTML {
return template.HTML(s)
},
"hostname": func(s string) string {
parsed, err := nurl.ParseRequestURI(s)
if err != nil || parsed.Host == "" {
return s
}
return parsed.Hostname()
},
}
tplCache, err := createTemplate("cache.html", funcMap)
checkError(err)
bt, err := json.Marshal(&bookmarks[0])
checkError(err)
2018-04-28 22:02:36 +08:00
// Execute template
2018-05-22 21:13:28 +08:00
strBt := string(bt)
err = tplCache.Execute(w, &strBt)
2018-04-28 22:02:36 +08:00
checkError(err)
}
2018-05-18 17:18:38 +08:00
// serveThumbnailImage is handler for GET /thumb/:id
func (h *webHandler) serveThumbnailImage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Get bookmark ID from URL
id := ps.ByName("id")
// Open image
imgPath := fp.Join(h.dataDir, "thumb", id)
img, err := os.Open(imgPath)
checkError(err)
defer img.Close()
// Get image type from its 512 first bytes
buffer := make([]byte, 512)
_, err = img.Read(buffer)
checkError(err)
mimeType := http.DetectContentType(buffer)
w.Header().Set("Content-Type", mimeType)
// Serve image
img.Seek(0, 0)
_, err = io.Copy(w, img)
checkError(err)
}
2018-04-28 22:02:36 +08:00
func serveFile(w http.ResponseWriter, path string) error {
// Open file
src, err := assets.Open(path)
if err != nil {
return err
}
defer src.Close()
// Get content type
ext := fp.Ext(path)
mimeType := mime.TypeByExtension(ext)
if mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
// Serve file
_, err = io.Copy(w, src)
return err
}