diff --git a/src/mod_Mods.go b/src/mod_Mods.go index e78074a..0b528bc 100644 --- a/src/mod_Mods.go +++ b/src/mod_Mods.go @@ -165,8 +165,6 @@ func (mods *Mods) downloadMod(url string, filename string, mod_id string) (error func (mods *Mods) uploadMod(header *multipart.FileHeader) (error) { var err error - log.Printf("filename of the uploaded file: %s", header.Filename) - if filepath.Ext(header.Filename) != ".zip" { log.Print("The uploaded file wasn't a zip-file -> ignore it") return nil //simply do nothing xD diff --git a/src/mod_modSimple.go b/src/mod_modSimple.go index ce517b2..c77e024 100644 --- a/src/mod_modSimple.go +++ b/src/mod_modSimple.go @@ -4,6 +4,7 @@ import ( "log" "io/ioutil" "encoding/json" + "os" ) type ModSimple struct { @@ -36,8 +37,25 @@ func (mod_simple_list *ModSimpleList) listInstalledMods() (error) { var err error file, err := ioutil.ReadFile(mod_simple_list.Destination + "/mod-list.json") + if os.IsNotExist(err) { + log.Println("no mod-list.json found ... create new one ...") + + //mod-list.json does not exist, create one + base_mod := ModSimple{ + Name: "base", + Enabled: true, + } + mod_simple_list.Mods = append(mod_simple_list.Mods, base_mod) + err = mod_simple_list.saveModInfoJson() + if err != nil { + log.Printf("error saving mod-list.json: %s", err) + return err + } + + return nil + } if err != nil { - log.Printf("ModSimpleList ... error read the mod-info.json: %s", err) + log.Printf("ModSimpleList ... error read or write the mod-info.json: %s", err) return err } diff --git a/src/mods.go b/src/mods.go index ced98e7..9960504 100644 --- a/src/mods.go +++ b/src/mods.go @@ -185,6 +185,32 @@ func getModDetails(modId string) (string, error, int) { return text_string, nil, resp.StatusCode } +func deleteAllMods() (error) { + var err error + + mods_dir_info, err := os.Stat(config.FactorioModsDir) + if err != nil { + log.Printf("error getting stats of FactorioModsDir: %s", err) + return err + } + + mods_dir_perm := mods_dir_info.Mode().Perm() + + err = os.RemoveAll(config.FactorioModsDir) + if err != nil { + log.Printf("removing FactorioModsDir failed: %s", err) + return err + } + + err = os.Mkdir(config.FactorioModsDir, mods_dir_perm) + if err != nil { + log.Printf("error recreating modPackDir: %s", err) + return err + } + + return nil +} + func modStartUp() { var err error diff --git a/src/mods_handler.go b/src/mods_handler.go index e82266c..8915d4e 100644 --- a/src/mods_handler.go +++ b/src/mods_handler.go @@ -292,6 +292,34 @@ func DeleteModHandler(w http.ResponseWriter, r *http.Request) { } } +func DeleteAllModsHandler(w http.ResponseWriter, r *http.Request) { + var err error + resp := JSONResponse{ + Success: false, + } + + w.Header().Set("Content-Type", "application/json;charset=UTF-8") + + //delete mods folder + err = deleteAllMods() + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + resp.Data = fmt.Sprintf("Error in deleteMod: %s", err) + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error in DeleteModHandler: %s", err) + } + return + } + + resp.Data = nil + resp.Success = true + + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error in DeleteModHandler: %s", err) + } +} + func UpdateModHandler(w http.ResponseWriter, r *http.Request) { var err error resp := JSONResponse{ diff --git a/src/routes.go b/src/routes.go index ad51af7..c12d0e1 100644 --- a/src/routes.go +++ b/src/routes.go @@ -193,6 +193,11 @@ var apiRoutes = Routes{ "POST", "/mods/delete", DeleteModHandler, + }, { + "DeleteAllMods", + "POST", + "/mods/delete/all", + DeleteAllModsHandler, }, { "UpdateMod", "POST", diff --git a/ui/App/components/Mods/ModOverview.jsx b/ui/App/components/Mods/ModOverview.jsx index 2b22ed4..1ee0d81 100644 --- a/ui/App/components/Mods/ModOverview.jsx +++ b/ui/App/components/Mods/ModOverview.jsx @@ -83,6 +83,16 @@ class ModOverview extends React.Component {

Manage Mods

+
+ { + this.props.installedMods != null ? + + + + : null + } +
Are you sure?", + type: "info", + showCancelButton: true, + closeOnConfirm: false, + confirmButtonText: "Yes, Delete ALL!", + cancelButtonText: "Cancel", + showLoaderOnConfirm: true, + confirmButtonColor: "#DD6B55", + html: true, + }, function () { + $.ajax({ + url: "/api/mods/delete/all", + method: "POST", + dataType: "JSON", + success: (data) => { + swal("ALL mods deleted successful", "", "success"); + class_this.setState({ + installedMods: data.data + }); + }, + error: (jqXHR, status, err) => { + console.log('api/mods/delete/all', status, err.toString()); + swal({ + title: "Delete all mods went wrong", + text: err.toString() + "
" + jqXHR.responseJSON.data, + type: "error", + html: true + }); + } + }); + }); + } + updateModHandler(e, toggleUpdateStatus, removeVersionAvailableStatus) { e.preventDefault(); @@ -397,6 +439,7 @@ class ModsContent extends React.Component { submitFactorioLogin={this.handlerFactorioLogin} toggleMod={this.toggleModHandler} deleteMod={this.deleteModHandler} + deleteAll={this.deleteAllHandler} updateMod={this.updateModHandler} uploadModSuccessHandler={this.uploadModSuccessHandler} modContentClass={this}