Import logos from challenge.json
Some checks are pending
continuous-integration/drone/push Build is running

This commit is contained in:
nemunaire 2022-06-06 20:42:46 +02:00
parent f4188ec289
commit 11a12e1d44
5 changed files with 81 additions and 27 deletions

View File

@ -118,7 +118,6 @@ func GetChallengeInfo() (*settings.ChallengeInfo, error) {
func getChallengeInfo(c *gin.Context) { func getChallengeInfo(c *gin.Context) {
if s, err := GetChallengeInfo(); err != nil { if s, err := GetChallengeInfo(); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
} else { } else {
c.JSON(http.StatusOK, s) c.JSON(http.StatusOK, s)
} }
@ -132,6 +131,7 @@ func saveChallengeInfo(c *gin.Context) {
return return
} }
if sync.GlobalImporter != nil {
jenc, err := json.Marshal(info) jenc, err := json.Marshal(info)
if err != nil { if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
@ -145,6 +145,14 @@ func saveChallengeInfo(c *gin.Context) {
return return
} }
err = sync.ImportChallengeInfo(info)
if err != nil {
log.Println("Unable to ImportChallengeInfo:", err.Error())
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something goes wrong when trying to import related files: %s", err.Error())})
return
}
}
if err := settings.SaveChallengeInfo(path.Join(settings.SettingsDir, settings.ChallengeFile), info); err != nil { if err := settings.SaveChallengeInfo(path.Join(settings.SettingsDir, settings.ChallengeFile), info); err != nil {
log.Println("Unable to SaveChallengeInfo:", err.Error()) log.Println("Unable to SaveChallengeInfo:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to save distributed challenge info: %s", err.Error())}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to save distributed challenge info: %s", err.Error())})

View 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
}

View File

@ -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)) 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. // 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. // 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) { 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 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) return next(dest, URI)
} }

View File

@ -45,7 +45,7 @@
<a href="." style="max-width: 50%"> <a href="." style="max-width: 50%">
{#if $challengeInfo && $challengeInfo.main_logo} {#if $challengeInfo && $challengeInfo.main_logo}
{#each $challengeInfo.main_logo as logo, i} {#each $challengeInfo.main_logo as logo, i}
<img src={logo} alt={'Logo principal #' + i} class={'h-100' + (i > 0?' d-none d-md-inline ms-2':'')}> <img src={logo.replace('$FILES$', '/files/')} alt={'Logo principal #' + i} class={'h-100' + (i > 0?' d-none d-md-inline ms-2':'')}>
{/each} {/each}
{/if} {/if}
</a> </a>

View File

@ -16,10 +16,10 @@
<CarouselItem bind:activeIndex={activePartner} itemIndex={index} class="h-100 text-end"> <CarouselItem bind:activeIndex={activePartner} itemIndex={index} class="h-100 text-end">
{#if partner.href} {#if partner.href}
<a href="{partner.href}" target="_blank" class="h-100"> <a href="{partner.href}" target="_blank" class="h-100">
<img src={partner.img} class="h-100" alt={partner.alt}> <img src={partner.img.replace('$FILES$', '/files')} class="h-100" alt={partner.alt}>
</a> </a>
{:else} {:else}
<img src={partner.img} class="h-100" alt={partner.alt}> <img src={partner.img.replace('$FILES$', '/files')} class="h-100" alt={partner.alt}>
{/if} {/if}
</CarouselItem> </CarouselItem>
{/each} {/each}