Import file from owncloud
This commit is contained in:
parent
97bb149eb6
commit
69ad64715c
1
admin/.gitignore
vendored
1
admin/.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
admin
|
admin
|
||||||
fic.db
|
fic.db
|
||||||
PKI/
|
PKI/
|
||||||
|
FILES/
|
||||||
|
|
|
@ -19,7 +19,7 @@ type uploadedExercice struct {
|
||||||
|
|
||||||
func createExercice(theme fic.Theme, args []string, body []byte) (interface{}, error) {
|
func createExercice(theme fic.Theme, args []string, body []byte) (interface{}, error) {
|
||||||
if len(args) >= 1 {
|
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
|
return nil, err
|
||||||
} else if exercice, err := theme.GetExercice(eid); err != nil {
|
} else if exercice, err := theme.GetExercice(eid); err != nil {
|
||||||
return nil, err
|
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 {
|
type uploadedKey struct {
|
||||||
Name string
|
Name string
|
||||||
Key string
|
Key string
|
||||||
|
|
65
admin/api_file.go
Normal file
65
admin/api_file.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
)
|
)
|
||||||
|
@ -13,6 +14,9 @@ import (
|
||||||
var PKIDir string
|
var PKIDir string
|
||||||
var SubmissionDir string
|
var SubmissionDir string
|
||||||
var BaseURL string
|
var BaseURL string
|
||||||
|
var CloudDAVBase string
|
||||||
|
var CloudUsername string
|
||||||
|
var CloudPassword string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
|
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(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
|
||||||
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
||||||
flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts")
|
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()
|
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...")
|
log.Println("Opening database...")
|
||||||
if err := fic.DBInit(*dbfile); err != nil {
|
if err := fic.DBInit(*dbfile); err != nil {
|
||||||
log.Fatal("Cannot open the database: ", err)
|
log.Fatal("Cannot open the database: ", err)
|
||||||
|
|
|
@ -58,6 +58,7 @@ CREATE TABLE IF NOT EXISTS exercices(
|
||||||
);
|
);
|
||||||
CREATE TABLE IF NOT EXISTS exercice_files(
|
CREATE TABLE IF NOT EXISTS exercice_files(
|
||||||
id_file INTEGER NOT NULL PRIMARY KEY,
|
id_file INTEGER NOT NULL PRIMARY KEY,
|
||||||
|
origin TEXT NOT NULL,
|
||||||
path TEXT NOT NULL UNIQUE,
|
path TEXT NOT NULL UNIQUE,
|
||||||
id_exercice INTEGER NOT NULL,
|
id_exercice INTEGER NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
|
|
|
@ -5,10 +5,14 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var FilesDir string
|
||||||
|
|
||||||
type EFile struct {
|
type EFile struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
|
origin string
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
IdExercice int64 `json:"idExercice"`
|
IdExercice int64 `json:"idExercice"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -17,7 +21,7 @@ type EFile struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Exercice) GetFiles() ([]EFile, error) {
|
func (e Exercice) GetFiles() ([]EFile, error) {
|
||||||
if rows, err := DBQuery("SELECT id_file, path, name, sha1, size FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
|
if rows, err := DBQuery("SELECT id_file, origin, path, name, sha1, size FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
@ -26,7 +30,7 @@ func (e Exercice) GetFiles() ([]EFile, error) {
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var f EFile
|
var f EFile
|
||||||
f.IdExercice = e.Id
|
f.IdExercice = e.Id
|
||||||
if err := rows.Scan(&f.Id, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
|
if err := rows.Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
files = append(files, f)
|
files = append(files, f)
|
||||||
|
@ -39,7 +43,7 @@ func (e Exercice) GetFiles() ([]EFile, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Exercice) ImportFile(filePath string) (EFile, error) {
|
func (e Exercice) ImportFile(filePath string, origin string) (EFile, error) {
|
||||||
if fi, err := os.Stat(filePath); err != nil {
|
if fi, err := os.Stat(filePath); err != nil {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
} else if fd, err := os.Open(filePath); err != nil {
|
} else if fd, err := os.Open(filePath); err != nil {
|
||||||
|
@ -53,22 +57,22 @@ func (e Exercice) ImportFile(filePath string) (EFile, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var result []byte
|
var result []byte
|
||||||
return e.AddFile(filePath, path.Base(filePath), hash.Sum(result), fi.Size())
|
return e.AddFile(strings.TrimPrefix(filePath, FilesDir), origin, path.Base(filePath), hash.Sum(result), fi.Size())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Exercice) AddFile(path string, name string, checksum []byte, size int64) (EFile, error) {
|
func (e Exercice) AddFile(path string, origin string, name string, checksum []byte, size int64) (EFile, error) {
|
||||||
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, path, name, sha1, size) VALUES (?, ?, ?, ?, ?)", e.Id, path, name, checksum, size); err != nil {
|
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, origin, path, name, sha1, size) VALUES (?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, size); err != nil {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
} else if fid, err := res.LastInsertId(); err != nil {
|
} else if fid, err := res.LastInsertId(); err != nil {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
} else {
|
} else {
|
||||||
return EFile{fid, path, e.Id, name, checksum, size}, nil
|
return EFile{fid, origin, path, e.Id, name, checksum, size}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f EFile) Update() (int64, error) {
|
func (f EFile) Update() (int64, error) {
|
||||||
if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, path = ?, name = ?, sha1 = ?, size = ? WHERE id_file = ?", f.IdExercice, f.Path, f.Name, f.Checksum, f.Size, f.Id); err != nil {
|
if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, origin = ?, path = ?, name = ?, sha1 = ?, size = ? WHERE id_file = ?", f.IdExercice, f.origin, f.Path, f.Name, f.Checksum, f.Size, f.Id); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
} else if nb, err := res.RowsAffected(); err != nil {
|
} else if nb, err := res.RowsAffected(); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
Loading…
Reference in New Issue
Block a user