Import file from owncloud

This commit is contained in:
nemunaire 2016-01-20 22:44:34 +01:00
parent 97bb149eb6
commit 69ad64715c
6 changed files with 104 additions and 28 deletions

1
admin/.gitignore vendored
View file

@ -1,3 +1,4 @@
admin
fic.db
PKI/
FILES/

View file

@ -19,7 +19,7 @@ type uploadedExercice struct {
func createExercice(theme fic.Theme, args []string, body []byte) (interface{}, error) {
if len(args) >= 1 {
if eid, err := strconv.Atoi(string(args[0])); err != nil {
if eid, err := strconv.Atoi(args[0]); err != nil {
return nil, err
} else if exercice, err := theme.GetExercice(eid); err != nil {
return nil, err
@ -55,25 +55,6 @@ func createExercice(theme fic.Theme, args []string, body []byte) (interface{}, e
}
}
type uploadedFile struct {
URI string
}
func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args []string, body []byte) (interface{}, error) {
var uf uploadedFile
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err
}
if len(uf.URI) == 0 {
return nil, errors.New("URI not filled")
}
// TODO: se connecter à OwnCloud
return nil, nil
}
type uploadedKey struct {
Name string
Key string

65
admin/api_file.go Normal file
View file

@ -0,0 +1,65 @@
package main
import (
"crypto/sha512"
"encoding/base32"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"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
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err
}
if len(uf.URI) == 0 {
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
}
if err := ioutil.WriteFile(pathname, body, 0777); err != nil {
return nil, err
}
return exercice.ImportFile(pathname, uf.URI)
}
}
func getCloudFile(pathname string) ([]byte, error) {
client := http.Client{}
if req, err := http.NewRequest("GET", CloudDAVBase+pathname, nil); err != nil {
return nil, err
} else {
req.SetBasicAuth(CloudUsername, CloudPassword)
if resp, err := client.Do(req); err != nil {
return nil, err
} else {
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
return nil, err
} else {
return body, nil
}
}
}
}

View file

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"srs.epita.fr/fic-server/libfic"
)
@ -13,6 +14,9 @@ import (
var PKIDir string
var SubmissionDir string
var BaseURL string
var CloudDAVBase string
var CloudUsername string
var CloudPassword string
func main() {
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
@ -20,8 +24,28 @@ func main() {
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts")
flag.StringVar(&fic.FilesDir, "files", "./FILES/", "Base directory where found challenges files, local part")
flag.StringVar(&CloudDAVBase, "clouddav", "https://srs.epita.fr/owncloud/remote.php/webdav/FIC 2016",
"Base directory where found challenges files, cloud part")
flag.StringVar(&CloudUsername, "clouduser", "fic", "Username used to sync")
flag.StringVar(&CloudPassword, "cloudpass", "", "Password used to sync")
flag.Parse()
var err error
log.Println("Checking paths...")
if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil {
log.Fatal(err)
os.Exit(1)
}
if PKIDir, err = filepath.Abs(PKIDir); err != nil {
log.Fatal(err)
os.Exit(1)
}
if SubmissionDir, err = filepath.Abs(SubmissionDir); err != nil {
log.Fatal(err)
os.Exit(1)
}
log.Println("Opening database...")
if err := fic.DBInit(*dbfile); err != nil {
log.Fatal("Cannot open the database: ", err)