feat(backend): add mod management API endpoints
This commit is contained in:
89
backend/internal/api/mods.go
Normal file
89
backend/internal/api/mods.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"arma3-web-server/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type deleteModInput struct {
|
||||
Path string `json:"path" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) ListMods(c *gin.Context) {
|
||||
workshopMods := services.ListWorkshopMods(h.serverfileDir)
|
||||
localMods := services.ListLocalMods(h.modsDir)
|
||||
|
||||
usedWorkshop, usedLocal := services.BuildUsageMap(h.modlists)
|
||||
|
||||
for i := range workshopMods {
|
||||
if lists, ok := usedWorkshop[workshopMods[i].ID]; ok {
|
||||
workshopMods[i].InUse = true
|
||||
workshopMods[i].Modlists = lists
|
||||
// pull name from first modlist that has it
|
||||
if workshopMods[i].Name == "" {
|
||||
workshopMods[i].Name = lists[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i := range localMods {
|
||||
// check both the display name and the @name
|
||||
if lists, ok := usedLocal[localMods[i].ID]; ok {
|
||||
localMods[i].InUse = true
|
||||
localMods[i].Modlists = lists
|
||||
} else if lists, ok := usedLocal[localMods[i].Name]; ok {
|
||||
localMods[i].InUse = true
|
||||
localMods[i].Modlists = lists
|
||||
}
|
||||
}
|
||||
|
||||
all := append(workshopMods, localMods...)
|
||||
if all == nil {
|
||||
all = []services.ModInfo{}
|
||||
}
|
||||
c.JSON(http.StatusOK, all)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteMod(c *gin.Context) {
|
||||
var input deleteModInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := services.RemoveMod(input.Path); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
}
|
||||
|
||||
func (h *Handler) CleanupMods(c *gin.Context) {
|
||||
workshopMods := services.ListWorkshopMods(h.serverfileDir)
|
||||
localMods := services.ListLocalMods(h.modsDir)
|
||||
|
||||
usedWorkshop, usedLocal := services.BuildUsageMap(h.modlists)
|
||||
|
||||
var deleted int
|
||||
for _, m := range workshopMods {
|
||||
if _, ok := usedWorkshop[m.ID]; !ok {
|
||||
if err := services.RemoveMod(m.Path); err == nil {
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, m := range localMods {
|
||||
if _, ok := usedLocal[m.ID]; !ok {
|
||||
if _, ok2 := usedLocal[m.Name]; !ok2 {
|
||||
if err := services.RemoveMod(m.Path); err == nil {
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"deleted": deleted})
|
||||
}
|
||||
Reference in New Issue
Block a user