[ci skip] updates on restructuring stuff
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/config"
|
||||
"github.com/hacdias/caddy-hugo/utils"
|
||||
)
|
||||
|
||||
// DELETE handles the delete requests on browse pages
|
||||
@@ -16,25 +17,26 @@ func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, erro
|
||||
path = strings.TrimSuffix(path, "/")
|
||||
path = c.Path + path
|
||||
|
||||
message := "File deleted."
|
||||
|
||||
// Check if the file or directory exists
|
||||
if stat, err := os.Stat(path); err == nil {
|
||||
var err error
|
||||
// If it's dir, remove all of the content inside
|
||||
if stat.IsDir() {
|
||||
err = os.RemoveAll(path)
|
||||
message = "Folder deleted."
|
||||
} else {
|
||||
err = os.Remove(path)
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if err != nil {
|
||||
return 500, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, nil)
|
||||
}
|
||||
} else {
|
||||
return 404, nil
|
||||
return utils.RespondJSON(w, "File not found.", 404, nil)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
return 200, nil
|
||||
return utils.RespondJSON(w, message, 200, nil)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package browse
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
@@ -38,11 +36,11 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
// Check if filename and archetype are specified in
|
||||
// the request
|
||||
if _, ok := info["filename"]; !ok {
|
||||
return http.StatusBadRequest, errors.New("Filename not specified.")
|
||||
return utils.RespondJSON(w, "Filename not specified.", 500, nil)
|
||||
}
|
||||
|
||||
if _, ok := info["archetype"]; !ok {
|
||||
return http.StatusBadRequest, errors.New("Archtype not specified.")
|
||||
return utils.RespondJSON(w, "Archtype not specified.", 500, nil)
|
||||
}
|
||||
|
||||
// Sanitize the file name path
|
||||
@@ -64,7 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
}
|
||||
|
||||
if err := utils.RunCommand(c.Hugo, args, c.Path); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
@@ -79,23 +77,22 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fmt.Println(url)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte("{\"Location\": \"" + url + "\"}"))
|
||||
return http.StatusOK, nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||
// Parse the multipart form in the request
|
||||
err := r.ParseMultipartForm(100000)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
|
||||
// For each file header in the multipart form
|
||||
@@ -105,25 +102,24 @@ func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, erro
|
||||
// Open the first file
|
||||
var infile multipart.File
|
||||
if infile, err = hdr.Open(); nil != err {
|
||||
return http.StatusInternalServerError, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
|
||||
// Create the file
|
||||
var outfile *os.File
|
||||
if outfile, err = os.Create(c.Path + r.URL.Path + hdr.Filename); nil != err {
|
||||
return http.StatusInternalServerError, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
|
||||
// Copy the file content
|
||||
if _, err = io.Copy(outfile, infile); nil != err {
|
||||
return http.StatusInternalServerError, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
|
||||
defer outfile.Close()
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
return http.StatusOK, nil
|
||||
return utils.RespondJSON(w, "", 200, nil)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ package browse
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/config"
|
||||
"github.com/hacdias/caddy-hugo/utils"
|
||||
)
|
||||
|
||||
// PUT handles the HTTP PUT request for all /admin/browse related requests.
|
||||
@@ -31,7 +31,7 @@ func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
// Check if filename and archetype are specified in
|
||||
// the request
|
||||
if _, ok := info["filename"]; !ok {
|
||||
return 400, errors.New("Filename not specified.")
|
||||
return utils.RespondJSON(w, "Filename not specified.", 400, nil)
|
||||
}
|
||||
|
||||
// Sanitize the file name path
|
||||
@@ -42,10 +42,8 @@ func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||
|
||||
// Renames the file/folder
|
||||
if err := os.Rename(old, new); err != nil {
|
||||
return 500, err
|
||||
return utils.RespondJSON(w, "Something went wrong.", 500, err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
return 200, nil
|
||||
return utils.RespondJSON(w, "File renamed.", 200, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user