admin: add ability to add files from local storage

This commit is contained in:
nemunaire 2016-11-19 15:54:32 +01:00
commit b41180c7b0
4 changed files with 76 additions and 42 deletions

View file

@ -15,33 +15,43 @@ import (
"srs.epita.fr/fic-server/libfic"
)
type uploadedFile struct {
URI string
}
func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args []string, body []byte) (interface{}, error) {
var uf uploadedFile
var uf map[string]string
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err
}
if len(uf.URI) == 0 {
return nil, errors.New("URI not filled")
var hash [sha512.Size]byte
var logStr string
var fromURI string
var getFile func(string) (error)
if URI, ok := uf["URI"]; ok {
hash = sha512.Sum512([]byte(URI))
logStr = "Import file from Cloud: " + URI + " =>"
fromURI = URI
getFile = func(dest string) error { return getCloudFile(URI, dest); }
} else if path, ok := uf["path"]; ok {
hash = sha512.Sum512([]byte(path))
logStr = "Import file from local FS: " + path + " =>"
fromURI = path
getFile = func(dest string) error { return os.Symlink(path, dest); }
} else {
return nil, errors.New("URI or path not filled")
}
hash := sha512.Sum512([]byte(uf.URI))
pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(uf.URI))
pathname := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.EncodeToString(hash[:])), path.Base(fromURI))
if _, err := os.Stat(pathname); os.IsNotExist(err) {
log.Println("Import file from Cloud:", uf.URI, "=>", pathname)
log.Println(logStr, pathname)
if err := os.MkdirAll(path.Dir(pathname), 0777); err != nil {
return nil, err
} else if err := getCloudFile(uf.URI, pathname); err != nil {
} else if err := getFile(pathname); err != nil {
return nil, err
}
}
return exercice.ImportFile(pathname, uf.URI)
return exercice.ImportFile(pathname, fromURI)
}
func getCloudFile(pathname string, dest string) error {