diff --git a/admin/api/exercice.go b/admin/api/exercice.go index 16ea7fc6..f93e9c18 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -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 diff --git a/admin/sync/file.go b/admin/sync/file.go index 3e5f6d54..46776b92 100644 --- a/admin/sync/file.go +++ b/admin/sync/file.go @@ -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 }