feat: some updates
This commit is contained in:
@@ -6,6 +6,17 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var textPlainExtensions = map[string]bool{
|
||||
".txt": true,
|
||||
".log": true,
|
||||
".json": true,
|
||||
".yaml": true,
|
||||
".yml": true,
|
||||
".toml": true,
|
||||
".xml": true,
|
||||
".csv": true,
|
||||
}
|
||||
|
||||
func ContentTypeFor(name string) string {
|
||||
ext := strings.ToLower(filepath.Ext(name))
|
||||
switch ext {
|
||||
@@ -15,8 +26,7 @@ func ContentTypeFor(name string) string {
|
||||
if ct := mime.TypeByExtension(ext); ct != "" {
|
||||
return ct
|
||||
}
|
||||
switch ext {
|
||||
case ".txt", ".log", ".json", ".yaml", ".yml", ".toml", ".xml", ".csv":
|
||||
if textPlainExtensions[ext] {
|
||||
return "text/plain; charset=utf-8"
|
||||
}
|
||||
return "application/octet-stream"
|
||||
|
@@ -12,7 +12,7 @@ func CleanURLLike(p string) string {
|
||||
return "/"
|
||||
}
|
||||
parts := []string{}
|
||||
for _, seg := range strings.Split(strings.ReplaceAll(p, "\\", "/"), "/") {
|
||||
for seg := range strings.SplitSeq(strings.ReplaceAll(p, "/", "/"), "/") {
|
||||
switch seg {
|
||||
case "", ".":
|
||||
continue
|
||||
@@ -29,8 +29,8 @@ func CleanURLLike(p string) string {
|
||||
|
||||
func SafeRel(root, requested string) (string, error) {
|
||||
s := CleanURLLike(requested)
|
||||
if strings.HasPrefix(s, "/") {
|
||||
s = strings.TrimPrefix(s, "/")
|
||||
if after, ok := strings.CutPrefix(s, "/"); ok {
|
||||
s = after
|
||||
}
|
||||
full := filepath.Join(root, filepath.FromSlash(s))
|
||||
rel, err := filepath.Rel(root, full)
|
||||
|
@@ -3,14 +3,9 @@ package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.max-richter.dev/max/marka/parser"
|
||||
"git.max-richter.dev/max/marka/server/internal/fsx"
|
||||
"git.max-richter.dev/max/marka/server/internal/httpx"
|
||||
)
|
||||
@@ -41,131 +36,3 @@ func (h *File) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func read(path string) any {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
res, err := parser.ParseFile(string(data))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (h *File) get(w http.ResponseWriter, r *http.Request, target string) {
|
||||
fi, err := os.Stat(target)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
httpx.WriteError(w, http.StatusNotFound, errors.New("not found"))
|
||||
} else {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
entries, err := os.ReadDir(target)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
type item struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Content any `json:"content"`
|
||||
IsDir bool `json:"isDir"`
|
||||
Size int64 `json:"size"`
|
||||
ModTime time.Time `json:"modTime"`
|
||||
}
|
||||
out := make([]item, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info()
|
||||
out = append(out, item{
|
||||
Name: e.Name(),
|
||||
Path: fsx.ResponsePath(h.root, filepath.Join(target, e.Name())),
|
||||
IsDir: e.IsDir(),
|
||||
Content: read(filepath.Join(target, e.Name())),
|
||||
Size: sizeOrZero(info),
|
||||
ModTime: modTimeOrZero(info),
|
||||
})
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, out)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(target)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
contentType := fsx.ContentTypeFor(target)
|
||||
|
||||
if contentType == "application/markdown" {
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
res, err := parser.ParseFile(string(data))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
httpx.WriteJSON(w, http.StatusOK, res)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
http.ServeContent(w, r, filepath.Base(target), fi.ModTime(), f)
|
||||
}
|
||||
|
||||
func (h *File) post(w http.ResponseWriter, r *http.Request, target string) {
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
tmp := target + ".tmp~"
|
||||
f, err := os.Create(tmp)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := io.Copy(f, r.Body); err != nil {
|
||||
_ = f.Close()
|
||||
_ = os.Remove(tmp)
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
_ = os.Remove(tmp)
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if err := os.Rename(tmp, target); err != nil {
|
||||
_ = os.Remove(tmp)
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Location", r.URL.Path)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func sizeOrZero(fi os.FileInfo) int64 {
|
||||
if fi == nil {
|
||||
return 0
|
||||
}
|
||||
return fi.Size()
|
||||
}
|
||||
|
||||
func modTimeOrZero(fi os.FileInfo) time.Time {
|
||||
if fi == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return fi.ModTime()
|
||||
}
|
||||
|
93
server/internal/handlers/get.go
Normal file
93
server/internal/handlers/get.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.max-richter.dev/max/marka/server/internal/fsx"
|
||||
"git.max-richter.dev/max/marka/server/internal/httpx"
|
||||
)
|
||||
|
||||
func (h *File) get(w http.ResponseWriter, r *http.Request, target string) {
|
||||
fi, err := os.Stat(target)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
httpx.WriteError(w, http.StatusNotFound, errors.New("not found"))
|
||||
} else {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
h.handleDir(w, r, target)
|
||||
} else {
|
||||
h.handleFile(w, r, target, fi)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *File) handleDir(w http.ResponseWriter, r *http.Request, dirPath string) {
|
||||
entries, err := os.ReadDir(dirPath)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
type item struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Content any `json:"content,omitempty"`
|
||||
IsDir bool `json:"isDir"`
|
||||
Size int64 `json:"size"`
|
||||
ModTime time.Time `json:"modTime"`
|
||||
}
|
||||
|
||||
out := make([]item, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info() // As in original, ignore error to get partial info
|
||||
entryPath := filepath.Join(dirPath, e.Name())
|
||||
|
||||
var content any
|
||||
if !e.IsDir() && fsx.ContentTypeFor(e.Name()) == "application/markdown" {
|
||||
content, _ = parseMarkdownFile(entryPath) // As in original, ignore error
|
||||
}
|
||||
|
||||
out = append(out, item{
|
||||
Name: e.Name(),
|
||||
Path: fsx.ResponsePath(h.root, entryPath),
|
||||
IsDir: e.IsDir(),
|
||||
Content: content,
|
||||
Size: sizeOrZero(info),
|
||||
ModTime: modTimeOrZero(info),
|
||||
})
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (h *File) handleFile(w http.ResponseWriter, r *http.Request, target string, fi os.FileInfo) {
|
||||
contentType := fsx.ContentTypeFor(target)
|
||||
|
||||
if contentType == "application/markdown" {
|
||||
res, err := parseMarkdownFile(target)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, fmt.Errorf("failed to parse markdown: %w", err))
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, res)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(target)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
http.ServeContent(w, r, filepath.Base(target), fi.ModTime(), f)
|
||||
}
|
30
server/internal/handlers/helpers.go
Normal file
30
server/internal/handlers/helpers.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.max-richter.dev/max/marka/parser"
|
||||
)
|
||||
|
||||
func parseMarkdownFile(path string) (any, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parser.ParseFile(string(data))
|
||||
}
|
||||
|
||||
func sizeOrZero(fi os.FileInfo) int64 {
|
||||
if fi == nil {
|
||||
return 0
|
||||
}
|
||||
return fi.Size()
|
||||
}
|
||||
|
||||
func modTimeOrZero(fi os.FileInfo) time.Time {
|
||||
if fi == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return fi.ModTime()
|
||||
}
|
47
server/internal/handlers/post.go
Normal file
47
server/internal/handlers/post.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.max-richter.dev/max/marka/server/internal/httpx"
|
||||
)
|
||||
|
||||
func (h *File) post(w http.ResponseWriter, r *http.Request, target string) {
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
tmpPath := target + ".tmp~"
|
||||
f, err := os.Create(tmpPath)
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, copyErr := io.Copy(f, r.Body)
|
||||
closeErr := f.Close()
|
||||
|
||||
if copyErr != nil {
|
||||
os.Remove(tmpPath)
|
||||
httpx.WriteError(w, http.StatusInternalServerError, copyErr)
|
||||
return
|
||||
}
|
||||
if closeErr != nil {
|
||||
os.Remove(tmpPath)
|
||||
httpx.WriteError(w, http.StatusInternalServerError, closeErr)
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.Rename(tmpPath, target); err != nil {
|
||||
os.Remove(tmpPath)
|
||||
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Location", r.URL.Path)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
Reference in New Issue
Block a user