mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Add file save and delete api endpoints
This commit is contained in:
@@ -27,6 +27,8 @@ func main() {
|
|||||||
r.Route("/api/v1", func(r chi.Router) {
|
r.Route("/api/v1", func(r chi.Router) {
|
||||||
r.Get("/files", api.ListFiles(fs))
|
r.Get("/files", api.ListFiles(fs))
|
||||||
r.Get("/files/*", api.GetFileContent(fs))
|
r.Get("/files/*", api.GetFileContent(fs))
|
||||||
|
r.Post("/files/*", api.SaveFile(fs))
|
||||||
|
r.Delete("/files/*", api.DeleteFile(fs))
|
||||||
})
|
})
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -34,3 +35,37 @@ func GetFileContent(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
w.Write(content)
|
w.Write(content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
|
||||||
|
content, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fs.SaveFile(filePath, content)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("File saved successfully"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
filePath := strings.TrimPrefix(r.URL.Path, "/api/v1/files/")
|
||||||
|
err := fs.DeleteFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to delete file", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("File deleted successfully"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,3 +58,19 @@ func (fs *FileSystem) GetFileContent(filePath string) ([]byte, error) {
|
|||||||
fullPath := filepath.Join(fs.RootDir, filePath)
|
fullPath := filepath.Join(fs.RootDir, filePath)
|
||||||
return os.ReadFile(fullPath)
|
return os.ReadFile(fullPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fs *FileSystem) SaveFile(filePath string, content []byte) error {
|
||||||
|
fullPath := filepath.Join(fs.RootDir, filePath)
|
||||||
|
dir := filepath.Dir(fullPath)
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(fullPath, content, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fs *FileSystem) DeleteFile(filePath string) error {
|
||||||
|
fullPath := filepath.Join(fs.RootDir, filePath)
|
||||||
|
return os.Remove(fullPath)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user