admin: Don't download file already downloaded

This commit is contained in:
nemunaire 2016-01-22 17:57:50 +01:00
commit 58fb73f742

View file

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -28,21 +29,25 @@ func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args []string, b
return nil, errors.New("URI not filled") return nil, errors.New("URI not filled")
} }
if body, err := getCloudFile(uf.URI); err != nil { hash := sha512.Sum512([]byte(uf.URI))
return nil, err pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(uf.URI))
} else {
hash := sha512.Sum512([]byte(uf.URI))
pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(uf.URI))
if err := os.MkdirAll(path.Dir(pathname), 0777); err != nil {
return nil, err
}
if err := ioutil.WriteFile(pathname, body, 0777); err != nil { if _, err := os.Stat(pathname); os.IsNotExist(err) {
log.Println("Import file from Cloud:", uf.URI, "=>", pathname)
if body, err := getCloudFile(uf.URI); err != nil {
return nil, err return nil, err
} } else {
if err := os.MkdirAll(path.Dir(pathname), 0777); err != nil {
return nil, err
}
return exercice.ImportFile(pathname, uf.URI) if err := ioutil.WriteFile(pathname, body, 0777); err != nil {
return nil, err
}
}
} }
return exercice.ImportFile(pathname, uf.URI)
} }
func getCloudFile(pathname string) ([]byte, error) { func getCloudFile(pathname string) ([]byte, error) {