2017-12-09 00:21:58 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2018-01-07 22:40:11 +00:00
|
|
|
"bufio"
|
|
|
|
"encoding/hex"
|
2017-12-09 00:21:58 +00:00
|
|
|
"fmt"
|
2018-01-07 22:40:11 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-12-09 00:21:58 +00:00
|
|
|
"path"
|
2018-01-07 22:40:11 +00:00
|
|
|
"strings"
|
2017-12-09 00:21:58 +00:00
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
|
2018-01-07 22:40:11 +00:00
|
|
|
"github.com/dchest/blake2b"
|
|
|
|
)
|
2017-12-09 00:21:58 +00:00
|
|
|
|
2018-01-07 22:40:11 +00:00
|
|
|
func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
|
2017-12-09 00:21:58 +00:00
|
|
|
if _, err := exercice.WipeHints(); err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
2017-12-12 07:02:30 +00:00
|
|
|
} else if ! i.exists(path.Join(exercice.Path, "hints")) {
|
2018-01-07 22:40:11 +00:00
|
|
|
return
|
2017-12-09 00:21:58 +00:00
|
|
|
} else if hints, err := i.listDir(path.Join(exercice.Path, "hints")); err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
} else {
|
2017-12-12 06:13:38 +00:00
|
|
|
for n, hfile := range hints {
|
2018-01-07 22:40:11 +00:00
|
|
|
var hint_cnt string
|
|
|
|
|
|
|
|
if fi, err := i.stat(path.Join(exercice.Path, "hints", hfile)); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err))
|
|
|
|
} else if ! fi.IsDir() && fi.Size() > 512 {
|
|
|
|
// Handle big files as downloadable content
|
|
|
|
if res, err := i.importFile(path.Join(exercice.Path, "hints", hfile), func(filePath string, origin string) (interface{}, error) {
|
|
|
|
// Calculate hash
|
|
|
|
hash512 := blake2b.New512()
|
|
|
|
if fd, err := os.Open(filePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
reader := bufio.NewReader(fd)
|
|
|
|
if _, err := io.Copy(hash512, reader); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result512 := hash512.Sum(nil)
|
|
|
|
|
|
|
|
return "$FILES" + hex.EncodeToString(result512) + strings.TrimPrefix(filePath, fic.FilesDir), nil
|
|
|
|
}); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: %s", path.Base(exercice.Path), hfile, err))
|
|
|
|
continue
|
|
|
|
} else if s, ok := res.(string); !ok {
|
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hfile))
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
hint_cnt = s
|
|
|
|
}
|
|
|
|
} else if hint_cnt, err = getFileContent(i, path.Join(exercice.Path, "hints", hfile)); err != nil {
|
2017-12-09 00:21:58 +00:00
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to read hint file %q: %s", path.Base(exercice.Path), hfile, err))
|
|
|
|
continue
|
2018-01-07 22:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := exercice.AddHint(fmt.Sprintf("Astuce #%d", n + 1), hint_cnt, 1); err != nil {
|
2017-12-09 00:21:58 +00:00
|
|
|
errs = append(errs, fmt.Sprintf("%q: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 22:40:11 +00:00
|
|
|
return
|
2017-12-09 00:21:58 +00:00
|
|
|
}
|