[ci skip] updates on restructuring stuff
parent
b97ef80596
commit
e33c79224f
File diff suppressed because one or more lines are too long
|
@ -29,22 +29,24 @@ $(document).on('page:browse', function() {
|
|||
request.send();
|
||||
request.onreadystatechange = function() {
|
||||
if (request.readyState == 4) {
|
||||
if (request.status == 200) {
|
||||
$(foreground).fadeOut(200);
|
||||
remove.form.fadeOut(200);
|
||||
remove.row.fadeOut(200);
|
||||
notification({
|
||||
text: remove.button.data("message"),
|
||||
type: 'success',
|
||||
timeout: 5000
|
||||
});
|
||||
} else {
|
||||
notification({
|
||||
text: 'Something went wrong.',
|
||||
type: 'error'
|
||||
});
|
||||
console.log(request.responseText);
|
||||
var response = JSON.parse(request.responseText),
|
||||
type = "success",
|
||||
timeout = 5000;
|
||||
|
||||
$(foreground).fadeOut(200);
|
||||
remove.form.fadeOut(200);
|
||||
remove.row.fadeOut(200);
|
||||
|
||||
if (request.status != 200) {
|
||||
type = "error";
|
||||
timeout = false;
|
||||
}
|
||||
|
||||
notification({
|
||||
text: response.message,
|
||||
type: type,
|
||||
timeout: timeout
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -244,23 +246,25 @@ $(document).on('page:browse', function() {
|
|||
request.send(JSON.stringify(content));
|
||||
request.onreadystatechange = function() {
|
||||
if (request.readyState == 4) {
|
||||
if (request.status == 200) {
|
||||
$.pjax({
|
||||
url: window.location.pathname,
|
||||
container: '#content'
|
||||
});
|
||||
notification({
|
||||
text: rename.button.data("message"),
|
||||
type: 'success',
|
||||
timeout: 5000
|
||||
});
|
||||
} else {
|
||||
notification({
|
||||
text: 'Something went wrong.',
|
||||
type: 'error'
|
||||
});
|
||||
console.log(request.responseText);
|
||||
var response = JSON.parse(request.responseText),
|
||||
type = "success",
|
||||
timeout = 5000;
|
||||
|
||||
if (request.status != 200) {
|
||||
type = "error";
|
||||
timeout = false;
|
||||
}
|
||||
|
||||
$.pjax({
|
||||
url: window.location.pathname,
|
||||
container: '#content'
|
||||
});
|
||||
|
||||
notification({
|
||||
text: response.message,
|
||||
type: type,
|
||||
timeout: timeout
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -313,22 +317,17 @@ $(document).on('page:browse', function() {
|
|||
request.onreadystatechange = function() {
|
||||
if (request.readyState == 4) {
|
||||
var data = JSON.parse(request.responseText);
|
||||
var type = "success"
|
||||
var timeout = 5000
|
||||
|
||||
if (request.status == 200) {
|
||||
if (data.success == "false") {
|
||||
type = "error"
|
||||
timeout = false
|
||||
}
|
||||
|
||||
notification({
|
||||
text: data.message,
|
||||
type: type,
|
||||
timeout: timeout
|
||||
type: "success"
|
||||
});
|
||||
} else {
|
||||
console.log(request.responseText)
|
||||
notification({
|
||||
text: data.message,
|
||||
type: "error"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
27
git/post.go
27
git/post.go
|
@ -12,9 +12,10 @@ import (
|
|||
|
||||
// POST handles the POST method on GIT page which is only an API.
|
||||
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||
// Check if git is installed on the computer
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
jsonMessage(w, "Git is not installed on your computer.", false)
|
||||
return 200, nil
|
||||
jsonMessage(w, "Git is not installed on your computer.", 500)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Get the JSON information sent using a buffer
|
||||
|
@ -25,9 +26,10 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
|||
var info map[string]interface{}
|
||||
json.Unmarshal(buff.Bytes(), &info)
|
||||
|
||||
// Check if command was sent
|
||||
if _, ok := info["command"]; !ok {
|
||||
jsonMessage(w, "Command not specified.", false)
|
||||
return 200, nil
|
||||
jsonMessage(w, "Command not specified.", 500)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
command := info["command"].(string)
|
||||
|
@ -38,8 +40,8 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
|||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
jsonMessage(w, "Command not specified.", false)
|
||||
return 200, nil
|
||||
jsonMessage(w, "Command not specified.", 500)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
cmd := exec.Command("git", args...)
|
||||
|
@ -47,26 +49,25 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
|||
output, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
jsonMessage(w, err.Error(), false)
|
||||
return 200, nil
|
||||
jsonMessage(w, err.Error(), 500)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
jsonMessage(w, string(output), true)
|
||||
return http.StatusOK, nil
|
||||
jsonMessage(w, string(output), 200)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type jsonMSG struct {
|
||||
Message string `json:"message"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func jsonMessage(w http.ResponseWriter, message string, success bool) {
|
||||
func jsonMessage(w http.ResponseWriter, message string, code int) {
|
||||
msg := &jsonMSG{
|
||||
Message: message,
|
||||
Success: success,
|
||||
}
|
||||
|
||||
m, _ := json.Marshal(msg)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(m)
|
||||
}
|
||||
|
|
18
hugo.go
18
hugo.go
|
@ -123,7 +123,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
|
||||
// Serve the static assets
|
||||
if page == "assets" {
|
||||
return serveAssets(w, r)
|
||||
code, err = serveAssets(w, r)
|
||||
}
|
||||
|
||||
// Browse page
|
||||
|
@ -151,6 +151,22 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
log.Panic(err)
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// Create the functions map, then the template, check for erros and
|
||||
// execute the template if there aren't errors
|
||||
functions := template.FuncMap{
|
||||
"Defined": utils.Defined,
|
||||
}
|
||||
|
||||
switch code {
|
||||
case 404:
|
||||
tpl, _ := utils.GetTemplate(r, functions, "404")
|
||||
tpl.Execute(w, nil)
|
||||
code = 200
|
||||
err = nil
|
||||
} */
|
||||
|
||||
return code, err
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
{{ define "content" }}
|
||||
<main>
|
||||
<h1>404 Not Found</h1>
|
||||
</main>
|
||||
{{ end }}
|
|
@ -1,6 +1,7 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
|
@ -236,3 +237,28 @@ func SplitCapitalize(name string) string {
|
|||
|
||||
return name
|
||||
}
|
||||
|
||||
type jsonMSG struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func RespondJSON(w http.ResponseWriter, message string, code int, err error) (int, error) {
|
||||
msg := &jsonMSG{
|
||||
Message: message,
|
||||
}
|
||||
|
||||
m, err := json.Marshal(msg)
|
||||
|
||||
if err != nil {
|
||||
return 500, err
|
||||
}
|
||||
|
||||
if code == 500 && err == nil {
|
||||
err = errors.New(message)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(m)
|
||||
return 0, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue