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"
|
2021-03-26 20:01:54 +02:00
|
|
|
"errors"
|
2020-10-16 11:41:56 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2020-11-12 20:16:59 +02:00
|
|
|
"strconv"
|
2020-10-16 11:41:56 +02:00
|
|
|
"strings"
|
2020-12-07 21:40:16 +02:00
|
|
|
"time"
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-01-27 00:13:46 +02:00
|
|
|
"github.com/mattermost/focalboard/server/app"
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-03-26 20:01:54 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2021-01-27 00:13:46 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2020-10-16 11:41:56 +02:00
|
|
|
)
|
|
|
|
|
2021-02-03 02:54:15 +02:00
|
|
|
const (
|
|
|
|
HEADER_REQUESTED_WITH = "X-Requested-With"
|
|
|
|
HEADER_REQUESTED_WITH_XML = "XMLHttpRequest"
|
|
|
|
)
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
const (
|
|
|
|
ERROR_NO_WORKSPACE_CODE = 1000
|
|
|
|
ERROR_NO_WORKSPACE_MESSAGE = "No workspace"
|
|
|
|
)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
// REST APIs
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
type WorkspaceAuthenticator interface {
|
|
|
|
DoesUserHaveWorkspaceAccess(session *model.Session, workspaceID string) bool
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
type API struct {
|
2021-03-26 20:01:54 +02:00
|
|
|
appBuilder func() *app.App
|
|
|
|
authService string
|
|
|
|
singleUserToken string
|
|
|
|
WorkspaceAuthenticator WorkspaceAuthenticator
|
2020-10-16 16:21:42 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func NewAPI(appBuilder func() *app.App, singleUserToken string, authService string) *API {
|
2021-02-09 22:27:34 +02:00
|
|
|
return &API{
|
|
|
|
appBuilder: appBuilder,
|
|
|
|
singleUserToken: singleUserToken,
|
2021-03-26 20:01:54 +02:00
|
|
|
authService: authService,
|
2021-02-09 22:27:34 +02:00
|
|
|
}
|
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) {
|
2021-02-05 20:28:52 +02:00
|
|
|
apiv1 := r.PathPrefix("/api/v1").Subrouter()
|
|
|
|
apiv1.Use(a.requireCSRFToken)
|
2020-12-07 21:40:16 +02:00
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/blocks", a.sessionRequired(a.handleGetBlocks)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/blocks", a.sessionRequired(a.handlePostBlocks)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/blocks/{blockID}", a.sessionRequired(a.handleDeleteBlock)).Methods("DELETE")
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/blocks/{blockID}/subtree", a.attachSession(a.handleGetSubTree, false)).Methods("GET")
|
|
|
|
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/blocks/export", a.sessionRequired(a.handleExport)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/blocks/import", a.sessionRequired(a.handleImport)).Methods("POST")
|
|
|
|
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/sharing/{rootID}", a.sessionRequired(a.handlePostSharing)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/sharing/{rootID}", a.sessionRequired(a.handleGetSharing)).Methods("GET")
|
|
|
|
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}", a.sessionRequired(a.handleGetWorkspace)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/workspaces/{workspaceID}/regenerate_signup_token", a.sessionRequired(a.handlePostWorkspaceRegenerateSignupToken)).Methods("POST")
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
// User APIs
|
2021-02-05 20:28:52 +02:00
|
|
|
apiv1.HandleFunc("/users/me", a.sessionRequired(a.handleGetMe)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/users/{userID}", a.sessionRequired(a.handleGetUser)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/users/{userID}/changepassword", a.sessionRequired(a.handleChangePassword)).Methods("POST")
|
2020-11-06 17:46:35 +02:00
|
|
|
|
2021-02-05 20:28:52 +02:00
|
|
|
apiv1.HandleFunc("/login", a.handleLogin).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/register", a.handleRegister).Methods("POST")
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2021-02-05 20:45:28 +02:00
|
|
|
apiv1.HandleFunc("/files", a.sessionRequired(a.handleUploadFile)).Methods("POST")
|
|
|
|
|
|
|
|
// Get Files API
|
2021-02-05 20:28:52 +02:00
|
|
|
|
2021-02-05 21:22:56 +02:00
|
|
|
files := r.PathPrefix("/files").Subrouter()
|
2021-02-05 20:28:52 +02:00
|
|
|
files.HandleFunc("/{filename}", a.sessionRequired(a.handleServeFile)).Methods("GET")
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:52:25 +02:00
|
|
|
func (a *API) RegisterAdminRoutes(r *mux.Router) {
|
2021-01-23 00:14:12 +02:00
|
|
|
r.HandleFunc("/api/v1/admin/users/{username}/password", a.adminRequired(a.handleAdminSetPassword)).Methods("POST")
|
2021-01-20 23:52:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-05 20:28:52 +02:00
|
|
|
func (a *API) requireCSRFToken(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-02-03 02:54:15 +02:00
|
|
|
if !a.checkCSRFToken(r) {
|
|
|
|
log.Println("checkCSRFToken FAILED")
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusBadRequest, "", nil)
|
2021-02-03 02:54:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:28:52 +02:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-02-03 02:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) checkCSRFToken(r *http.Request) bool {
|
|
|
|
token := r.Header.Get(HEADER_REQUESTED_WITH)
|
|
|
|
|
|
|
|
if token == HEADER_REQUESTED_WITH_XML {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func (a *API) getContainer(r *http.Request) (*store.Container, error) {
|
|
|
|
if a.WorkspaceAuthenticator == nil {
|
|
|
|
// Native auth: always use root workspace
|
|
|
|
container := store.Container{
|
|
|
|
WorkspaceID: "",
|
|
|
|
}
|
|
|
|
return &container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
workspaceID := vars["workspaceID"]
|
|
|
|
|
|
|
|
if workspaceID == "" || workspaceID == "0" {
|
|
|
|
// When authenticating, a workspace is required
|
|
|
|
return nil, errors.New("No workspace specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value("session").(*model.Session)
|
|
|
|
if !a.WorkspaceAuthenticator.DoesUserHaveWorkspaceAccess(session, workspaceID) {
|
|
|
|
return nil, errors.New("Access denied to workspace")
|
|
|
|
}
|
|
|
|
|
|
|
|
container := store.Container{
|
|
|
|
WorkspaceID: workspaceID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &container, nil
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handleGetBlocks(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation GET /api/v1/workspaces/{workspaceID}/blocks getBlocks
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Returns blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: parent_id
|
|
|
|
// in: query
|
|
|
|
// description: ID of parent block, omit to specify all blocks
|
|
|
|
// required: false
|
|
|
|
// type: string
|
|
|
|
// - name: type
|
|
|
|
// in: query
|
|
|
|
// description: Type of blocks to return, omit to specify all types
|
|
|
|
// required: false
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
parentID := query.Get("parent_id")
|
|
|
|
blockType := query.Get("type")
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
blocks, err := a.app().GetBlocks(*container, parentID, blockType)
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 20:03:01 +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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
2021-01-12 04:53:08 +02:00
|
|
|
func stampModifiedByUser(r *http.Request, blocks []model.Block) {
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value("session").(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
if userID == "single-user" {
|
|
|
|
userID = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range blocks {
|
|
|
|
blocks[i].ModifiedBy = userID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handlePostBlocks(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation POST /api/v1/workspaces/{workspaceID}/blocks updateBlocks
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Insert or update blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: array of blocks to insert or update
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
// Error checking
|
|
|
|
if len(block.Type) < 1 {
|
2021-02-17 21:29:20 +02:00
|
|
|
message := fmt.Sprintf("missing type for block id %s", block.ID)
|
|
|
|
errorResponse(w, http.StatusBadRequest, message, nil)
|
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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
message := fmt.Sprintf("invalid createAt for block id %s", block.ID)
|
|
|
|
errorResponse(w, http.StatusBadRequest, message, nil)
|
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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
message := fmt.Sprintf("invalid UpdateAt for block id %s", block.ID)
|
|
|
|
errorResponse(w, http.StatusBadRequest, message, nil)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 04:53:08 +02:00
|
|
|
stampModifiedByUser(r, blocks)
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
err = a.app().InsertBlocks(*container, blocks)
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
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, "{}")
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:40:16 +02:00
|
|
|
func (a *API) handleGetUser(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 21:29:20 +02:00
|
|
|
// swagger:operation GET /api/v1/users/{userID} getUser
|
|
|
|
//
|
|
|
|
// Returns a user
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: userID
|
|
|
|
// in: path
|
|
|
|
// description: User ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-12-07 21:40:16 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
userID := vars["userID"]
|
|
|
|
|
|
|
|
user, err := a.app().GetUser(userID)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-12-07 21:40:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userData, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-12-07 21:40:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, userData)
|
2020-12-07 21:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetMe(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 21:29:20 +02:00
|
|
|
// swagger:operation GET /api/v1/users/me getMe
|
|
|
|
//
|
|
|
|
// Returns the currently logged-in user
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-12-07 21:40:16 +02:00
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value("session").(*model.Session)
|
|
|
|
var user *model.User
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if session.UserID == "single-user" {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
user = &model.User{
|
|
|
|
ID: "single-user",
|
|
|
|
Username: "single-user",
|
|
|
|
Email: "single-user",
|
|
|
|
CreateAt: now,
|
|
|
|
UpdateAt: now,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user, err = a.app().GetUser(session.UserID)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-12-07 21:40:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userData, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-12-07 21:40:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, userData)
|
2020-12-07 21:40:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handleDeleteBlock(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation DELETE /api/v1/workspaces/{workspaceID}/blocks/{blockID} deleteBlock
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Deletes a block
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: blockID
|
|
|
|
// in: path
|
|
|
|
// description: ID of block to delete
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-01-12 21:16:25 +02:00
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value("session").(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
blockID := vars["blockID"]
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.app().DeleteBlock(*container, blockID, userID)
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
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) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation GET /api/v1/workspaces/{workspaceID}/blocks/{blockID}/subtree getSubTree
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Returns the blocks of a subtree
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: blockID
|
|
|
|
// in: path
|
|
|
|
// description: The ID of the root block of the subtree
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: l
|
|
|
|
// in: query
|
|
|
|
// description: The number of levels to return. 2 or 3. Defaults to 2.
|
|
|
|
// required: false
|
|
|
|
// type: integer
|
|
|
|
// minimum: 2
|
|
|
|
// maximum: 3
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
blockID := vars["blockID"]
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 04:49:08 +02:00
|
|
|
// If not authenticated (no session), check that block is publicly shared
|
|
|
|
ctx := r.Context()
|
|
|
|
session, _ := ctx.Value("session").(*model.Session)
|
|
|
|
if session == nil {
|
2021-01-13 19:40:37 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
readToken := query.Get("read_token")
|
|
|
|
|
|
|
|
// Require read token
|
|
|
|
if len(readToken) < 1 {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusBadRequest, "No read_token", nil)
|
2021-01-13 19:40:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
isValid, err := a.app().IsValidReadToken(*container, blockID, readToken)
|
2021-01-13 04:49:08 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-13 04:49:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-03 04:15:03 +02:00
|
|
|
if !isValid {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusUnauthorized, "", nil)
|
2021-01-13 04:49:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:16:59 +02: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)
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusBadRequest, "invalid levels", nil)
|
2020-11-12 20:16:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
blocks, err := a.app().GetSubTree(*container, blockID, int(levels))
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2020-11-12 20:16:59 +02: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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleExport(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation GET /api/v1/workspaces/{workspaceID}/blocks/export exportBlocks
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Returns all blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2021-03-26 20:01:54 +02:00
|
|
|
// parameters:
|
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks, err := a.app().GetAllBlocks(*container)
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2020-12-10 22:39:09 +02:00
|
|
|
log.Printf("%d raw block(s)", len(blocks))
|
|
|
|
blocks = filterOrphanBlocks(blocks)
|
2020-12-14 21:24:38 +02:00
|
|
|
log.Printf("EXPORT %d filtered block(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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:39:09 +02:00
|
|
|
func filterOrphanBlocks(blocks []model.Block) (ret []model.Block) {
|
2020-12-14 21:24:38 +02:00
|
|
|
queue := make([]model.Block, 0)
|
2021-03-21 10:28:26 +02:00
|
|
|
childrenOfBlockWithID := make(map[string]*[]model.Block)
|
2020-12-14 21:24:38 +02:00
|
|
|
|
|
|
|
// Build the trees from nodes
|
2020-12-10 22:39:09 +02:00
|
|
|
for _, block := range blocks {
|
2020-12-14 21:24:38 +02:00
|
|
|
if len(block.ParentID) == 0 {
|
|
|
|
// Queue root blocks to process first
|
|
|
|
queue = append(queue, block)
|
|
|
|
} else {
|
|
|
|
siblings := childrenOfBlockWithID[block.ParentID]
|
|
|
|
if siblings != nil {
|
|
|
|
*siblings = append(*siblings, block)
|
|
|
|
} else {
|
|
|
|
siblings := []model.Block{block}
|
|
|
|
childrenOfBlockWithID[block.ParentID] = &siblings
|
|
|
|
}
|
2020-12-10 22:39:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 21:24:38 +02:00
|
|
|
|
|
|
|
// Map the trees to an array, which skips orphaned nodes
|
|
|
|
blocks = make([]model.Block, 0)
|
|
|
|
for len(queue) > 0 {
|
|
|
|
block := queue[0]
|
|
|
|
queue = queue[1:] // dequeue
|
|
|
|
blocks = append(blocks, block)
|
|
|
|
children := childrenOfBlockWithID[block.ID]
|
|
|
|
if children != nil {
|
|
|
|
queue = append(queue, (*children)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks
|
2020-12-10 22:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func arrayContainsBlockWithID(array []model.Block, blockID string) bool {
|
|
|
|
for _, item := range array {
|
|
|
|
if item.ID == blockID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handleImport(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation POST /api/v1/workspaces/{workspaceID}/blocks/import importBlocks
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Import blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: array of blocks to import
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
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 {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-12 04:53:08 +02:00
|
|
|
stampModifiedByUser(r, blocks)
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
err = a.app().InsertBlocks(*container, blocks)
|
2020-10-26 19:54:37 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-26 19:54:37 +02:00
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("IMPORT Blocks %d block(s)", len(blocks))
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:35:30 +02:00
|
|
|
// Sharing
|
|
|
|
|
|
|
|
func (a *API) handleGetSharing(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation GET /api/v1/workspaces/{workspaceID}/sharing/{rootID} getSharing
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Returns sharing information for a root block
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: rootID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the root block
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Sharing"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-01-13 01:35:30 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
rootID := vars["rootID"]
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sharing, err := a.app().GetSharing(*container, rootID)
|
2021-01-13 01:35:30 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-13 01:35:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sharingData, err := json.Marshal(sharing)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-13 01:35:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("GET sharing %s", rootID)
|
2021-02-17 21:29:20 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, sharingData)
|
2021-01-13 01:35:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handlePostSharing(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation POST /api/v1/workspaces/{workspaceID}/sharing/{rootID} postSharing
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Sets sharing information for a root block
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-03-26 20:01:54 +02:00
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// - name: rootID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the root block
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: sharing information for a root block
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Sharing"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
container, err := a.getContainer(r)
|
|
|
|
if err != nil {
|
|
|
|
noContainerErrorResponse(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:35:30 +02:00
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-13 01:35:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var sharing model.Sharing
|
|
|
|
|
|
|
|
err = json.Unmarshal(requestBody, &sharing)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-13 01:35:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stamp ModifiedBy
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value("session").(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
if userID == "single-user" {
|
|
|
|
userID = ""
|
|
|
|
}
|
|
|
|
sharing.ModifiedBy = userID
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
err = a.app().UpsertSharing(*container, sharing)
|
2021-01-13 01:35:30 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-13 01:35:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("POST sharing %s", sharing.ID)
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
}
|
|
|
|
|
2021-01-14 02:56:01 +02:00
|
|
|
// Workspace
|
|
|
|
|
|
|
|
func (a *API) handleGetWorkspace(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation GET /api/v1/workspaces/{workspaceID} getWorkspace
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Returns information of the root workspace
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2021-03-26 20:01:54 +02:00
|
|
|
// parameters:
|
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Workspace"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
var workspace *model.Workspace
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if a.WorkspaceAuthenticator != nil {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
workspaceID := vars["workspaceID"]
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value("session").(*model.Session)
|
|
|
|
if !a.WorkspaceAuthenticator.DoesUserHaveWorkspaceAccess(session, workspaceID) {
|
|
|
|
errorResponse(w, http.StatusUnauthorized, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
workspace = &model.Workspace{
|
|
|
|
ID: workspaceID,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
workspace, err = a.app().GetRootWorkspace()
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
2021-01-14 02:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
workspaceData, err := json.Marshal(workspace)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-14 02:56:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, workspaceData)
|
2021-01-14 02:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handlePostWorkspaceRegenerateSignupToken(w http.ResponseWriter, r *http.Request) {
|
2021-03-26 20:01:54 +02:00
|
|
|
// swagger:operation POST /api/v1/workspaces/{workspaceID}/regenerate_signup_token regenerateSignupToken
|
2021-02-17 21:29:20 +02:00
|
|
|
//
|
|
|
|
// Regenerates the signup token for the root workspace
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2021-03-26 20:01:54 +02:00
|
|
|
// parameters:
|
|
|
|
// - name: workspaceID
|
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 21:29:20 +02:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2021-01-14 02:56:01 +02:00
|
|
|
workspace, err := a.app().GetRootWorkspace()
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-14 02:56:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
workspace.SignupToken = utils.CreateGUID()
|
|
|
|
|
|
|
|
err = a.app().UpsertWorkspaceSignupToken(*workspace)
|
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2021-01-14 02:56:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
// File upload
|
|
|
|
|
|
|
|
func (a *API) handleServeFile(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 21:29:20 +02:00
|
|
|
// swagger:operation GET /files/{fileID} getFile
|
|
|
|
//
|
|
|
|
// Returns the contents of an uploaded file
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// - image/jpg
|
|
|
|
// - image/png
|
|
|
|
// parameters:
|
|
|
|
// - name: fileID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the file
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
// FileUploadResponse is the response to a file upload
|
|
|
|
// swagger:model
|
|
|
|
type FileUploadResponse struct {
|
2021-02-23 21:42:28 +02:00
|
|
|
// The FileID to retrieve the uploaded file
|
2021-02-17 21:29:20 +02:00
|
|
|
// required: true
|
2021-02-23 21:42:28 +02:00
|
|
|
FileID string `json:"fileId"`
|
2021-02-17 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handleUploadFile(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 21:29:20 +02:00
|
|
|
// swagger:operation POST /api/v1/files uploadFile
|
|
|
|
//
|
|
|
|
// Upload a binary file
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// consumes:
|
|
|
|
// - multipart/form-data
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: uploaded file
|
|
|
|
// in: formData
|
|
|
|
// type: file
|
|
|
|
// description: The file to upload
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/FileUploadResponse"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
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()
|
|
|
|
|
2021-02-23 21:42:28 +02:00
|
|
|
fileId, err := a.app().SaveFile(file, handle.Filename)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
2021-02-17 21:29:20 +02:00
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-02-23 21:42:28 +02:00
|
|
|
log.Printf("uploadFile, filename: %s, fileId: %s", handle.Filename, fileId)
|
|
|
|
data, err := json.Marshal(FileUploadResponse{FileID: fileId})
|
2021-02-17 21:29:20 +02:00
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
func errorResponse(w http.ResponseWriter, code int, message string, sourceError error) {
|
2021-01-14 20:03:01 +02:00
|
|
|
log.Printf("API ERROR %d, err: %v\n", code, sourceError)
|
2020-11-17 16:43:56 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-03-26 20:01:54 +02:00
|
|
|
data, err := json.Marshal(model.ErrorResponse{Error: message, ErrorCode: code})
|
2020-10-22 15:22:36 +02:00
|
|
|
if err != nil {
|
|
|
|
data = []byte("{}")
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
w.WriteHeader(code)
|
2020-11-12 20:16:59 +02:00
|
|
|
w.Write(data)
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
2020-10-16 16:21:42 +02:00
|
|
|
|
2021-03-26 20:01:54 +02:00
|
|
|
func errorResponseWithCode(w http.ResponseWriter, statusCode int, errorCode int, message string, sourceError error) {
|
|
|
|
log.Printf("API ERROR status %d, errorCode: %d, err: %v\n", statusCode, errorCode, sourceError)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
data, err := json.Marshal(model.ErrorResponse{Error: message, ErrorCode: errorCode})
|
|
|
|
if err != nil {
|
|
|
|
data = []byte("{}")
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func noContainerErrorResponse(w http.ResponseWriter, sourceError error) {
|
|
|
|
errorResponseWithCode(w, http.StatusBadRequest, ERROR_NO_WORKSPACE_CODE, ERROR_NO_WORKSPACE_MESSAGE, sourceError)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|