From 2bae30a841ec3399c53edd7893874070a9d47a3b Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 19 Jan 2020 00:54:00 +0100 Subject: [PATCH] admin/api: new route to list remote files and their properties --- admin/api/file.go | 3 +++ admin/sync/exercice_files.go | 34 ++++++++++++++++++++++++++++++++++ libfic/file.go | 4 ++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/admin/api/file.go b/admin/api/file.go index 6b28b7e7..ca7cce88 100644 --- a/admin/api/file.go +++ b/admin/api/file.go @@ -29,6 +29,9 @@ func init() { router.PUT("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(updateFile))) router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(deleteFile))) + // Remote + router.GET("/api/remote/themes/:thid/exercices/:exid/files", apiHandler(sync.ApiGetRemoteExerciceFiles)) + // Check router.POST("/api/files/:fileid/check", apiHandler(fileHandler(checkFile))) diff --git a/admin/sync/exercice_files.go b/admin/sync/exercice_files.go index 4d7e240c..3ca071a2 100644 --- a/admin/sync/exercice_files.go +++ b/admin/sync/exercice_files.go @@ -3,11 +3,14 @@ package sync import ( "bufio" "encoding/hex" + "errors" "fmt" "path" "strings" "unicode" + "github.com/julienschmidt/httprouter" + "srs.epita.fr/fic-server/libfic" ) @@ -137,3 +140,34 @@ func SyncExerciceFiles(i Importer, exercice fic.Exercice) (errs []string) { } return } + +// ApiGetRemoteExerciceFiles is an accessor to remote exercice files list. +func ApiGetRemoteExerciceFiles(ps httprouter.Params, _ []byte) (interface{}, error) { + theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid")) + if theme != nil { + exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil) + if exercice != nil { + files, digests, errs := BuildFilesListInto(GlobalImporter, *exercice, "files") + if files != nil { + var ret []fic.EFile + for _, fname := range files { + fPath := path.Join(exercice.Path, "files", fname) + fSize, _ := getFileSize(GlobalImporter, fPath) + ret = append(ret, fic.EFile{ + Path: fPath, + Name: fname, + Checksum: digests[fname], + Size: fSize, + }) + } + return ret, nil + } else { + return nil, errors.New(fmt.Sprintf("%q", errs)) + } + } else { + return nil, errors.New(fmt.Sprintf("%q", errs)) + } + } else { + return nil, errors.New(fmt.Sprintf("%q", errs)) + } +} diff --git a/libfic/file.go b/libfic/file.go index d2dbe693..1e565a6b 100644 --- a/libfic/file.go +++ b/libfic/file.go @@ -29,13 +29,13 @@ var PlainDigest bool = false // EFile represents a challenge file. type EFile struct { - Id int64 `json:"id"` + Id int64 `json:"id,omitempty"` // origin holds the import relative path of the file origin string // Path is the location where the file is stored, relatively to FilesDir Path string `json:"path"` // IdExercice is the identifier of the underlying challenge - IdExercice int64 `json:"idExercice"` + IdExercice int64 `json:"idExercice,omitempty"` // Name is the title displayed to players Name string `json:"name"` // Checksum stores the cached hash of the file