Skip to content

Commit 04992d2

Browse files
committed
allow upload of mod-settings.dat and mod-list.json
1 parent 6e0c098 commit 04992d2

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/api/mods_handler.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,36 @@ func ModUploadHandler(w http.ResponseWriter, r *http.Request) {
225225
return
226226
}
227227

228-
err = mods.UploadMod(formFile, fileHeader)
229-
if err != nil {
230-
resp = fmt.Sprintf("error saving file to mods: %s", err)
228+
// if the file is a zip file, we handle it as mod
229+
// if the file is mod-settings.dat or mod-list.json, we just replace the
230+
if filepath.Ext(fileHeader.Filename) == ".zip" {
231+
err = mods.UploadMod(formFile, fileHeader)
232+
if err != nil {
233+
resp = fmt.Sprintf("error saving file to mods: %s", err)
234+
log.Println(resp)
235+
w.WriteHeader(http.StatusInternalServerError)
236+
return
237+
}
238+
} else if fileHeader.Filename == "mod-settings.dat" || fileHeader.Filename == "mod-list.json" {
239+
modsDir := filepath.Join(bootstrap.GetConfig().FactorioModsDir, fileHeader.Filename)
240+
file, err := os.Create(modsDir)
241+
if err != nil {
242+
resp = fmt.Sprintf("error creating %s: %s", fileHeader.Filename, err)
243+
log.Println(resp)
244+
w.WriteHeader(http.StatusBadRequest)
245+
return
246+
}
247+
_, err = io.Copy(file, formFile)
248+
if err != nil {
249+
resp = fmt.Sprintf("error saving %s: %s", fileHeader.Filename, err)
250+
log.Println(resp)
251+
w.WriteHeader(http.StatusBadRequest)
252+
return
253+
}
254+
} else {
255+
resp = fmt.Sprintf("The uploaded file wasn't a zip-file, a mod-settings. dat or a mod-info.json")
231256
log.Println(resp)
232-
w.WriteHeader(http.StatusInternalServerError)
257+
w.WriteHeader(http.StatusBadRequest)
233258
return
234259
}
235260

@@ -296,7 +321,7 @@ func ModDownloadHandler(w http.ResponseWriter, r *http.Request) {
296321
writerHeader.Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "all_installed_mods.zip"))
297322
}
298323

299-
//LoadModsFromSaveHandler returns JSON response with the found mods
324+
// LoadModsFromSaveHandler returns JSON response with the found mods
300325
func LoadModsFromSaveHandler(w http.ResponseWriter, r *http.Request) {
301326
var err error
302327
var resp interface{}

ui/App/views/Mods/components/UploadMod.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const UploadMod = ({refetchInstalledMods}) => {
3232
onChange={e => setFileName(e.currentTarget.files[0].name)}
3333
id="mod_file"
3434
type="file"
35+
accept="application/zip,.zip,.dat,.json"
3536
/>
3637
<div className="px-2 py-2">{fileName}</div>
3738
</div>

0 commit comments

Comments
 (0)