mirror of
https://github.com/mattermost/focalboard.git
synced 2024-12-24 13:43:12 +02:00
Removing entirely global variables
This commit is contained in:
parent
0cdde971c0
commit
5fc5281967
@ -6,9 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@ -223,8 +221,7 @@ func (a *API) handleServeFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
|
||||
folderPath := config.FilesPath
|
||||
filePath := filepath.Join(folderPath, filename)
|
||||
filePath := a.app().GetFilePath(filename)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
@ -239,33 +236,11 @@ func (a *API) handleUploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Printf(`handleUploadFile, filename: %s`, handle.Filename)
|
||||
|
||||
saveFile(w, file, handle)
|
||||
}
|
||||
|
||||
func saveFile(w http.ResponseWriter, file multipart.File, handle *multipart.FileHeader) {
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// NOTE: File extension includes the dot
|
||||
fileExtension := strings.ToLower(filepath.Ext(handle.Filename))
|
||||
if fileExtension == ".jpeg" {
|
||||
fileExtension = ".jpg"
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf(`%s%s`, createGUID(), fileExtension)
|
||||
|
||||
folderPath := config.FilesPath
|
||||
filePath := filepath.Join(folderPath, filename)
|
||||
os.MkdirAll(folderPath, os.ModePerm)
|
||||
err = ioutil.WriteFile(filePath, data, 0666)
|
||||
url, err := a.app().SaveFile(file, handle.Filename)
|
||||
if err != nil {
|
||||
jsonStringResponse(w, http.StatusInternalServerError, `{}`)
|
||||
return
|
||||
}
|
||||
url := fmt.Sprintf(`%s/files/%s`, config.ServerRoot, filename)
|
||||
log.Printf(`saveFile, url: %s`, url)
|
||||
json := fmt.Sprintf(`{ "url": "%s" }`, url)
|
||||
jsonStringResponse(w, http.StatusOK, json)
|
||||
|
@ -1,6 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
config *Configuration
|
||||
store *SQLStore
|
||||
wsServer *WSServer
|
||||
}
|
||||
@ -41,7 +51,7 @@ func (a *App) InsertBlocks(blocks []Block) error {
|
||||
}
|
||||
}
|
||||
|
||||
wsServer.broadcastBlockChangeToWebsocketClients(blockIDsToNotify)
|
||||
a.wsServer.broadcastBlockChangeToWebsocketClients(blockIDsToNotify)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -72,3 +82,32 @@ func (a *App) DeleteBlock(blockID string) error {
|
||||
a.wsServer.broadcastBlockChangeToWebsocketClients(blockIDsToNotify)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) SaveFile(reader io.Reader, filename string) (string, error) {
|
||||
data, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// NOTE: File extension includes the dot
|
||||
fileExtension := strings.ToLower(filepath.Ext(filename))
|
||||
if fileExtension == ".jpeg" {
|
||||
fileExtension = ".jpg"
|
||||
}
|
||||
|
||||
createdFilename := fmt.Sprintf(`%s%s`, createGUID(), fileExtension)
|
||||
|
||||
folderPath := a.config.FilesPath
|
||||
filePath := filepath.Join(folderPath, createdFilename)
|
||||
os.MkdirAll(folderPath, os.ModePerm)
|
||||
err = ioutil.WriteFile(filePath, data, 0666)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf(`%s/files/%s`, a.config.ServerRoot, createdFilename), nil
|
||||
}
|
||||
|
||||
func (a *App) GetFilePath(filename string) string {
|
||||
folderPath := a.config.FilesPath
|
||||
return filepath.Join(folderPath, filename)
|
||||
}
|
||||
|
@ -9,12 +9,6 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var config *Configuration
|
||||
var wsServer *WSServer
|
||||
var webServer *WebServer
|
||||
var api *API
|
||||
var store *SQLStore
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// WebSocket OnChange listener
|
||||
|
||||
@ -47,8 +41,7 @@ func monitorPid(pid int) {
|
||||
|
||||
func main() {
|
||||
// config.json file
|
||||
var err error
|
||||
config, err = readConfigFile()
|
||||
config, err := readConfigFile()
|
||||
if err != nil {
|
||||
log.Fatal("Unable to read the config file: ", err)
|
||||
return
|
||||
|
@ -20,12 +20,12 @@ func NewServer(config *Configuration) (*Server, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wsServer = NewWSServer()
|
||||
wsServer := NewWSServer()
|
||||
|
||||
appBuilder := func() *App { return &App{store: store, wsServer: wsServer} }
|
||||
appBuilder := func() *App { return &App{config: config, store: store, wsServer: wsServer} }
|
||||
|
||||
webServer = NewWebServer(config.WebPath, config.Port, config.UseSSL)
|
||||
api = NewAPI(appBuilder)
|
||||
webServer := NewWebServer(config.WebPath, config.Port, config.UseSSL)
|
||||
api := NewAPI(appBuilder)
|
||||
webServer.AddRoutes(wsServer)
|
||||
webServer.AddRoutes(api)
|
||||
|
||||
@ -51,7 +51,7 @@ func NewServer(config *Configuration) (*Server, error) {
|
||||
}
|
||||
|
||||
func (s *Server) Start() error {
|
||||
if err := webServer.Start(); err != nil {
|
||||
if err := s.webServer.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func (ws *WSServer) RegisterRoutes(r *mux.Router) {
|
||||
r.HandleFunc("/ws/onchange", wsServer.handleWebSocketOnChange)
|
||||
r.HandleFunc("/ws/onchange", ws.handleWebSocketOnChange)
|
||||
}
|
||||
|
||||
// AddListener adds a listener for a blockID's change
|
||||
|
Loading…
Reference in New Issue
Block a user