diff --git a/pkg/api/bundle/delete.go b/pkg/api/bundle/delete.go new file mode 100644 index 0000000..c53be47 --- /dev/null +++ b/pkg/api/bundle/delete.go @@ -0,0 +1,51 @@ +package bundle + +import ( + "encoding/json" + "log" + "net/http" + "os" + "strconv" + + "github.com/Ennovar/gPanel/pkg/gpaccount" +) + +func Delete(res http.ResponseWriter, req *http.Request, logger *log.Logger, bundles map[string]*gpaccount.Controller, dir string) bool { + if req.Method != "DELETE" { + logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed)) + http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return false + } + + var rqData struct { + Name string `json:"name"` + } + + err := json.NewDecoder(req.Body).Decode(&rqData) + if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) + http.Error(res, err.Error(), http.StatusBadRequest) + return false + } + + if _, ok := bundles[rqData.Name]; !ok { + logger.Println(req.URL.Path + ":: bundle does not exist") + http.Error(res, err.Error(), http.StatusBadRequest) + return false + } + + _ = bundles[rqData.Name].Public.Stop(false) + _ = bundles[rqData.Name].Stop(false) + + err = os.RemoveAll(dir) + if err != nil { + logger.Println(req.URL.Path + "::" + err.Error()) + http.Error(res, err.Error(), http.StatusInternalServerError) + return false + } + + delete(bundles, rqData.Name) + + res.WriteHeader(http.StatusNoContent) + return true +} diff --git a/pkg/gpserver/apihandler.go b/pkg/gpserver/apihandler.go index de646fc..4b6b965 100644 --- a/pkg/gpserver/apihandler.go +++ b/pkg/gpserver/apihandler.go @@ -60,6 +60,8 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b return true, logapi.Read(res, req, con.APILogger, specific.Directory) case "/log/truncate": return true, logapi.Truncate(res, req, con.APILogger, specific.Directory) + case "/bundle/delete": + return true, bundle.Delete(res, req, con.APILogger, con.Bundles, specific.Directory) default: return false, false } diff --git a/server/document_root/assets/js/panelHandlers/bundles/specific/delete.js b/server/document_root/assets/js/panelHandlers/bundles/specific/delete.js new file mode 100644 index 0000000..9e50dbf --- /dev/null +++ b/server/document_root/assets/js/panelHandlers/bundles/specific/delete.js @@ -0,0 +1,31 @@ +jQuery('._js_delete-specific-bundle').on('click', function(e){ + e.preventDefault(); + + var bundle = jQuery('.specific-bundle-modal').attr('data'); + + var ensure = confirm('Are you sure you want to delete this bundle? This action CANNOT be undone.'); + if(ensure) { + var requestData = {}; + requestData["name"] = bundle; + requestData["bundle_name"] = bundle; + + var xhr = new XMLHttpRequest(); + xhr.open('DELETE', 'api/bundle/delete', true); + xhr.send(JSON.stringify(requestData)); + + xhr.onloadend = function() { + if(xhr.status == 204) { + alert('Bundle \"' + bundle + '\" has been successfully deleted.'); + jQuery('.specific-bundle-modal').modal('hide'); + } + else { + if(xhr.response != undefined && xhr.response.length != 0) { + alert('Error: ' + xhr.response); + } + else { + alert('An error has occurred. Please refresh and try again. If error persists please contact your administrator.'); + } + } + } + } +}); diff --git a/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js b/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js index e039005..a104392 100644 --- a/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js +++ b/server/document_root/assets/js/panelHandlers/bundles/specific/server/status.js @@ -5,7 +5,6 @@ function getPublicServerStatus(name) { var requestData = {}; requestData["bundle_name"] = name; - console.log(requestData); xhr.open('POST', 'api/server/status', true); xhr.send(JSON.stringify(requestData)); diff --git a/server/document_root/gPanel.html b/server/document_root/gPanel.html index 23fb211..2335967 100644 --- a/server/document_root/gPanel.html +++ b/server/document_root/gPanel.html @@ -151,6 +151,7 @@ @@ -424,6 +425,7 @@ +