Import logos from challenge.json
Some checks are pending
continuous-integration/drone/push Build is running
Some checks are pending
continuous-integration/drone/push Build is running
This commit is contained in:
parent
f4188ec289
commit
11a12e1d44
5 changed files with 81 additions and 27 deletions
38
admin/sync/challengeinfo.go
Normal file
38
admin/sync/challengeinfo.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
)
|
||||
|
||||
// ImportChallengeInfo imports images defined in the challengeinfo.
|
||||
func ImportChallengeInfo(ci *settings.ChallengeInfo) (err error) {
|
||||
if len(ci.MainLogo) > 0 {
|
||||
for i, logo := range ci.MainLogo {
|
||||
dest := path.Join(fic.FilesDir, "logo", path.Base(logo))
|
||||
err = importFile(GlobalImporter, logo, dest)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ci.MainLogo[i] = path.Join("$FILES$", strings.TrimPrefix(dest, fic.FilesDir))
|
||||
}
|
||||
}
|
||||
|
||||
if len(ci.Partners) > 0 {
|
||||
for i, partner := range ci.Partners {
|
||||
dest := path.Join(fic.FilesDir, "partner", path.Base(partner.Src))
|
||||
err = importFile(GlobalImporter, partner.Src, dest)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ci.Partners[i].Src = path.Join("$FILES$", strings.TrimPrefix(dest, fic.FilesDir))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -165,6 +165,26 @@ func getDestinationFilePath(URI string) string {
|
|||
return path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:])), path.Base(URI))
|
||||
}
|
||||
|
||||
func importFile(i Importer, URI string, dest string) error {
|
||||
if err := os.MkdirAll(path.Dir(dest), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write file
|
||||
if fdto, err := os.Create(dest); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer fdto.Close()
|
||||
writer := bufio.NewWriter(fdto)
|
||||
if err := GetFile(i, URI, writer); err != nil {
|
||||
os.Remove(dest)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ImportFile imports the file at the given URI, using helpers of the given Importer.
|
||||
// After import, next is called with relative path where the file has been saved and the original URI.
|
||||
func ImportFile(i Importer, URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
|
||||
|
|
@ -177,22 +197,10 @@ func ImportFile(i Importer, URI string, next func(string, string) (interface{},
|
|||
}
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(path.Dir(dest), 0755); err != nil {
|
||||
if err := importFile(i, URI, dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Write file
|
||||
if fdto, err := os.Create(dest); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer fdto.Close()
|
||||
writer := bufio.NewWriter(fdto)
|
||||
if err := GetFile(i, URI, writer); err != nil {
|
||||
os.Remove(dest)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return next(dest, URI)
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue