admin: sync.ImportFile takes Importer as first arg
This commit is contained in:
parent
9a9d5fcda4
commit
3d59042802
@ -131,7 +131,7 @@ func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error)
|
|||||||
if len(uh.Content) != 0 {
|
if len(uh.Content) != 0 {
|
||||||
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
|
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
|
||||||
} else if len(uh.URI) != 0 {
|
} 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) {
|
func(filePath string, origin string) (interface{}, error) {
|
||||||
return exercice.AddHint(uh.Title, "$FILES" + strings.TrimPrefix(filePath, fic.FilesDir), uh.Cost)
|
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 nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return sync.ImportFile(uf.URI,
|
return sync.ImportFile(sync.GlobalImporter, uf.URI,
|
||||||
func(filePath string, origin string) (interface{}, error) {
|
func(filePath string, origin string) (interface{}, error) {
|
||||||
if digest, err := hex.DecodeString(uf.Digest); err != nil {
|
if digest, err := hex.DecodeString(uf.Digest); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -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))
|
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 the present file is still valide, don't erase it
|
||||||
if _, err := os.Stat(dest); !os.IsNotExist(err) {
|
if _, err := os.Stat(dest); !os.IsNotExist(err) {
|
||||||
@ -100,7 +104,7 @@ func ImportFile(URI string, next func(string, string) (interface{}, error)) (int
|
|||||||
} else {
|
} else {
|
||||||
defer fdto.Close()
|
defer fdto.Close()
|
||||||
writer := bufio.NewWriter(fdto)
|
writer := bufio.NewWriter(fdto)
|
||||||
if err := getFile(GlobalImporter, URI, writer); err != nil {
|
if err := getFile(i, URI, writer); err != nil {
|
||||||
os.Remove(dest)
|
os.Remove(dest)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user