admin: sync.ImportFile takes Importer as first arg

This commit is contained in:
nemunaire 2017-12-12 06:22:22 +01:00 committed by Pierre-Olivier Mercier
parent 9a9d5fcda4
commit 3d59042802
2 changed files with 9 additions and 5 deletions

View File

@ -131,7 +131,7 @@ func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error)
if len(uh.Content) != 0 {
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
} else if len(uh.URI) != 0 {
return sync.ImportFile(uh.URI,
return sync.ImportFile(sync.GlobalImporter, uh.URI,
func(filePath string, origin string) (interface{}, error) {
return exercice.AddHint(uh.Title, "$FILES" + strings.TrimPrefix(filePath, fic.FilesDir), uh.Cost)
})
@ -221,7 +221,7 @@ func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error)
return nil, err
}
return sync.ImportFile(uf.URI,
return sync.ImportFile(sync.GlobalImporter, uf.URI,
func(filePath string, origin string) (interface{}, error) {
if digest, err := hex.DecodeString(uf.Digest); err != nil {
return nil, err

View File

@ -70,9 +70,13 @@ func getFileContent(i Importer, URI string) (string, error) {
}
}
func ImportFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
func getDestinationFilePath(URI string) string {
hash := blake2b.Sum512([]byte(URI))
dest := 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, next func(string, string) (interface{}, error)) (interface{}, error) {
dest := getDestinationFilePath(URI)
// If the present file is still valide, don't erase it
if _, err := os.Stat(dest); !os.IsNotExist(err) {
@ -100,7 +104,7 @@ func ImportFile(URI string, next func(string, string) (interface{}, error)) (int
} else {
defer fdto.Close()
writer := bufio.NewWriter(fdto)
if err := getFile(GlobalImporter, URI, writer); err != nil {
if err := getFile(i, URI, writer); err != nil {
os.Remove(dest)
return nil, err
}