admin: Don't download file already downloaded

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

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"path"
@ -28,21 +29,25 @@ func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args []string, b
return nil, errors.New("URI not filled")
}
if body, err := getCloudFile(uf.URI); err != nil {
return nil, err
} 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
}
hash := sha512.Sum512([]byte(uf.URI))
pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(uf.URI))
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
}
} 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) {