feature: delete all mods button

This commit is contained in:
knoxfighter 2017-09-30 14:31:18 +02:00
parent caba55241b
commit 5d9900d34a
7 changed files with 133 additions and 4 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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{

View File

@ -193,6 +193,11 @@ var apiRoutes = Routes{
"POST",
"/mods/delete",
DeleteModHandler,
}, {
"DeleteAllMods",
"POST",
"/mods/delete/all",
DeleteAllModsHandler,
}, {
"UpdateMod",
"POST",

View File

@ -83,6 +83,16 @@ class ModOverview extends React.Component {
<div className="box-header" data-widget="collapse" style={{cursor: "pointer"}}>
<i className="fa fa-minus"></i>
<h3 className="box-title">Manage Mods</h3>
<div className="box-tools pull-right">
{
this.props.installedMods != null ?
<NativeListener onClick={this.props.deleteAll}>
<button className="btn btn-box-tool btn-danger" style={{color: "#fff"}}>Delete ALL
</button>
</NativeListener>
: null
}
</div>
</div>
<ModManager
@ -110,6 +120,7 @@ ModOverview.propTypes = {
submitFactorioLogin: React.PropTypes.func.isRequired,
toggleMod: React.PropTypes.func.isRequired,
deleteMod: React.PropTypes.func.isRequired,
deleteAll: React.PropTypes.func.isRequired,
updateMod: React.PropTypes.func.isRequired,
uploadModSuccessHandler: React.PropTypes.func.isRequired,
logged_in: React.PropTypes.bool.isRequired,

View File

@ -17,10 +17,11 @@ class ModsContent extends React.Component {
this.updateModHandler = this.updateModHandler.bind(this);
this.uploadModSuccessHandler = this.uploadModSuccessHandler.bind(this);
this.factorioLogoutHandler = this.factorioLogoutHandler.bind(this);
this.deleteAllHandler = this.deleteAllHandler.bind(this);
this.state = {
logged_in: false,
installedMods: [],
installedMods: null,
}
}
@ -317,6 +318,47 @@ class ModsContent extends React.Component {
});
}
deleteAllHandler(e) {
e.preventDefault();
e.stopPropagation();
let class_this = this;
swal({
title: "Delete Mod?",
text: "This will delete ALL mods and can't be redone!<br> 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() + "<br>" + 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}