mirror of
https://github.com/maputnik/editor.git
synced 2026-08-25 14:37:27 +00:00
Support saving file
This commit is contained in:
@@ -2,48 +2,77 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StyleFileAccessor(filename string) styleFileAccessor {
|
func StyleFileAccessor(filename string) styleFileAccessor {
|
||||||
file, err := os.Open(filename)
|
file, err := os.OpenFile(filename, os.O_RDWR, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Can not access style file: %s", err.Error())
|
log.Fatalf("Can not access style file: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return styleFileAccessor{file}
|
return styleFileAccessor{file, styleId(file)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func styleId(file *os.File) string {
|
||||||
|
raw, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var spec styleSpec
|
||||||
|
err = json.Unmarshal(raw, &spec)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if spec.Id == "" {
|
||||||
|
fmt.Println("No id in style")
|
||||||
|
}
|
||||||
|
return spec.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
type styleSpec struct {
|
||||||
|
Id string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allows access to a single style file
|
// Allows access to a single style file
|
||||||
type styleFileAccessor struct {
|
type styleFileAccessor struct {
|
||||||
file *os.File
|
file *os.File
|
||||||
|
id string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fa styleFileAccessor) ListFiles(w http.ResponseWriter, r *http.Request) {
|
func (fa styleFileAccessor) ListFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
encoder.Encode([]string{filepath.Base(fa.file.Name())})
|
encoder.Encode([]string{fa.id})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fa styleFileAccessor) ReadFile(w http.ResponseWriter, r *http.Request) {
|
func (fa styleFileAccessor) ReadFile(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
_ = vars["filename"]
|
_ = vars["styleId"]
|
||||||
|
|
||||||
//TODO: Choose right file
|
//TODO: Choose right file
|
||||||
// right now we just return the single file we know of
|
// right now we just return the single file we know of
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
fa.file.Seek(0, 0)
|
||||||
if _, err := io.Copy(w, fa.file); err != nil {
|
if _, err := io.Copy(w, fa.file); err != nil {
|
||||||
log.Fatalf("Can not copy from file to request: %s", err.Error())
|
log.Fatalf("Can not copy from file to request: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fa styleFileAccessor) SaveFile(w http.ResponseWriter, r *http.Request) {
|
func (fa styleFileAccessor) SaveFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
_ = vars["styleId"]
|
||||||
|
|
||||||
|
//TODO: Save to right file
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if _, err := io.Copy(fa.file, r.Body); err != nil {
|
if _, err := io.Copy(fa.file, r.Body); err != nil {
|
||||||
|
|||||||
+3
-3
@@ -39,9 +39,9 @@ func main() {
|
|||||||
// Allow access to reading and writing file on the local system
|
// Allow access to reading and writing file on the local system
|
||||||
path, _ := filepath.Abs(filename)
|
path, _ := filepath.Abs(filename)
|
||||||
accessor := StyleFileAccessor(path)
|
accessor := StyleFileAccessor(path)
|
||||||
router.Path("/files").Methods("GET").HandlerFunc(accessor.ListFiles)
|
router.Path("/styles").Methods("GET").HandlerFunc(accessor.ListFiles)
|
||||||
router.Path("/files/{filename}").Methods("GET").HandlerFunc(accessor.ReadFile)
|
router.Path("/styles/{styleId}").Methods("GET").HandlerFunc(accessor.ReadFile)
|
||||||
router.Path("/files/{filename}").Methods("PUT").HandlerFunc(accessor.SaveFile)
|
router.Path("/styles/{styleId}").Methods("PUT").HandlerFunc(accessor.SaveFile)
|
||||||
|
|
||||||
// Register websocket to notify we clients about file changes
|
// Register websocket to notify we clients about file changes
|
||||||
if c.Bool("watch") {
|
if c.Bool("watch") {
|
||||||
|
|||||||
Reference in New Issue
Block a user