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

111 lines
2.4 KiB
Go
Raw Normal View History

2018-04-28 22:02:36 +08:00
package serve
import (
"fmt"
"io"
"mime"
"net/http"
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"))
}
// Execute template
err = h.tplCache.Execute(w, &bookmarks[0])
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
}