added download all mods button

This commit is contained in:
knoxfighter 2017-11-16 09:30:40 +01:00
parent 17143d7bf8
commit 0c7d1f3a21
3 changed files with 74 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"os"
"io"
"errors"
"lockfile"
)
// Returns JSON response of all mods installed in factorio/mods
@ -400,6 +401,61 @@ func UploadModHandler(w http.ResponseWriter, r *http.Request) {
}
}
func DownloadModsHandler(w http.ResponseWriter, r *http.Request) {
var err error
zipWriter := zip.NewWriter(w)
defer zipWriter.Close()
//iterate over folder and create everything in the zip
err = filepath.Walk(config.FactorioModsDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() == false {
//Lock the file, that we are want to read
err := fileLock.RLock(path)
if err != nil {
log.Printf("error locking file for reading, something else has locked it")
return err
}
defer fileLock.RUnlock(path)
writer, err := zipWriter.Create(info.Name())
if err != nil {
log.Printf("error on creating new file inside zip: %s", err)
return err
}
file, err := os.Open(path)
if err != nil {
log.Printf("error on opening modfile: %s", err)
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
if err != nil {
log.Printf("error on copying file into zip: %s", err)
return err
}
}
return nil
})
if err == lockfile.ErrorAlreadyLocked {
w.WriteHeader(http.StatusLocked)
return
}
if err != nil {
log.Printf("error on walking over the mods: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
writer_header := w.Header()
writer_header.Set("Content-Type", "application/zip;charset=UTF-8")
writer_header.Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "all_installed_mods.zip"))
}
func ListModPacksHandler(w http.ResponseWriter, r *http.Request) {
var err error
resp := JSONResponse{

View File

@ -208,6 +208,11 @@ var apiRoutes = Routes{
"POST",
"/mods/upload",
UploadModHandler,
},{
"DownloadMods",
"GET",
"/mods/download",
DownloadModsHandler,
}, {
"ListSaves",
"GET",

View File

@ -43,6 +43,10 @@ class ModOverview extends React.Component {
})
}
downloadAllHandler(e) {
e.stopPropagation();
}
render() {
return(
<div>
@ -84,6 +88,15 @@ class ModOverview extends React.Component {
<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.downloadAllHandler}>
<a className="btn btn-box-tool btn-default" style={{marginRight: 20}} href={"/api/mods/download"} download>
Download all Mods
</a>
</NativeListener>
: null
}
{
this.props.updates_available > 0 ?
<NativeListener onClick={this.props.updateAllMods}>