2020-10-16 19:12:53 +02:00
|
|
|
package api
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-16 16:21:42 +02:00
|
|
|
"context"
|
2020-10-16 11:41:56 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2020-11-12 19:16:59 +01:00
|
|
|
"strconv"
|
2020-10-16 11:41:56 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-10-16 19:12:53 +02:00
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/app"
|
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
2020-10-16 11:41:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
// REST APIs
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
type API struct {
|
2020-10-16 19:12:53 +02:00
|
|
|
appBuilder func() *app.App
|
2020-12-04 11:28:35 +01:00
|
|
|
singleUser bool
|
2020-10-16 16:21:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:28:35 +01:00
|
|
|
func NewAPI(appBuilder func() *app.App, singleUser bool) *API {
|
|
|
|
return &API{appBuilder: appBuilder, singleUser: singleUser}
|
2020-10-16 16:21:42 +02:00
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
func (a *API) app() *app.App {
|
2020-10-16 16:21:42 +02:00
|
|
|
return a.appBuilder()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) RegisterRoutes(r *mux.Router) {
|
2020-12-02 21:12:14 +01:00
|
|
|
r.HandleFunc("/api/v1/blocks", a.sessionRequired(a.handleGetBlocks)).Methods("GET")
|
2020-10-16 11:41:56 +02:00
|
|
|
r.HandleFunc("/api/v1/blocks", a.handlePostBlocks).Methods("POST")
|
|
|
|
r.HandleFunc("/api/v1/blocks/{blockID}", a.handleDeleteBlock).Methods("DELETE")
|
|
|
|
r.HandleFunc("/api/v1/blocks/{blockID}/subtree", a.handleGetSubTree).Methods("GET")
|
|
|
|
|
2020-11-06 16:46:35 +01:00
|
|
|
r.HandleFunc("/api/v1/login", a.handleLogin).Methods("POST")
|
|
|
|
r.HandleFunc("/api/v1/register", a.handleRegister).Methods("POST")
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
r.HandleFunc("/api/v1/files", a.handleUploadFile).Methods("POST")
|
|
|
|
r.HandleFunc("/files/{filename}", a.handleServeFile).Methods("GET")
|
|
|
|
|
|
|
|
r.HandleFunc("/api/v1/blocks/export", a.handleExport).Methods("GET")
|
|
|
|
r.HandleFunc("/api/v1/blocks/import", a.handleImport).Methods("POST")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetBlocks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
|
|
|
parentID := query.Get("parent_id")
|
|
|
|
blockType := query.Get("type")
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
blocks, err := a.app().GetBlocks(parentID, blockType)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR GetBlocks: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("GetBlocks parentID: %s, type: %s, %d result(s)", parentID, blockType, len(blocks))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
json, err := json.Marshal(blocks)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR json.Marshal: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handlePostBlocks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch panics from parse errors, etc.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Printf(`ERROR: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
var blocks []model.Block
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
err = json.Unmarshal(requestBody, &blocks)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
// Error checking
|
|
|
|
if len(block.Type) < 1 {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorData := map[string]string{"description": "missing type", "id": block.ID}
|
|
|
|
errorResponse(w, http.StatusBadRequest, errorData)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
if block.CreateAt < 1 {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorData := map[string]string{"description": "invalid createAt", "id": block.ID}
|
|
|
|
errorResponse(w, http.StatusBadRequest, errorData)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
if block.UpdateAt < 1 {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorData := map[string]string{"description": "invalid UpdateAt", "id": block.ID}
|
|
|
|
errorResponse(w, http.StatusBadRequest, errorData)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
err = a.app().InsertBlocks(blocks)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
log.Printf("POST Blocks %d block(s)", len(blocks))
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDeleteBlock(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
blockID := vars["blockID"]
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
err := a.app().DeleteBlock(blockID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("DELETE Block %s", blockID)
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetSubTree(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
blockID := vars["blockID"]
|
|
|
|
|
2020-11-12 19:16:59 +01:00
|
|
|
query := r.URL.Query()
|
|
|
|
levels, err := strconv.ParseInt(query.Get("l"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
levels = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if levels != 2 && levels != 3 {
|
|
|
|
log.Printf(`ERROR Invalid levels: %d`, levels)
|
|
|
|
errorData := map[string]string{"description": "invalid levels"}
|
|
|
|
errorResponse(w, http.StatusInternalServerError, errorData)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks, err := a.app().GetSubTree(blockID, int(levels))
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2020-11-12 19:16:59 +01:00
|
|
|
log.Printf("GetSubTree (%v) blockID: %s, %d result(s)", levels, blockID, len(blocks))
|
2020-10-16 11:41:56 +02:00
|
|
|
json, err := json.Marshal(blocks)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR json.Marshal: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleExport(w http.ResponseWriter, r *http.Request) {
|
2020-10-16 16:21:42 +02:00
|
|
|
blocks, err := a.app().GetAllBlocks()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
log.Printf("EXPORT Blocks, %d result(s)", len(blocks))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
json, err := json.Marshal(blocks)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(`ERROR json.Marshal: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleImport(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch panics from parse errors, etc.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Printf(`ERROR: %v`, r)
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
var blocks []model.Block
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
err = json.Unmarshal(requestBody, &blocks)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
2020-10-22 15:22:36 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-26 18:54:37 +01:00
|
|
|
err = a.app().InsertBlocks(blocks)
|
|
|
|
if err != nil {
|
2020-12-03 23:09:48 +01:00
|
|
|
log.Printf(`ERROR: %v`, err)
|
2020-10-26 18:54:37 +01:00
|
|
|
errorResponse(w, http.StatusInternalServerError, nil)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-26 18:54:37 +01:00
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("IMPORT Blocks %d block(s)", len(blocks))
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
}
|
|
|
|
|
|
|
|
// File upload
|
|
|
|
|
|
|
|
func (a *API) handleServeFile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
filename := vars["filename"]
|
|
|
|
|
|
|
|
contentType := "image/jpg"
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
fileExtension := strings.ToLower(filepath.Ext(filename))
|
|
|
|
if fileExtension == "png" {
|
|
|
|
contentType = "image/png"
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
2020-10-16 16:36:08 +02:00
|
|
|
filePath := a.app().GetFilePath(filename)
|
2020-10-16 11:41:56 +02:00
|
|
|
http.ServeFile(w, r, filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleUploadFile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Println(`handleUploadFile`)
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
file, handle, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "%v", err)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
log.Printf(`handleUploadFile, filename: %s`, handle.Filename)
|
|
|
|
|
2020-10-16 16:36:08 +02:00
|
|
|
url, err := a.app().SaveFile(file, handle.Filename)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
|
|
|
jsonStringResponse(w, http.StatusInternalServerError, `{}`)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
log.Printf(`saveFile, url: %s`, url)
|
|
|
|
json := fmt.Sprintf(`{ "url": "%s" }`, url)
|
|
|
|
jsonStringResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response helpers
|
|
|
|
|
|
|
|
func jsonStringResponse(w http.ResponseWriter, code int, message string) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
fmt.Fprint(w, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonBytesResponse(w http.ResponseWriter, code int, json []byte) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write(json)
|
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
func errorResponse(w http.ResponseWriter, code int, message map[string]string) {
|
2020-10-16 11:41:56 +02:00
|
|
|
log.Printf("%d ERROR", code)
|
2020-11-17 15:43:56 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-10-22 15:22:36 +02:00
|
|
|
data, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
data = []byte("{}")
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
w.WriteHeader(code)
|
2020-11-12 19:16:59 +01:00
|
|
|
w.Write(data)
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
2020-10-16 16:21:42 +02:00
|
|
|
|
|
|
|
func addUserID(rw http.ResponseWriter, req *http.Request, next http.Handler) {
|
|
|
|
ctx := context.WithValue(req.Context(), "userid", req.Header.Get("userid"))
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
next.ServeHTTP(rw, req)
|
|
|
|
}
|