From f0377f5f5d7ad7c88a724e790ce9fec17f186ac2 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 28 Mar 2024 18:25:07 +0100 Subject: [PATCH] admin: Able to export an archive for static publication --- admin/api/export.go | 85 ++++++++++++++++++++++++++++++++ admin/api/router.go | 1 + admin/static/views/settings.html | 1 + 3 files changed, 87 insertions(+) create mode 100644 admin/api/export.go diff --git a/admin/api/export.go b/admin/api/export.go new file mode 100644 index 00000000..5a15f31d --- /dev/null +++ b/admin/api/export.go @@ -0,0 +1,85 @@ +package api + +import ( + "archive/zip" + "encoding/json" + "net/http" + "path" + + "srs.epita.fr/fic-server/libfic" + "srs.epita.fr/fic-server/settings" + + "github.com/gin-gonic/gin" +) + +func declareExportRoutes(router *gin.RouterGroup) { + router.GET("/archive.zip", func(c *gin.Context) { + challengeinfo, err := GetChallengeInfo() + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + my, err := fic.MyJSONTeam(nil, true) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + s, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + teams, err := fic.ExportTeams(false) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + themes, err := fic.ExportThemes() + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.Writer.WriteHeader(http.StatusOK) + c.Header("Content-Disposition", "attachment; filename=archive.zip") + c.Header("Content-Type", "application/zip") + + w := zip.NewWriter(c.Writer) + + // challenge.json + f, err := w.Create("challenge.json") + if err == nil { + json.NewEncoder(f).Encode(challengeinfo) + } + + // my.json + f, err = w.Create("my.json") + if err == nil { + json.NewEncoder(f).Encode(my) + } + + // settings.json + f, err = w.Create("settings.json") + if err == nil { + json.NewEncoder(f).Encode(s) + } + + // teams.json + f, err = w.Create("teams.json") + if err == nil { + json.NewEncoder(f).Encode(teams) + } + + // themes.json + f, err = w.Create("themes.json") + if err == nil { + json.NewEncoder(f).Encode(themes) + } + + w.Close() + }) +} diff --git a/admin/api/router.go b/admin/api/router.go index 7e360de5..743bc22e 100644 --- a/admin/api/router.go +++ b/admin/api/router.go @@ -11,6 +11,7 @@ func DeclareRoutes(router *gin.RouterGroup) { declareClaimsRoutes(apiRoutes) declareEventsRoutes(apiRoutes) declareExercicesRoutes(apiRoutes) + declareExportRoutes(apiRoutes) declareFilesGlobalRoutes(apiRoutes) declareFilesRoutes(apiRoutes) declareGlobalExercicesRoutes(apiRoutes) diff --git a/admin/static/views/settings.html b/admin/static/views/settings.html index 77fa4f67..72474aab 100644 --- a/admin/static/views/settings.html +++ b/admin/static/views/settings.html @@ -459,6 +459,7 @@ Regénérer les fichiers statiques + Télécharger l'archive pour publication

Changements anticipés