Skip to content

Commit 9547653

Browse files
committed
added handlers and methods for downloading and deleting modpacks
1 parent fe380e5 commit 9547653

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

src/handlers.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func DownloadMod(w http.ResponseWriter, r *http.Request) {
199199

200200
vars := mux.Vars(r)
201201
mod := vars["mod"]
202-
modFile := config.FactorioModsDir + "/" + mod
202+
modFile := filepath.Join(config.FactorioModsDir, mod)
203203

204204
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", mod))
205205
log.Printf("%s downloading: %s", r.Host, modFile)
@@ -277,6 +277,48 @@ func ListModPacks(w http.ResponseWriter, r *http.Request) {
277277
}
278278
}
279279

280+
func DownloadModPack(w http.ResponseWriter, r *http.Request) {
281+
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
282+
283+
vars := mux.Vars(r)
284+
modpack := vars["modpack"]
285+
modFile := filepath.Join(config.FactorioDir, "modpacks", modpack)
286+
287+
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", modpack))
288+
log.Printf("%s downloading: %s", r.Host, modFile)
289+
290+
http.ServeFile(w, r, modFile)
291+
}
292+
293+
func DeleteModPack(w http.ResponseWriter, r *http.Request) {
294+
var err error
295+
resp := JSONResponse{
296+
Success: false,
297+
}
298+
299+
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
300+
301+
vars := mux.Vars(r)
302+
modPack := vars["modpack"]
303+
304+
err = rmModPack(modPack)
305+
if err == nil {
306+
// Modpack removed
307+
resp.Data = fmt.Sprintf("Removed modpack: %s", modPack)
308+
resp.Success = true
309+
if err := json.NewEncoder(w).Encode(resp); err != nil {
310+
log.Printf("Error removing modpack %s", err)
311+
}
312+
} else {
313+
log.Printf("Error in remove modpack handler: %s", err)
314+
resp.Data = fmt.Sprintf("Error in remove modpack handler: %s", err)
315+
316+
if err := json.NewEncoder(w).Encode(resp); err != nil {
317+
log.Printf("Error removing modpack: %s", err)
318+
}
319+
}
320+
}
321+
280322
// Lists all save files in the factorio/saves directory
281323
func ListSaves(w http.ResponseWriter, r *http.Request) {
282324
var err error

src/mods.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func rmMod(modName string) error {
5757
for _, mod := range installedMods {
5858
if strings.Contains(mod, modName) {
5959
log.Printf("Removing mod: %s", mod)
60-
err := os.Remove(config.FactorioModsDir + "/" + mod)
60+
err := os.Remove(filepath.Join(config.FactorioModsDir, mod))
6161
if err != nil {
6262
log.Printf("Error removing mod %s: %s", mod, err)
6363
return err
@@ -75,6 +75,39 @@ func rmMod(modName string) error {
7575
return nil
7676
}
7777

78+
func rmModPack(modpack string) error {
79+
removed := false
80+
if modpack == "" {
81+
return errors.New("No mod pack name provided.")
82+
}
83+
// Get list of modpacks
84+
modpacks, err := listModPacks(filepath.Join(config.FactorioDir, "modpacks"))
85+
if err != nil {
86+
log.Printf("Error listing modpacks in rmModPack: %s", err)
87+
return err
88+
}
89+
90+
for _, m := range modpacks {
91+
if strings.Contains(m, modpack) {
92+
log.Printf("Removing modpack: %s", m)
93+
err := os.Remove(filepath.Join(config.FactorioDir, "modpacks", m))
94+
if err != nil {
95+
log.Printf("Error trying to remove modpack: %s: %s", m, err)
96+
return err
97+
}
98+
removed = true
99+
log.Printf("Removed modpack: %s", m)
100+
}
101+
}
102+
103+
if !removed {
104+
log.Printf("Did not remove modpack: %s", modpack)
105+
return errors.New(fmt.Sprintf("Did not remove modpack: %s", modpack))
106+
}
107+
108+
return nil
109+
}
110+
78111
func createModPackDir() error {
79112
err := os.Mkdir(filepath.Join(config.FactorioDir, "modpacks"), 0775)
80113
if err != nil {

src/routes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,15 @@ var apiRoutes = Routes{
204204
"GET",
205205
"/mods/packs/list",
206206
ListModPacks,
207+
}, {
208+
"DownloadModPack",
209+
"GET",
210+
"/mods/packs/dl/{modpack}",
211+
DownloadModPack,
212+
}, {
213+
"DeleteModPack",
214+
"GET",
215+
"/mods/packs/rm/{modpack}",
216+
DeleteModPack,
207217
},
208218
}

0 commit comments

Comments
 (0)