admin: Able to export an archive for static publication
continuous-integration/drone/tag Build is failing Details

This commit is contained in:
nemunaire 2024-03-28 18:25:07 +01:00
parent 9172f36be7
commit f0377f5f5d
3 changed files with 87 additions and 0 deletions

85
admin/api/export.go Normal file
View File

@ -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()
})
}

View File

@ -11,6 +11,7 @@ func DeclareRoutes(router *gin.RouterGroup) {
declareClaimsRoutes(apiRoutes)
declareEventsRoutes(apiRoutes)
declareExercicesRoutes(apiRoutes)
declareExportRoutes(apiRoutes)
declareFilesGlobalRoutes(apiRoutes)
declareFilesRoutes(apiRoutes)
declareGlobalExercicesRoutes(apiRoutes)

View File

@ -459,6 +459,7 @@
Regénérer les fichiers statiques
</button>
<button ng-if="settings.wip" ng-click="switchToProd()" class="btn btn-danger my-1" type="button"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span> Activer le mode challenge</button>
<a href="api/archive.zip" class="btn btn-secondary my-1" type="button" target="_self"><span class="glyphicon glyphicon-download" aria-hidden="true"></span> Télécharger l'archive pour publication</a>
</div>
<hr>
<h4>Changements anticipés</h4>