From f4c3f1e15e6d726c1f9a9bf1a953036a563e5cc6 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Mon, 21 Nov 2022 12:02:36 +0100 Subject: [PATCH] sync: Create empty file for nginx gzip-static module --- admin/static.go | 1 + admin/sync/exercice_files.go | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/admin/static.go b/admin/static.go index c91e17b2..09cff9d3 100644 --- a/admin/static.go +++ b/admin/static.go @@ -101,6 +101,7 @@ func declareStaticRoutes(router *gin.RouterGroup, cfg *settings.Settings, baseUR }) router.GET("/files/*_", func(c *gin.Context) { + // TODO: handle .gz file here http.ServeFile(c.Writer, c.Request, path.Join(fic.FilesDir, strings.TrimPrefix(c.Request.URL.Path, path.Join(baseURL, "files")))) }) router.GET("/submissions/*_", func(c *gin.Context) { diff --git a/admin/sync/exercice_files.go b/admin/sync/exercice_files.go index 84cf8c61..00623228 100644 --- a/admin/sync/exercice_files.go +++ b/admin/sync/exercice_files.go @@ -4,7 +4,9 @@ import ( "bufio" "encoding/hex" "fmt" + "log" "net/http" + "os" "path" "strings" "unicode" @@ -173,12 +175,32 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExce } else if f.(*fic.EFile).Size == 0 { errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("imported file is empty!"))) } else { + file := f.(*fic.EFile) + // Call checks hooks for _, h := range hooks.fileHooks { - for _, e := range h(f.(*fic.EFile), exceptions) { + for _, e := range h(file, exceptions) { errs = append(errs, NewFileError(exercice, fname, e)) } } + + // Create empty non-gziped file for nginx gzip-static module + if len(file.ChecksumShown) > 0 && strings.HasSuffix(file.Name, ".gz") { + file.Name = strings.TrimSuffix(file.Name, ".gz") + file.Path = strings.TrimSuffix(file.Path, ".gz") + + fd, err := os.Create(path.Join(fic.FilesDir, file.Path)) + if err == nil { + fd.Close() + + _, err = file.Update() + if err != nil { + log.Println("Unable to update file after .gz removal:", err.Error()) + } + } else { + log.Printf("Unable to create %q: %s", file.Path, err) + } + } } } return