Import file from owncloud
This commit is contained in:
parent
97bb149eb6
commit
69ad64715c
6 changed files with 104 additions and 28 deletions
|
|
@ -58,6 +58,7 @@ CREATE TABLE IF NOT EXISTS exercices(
|
|||
);
|
||||
CREATE TABLE IF NOT EXISTS exercice_files(
|
||||
id_file INTEGER NOT NULL PRIMARY KEY,
|
||||
origin TEXT NOT NULL,
|
||||
path TEXT NOT NULL UNIQUE,
|
||||
id_exercice INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
|
|
|
|||
|
|
@ -5,10 +5,14 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var FilesDir string
|
||||
|
||||
type EFile struct {
|
||||
Id int64 `json:"id"`
|
||||
origin string
|
||||
Path string `json:"path"`
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
Name string `json:"name"`
|
||||
|
|
@ -17,7 +21,7 @@ type EFile struct {
|
|||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
|
@ -26,7 +30,7 @@ func (e Exercice) GetFiles() ([]EFile, error) {
|
|||
for rows.Next() {
|
||||
var f EFile
|
||||
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
|
||||
}
|
||||
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 {
|
||||
return EFile{}, err
|
||||
} else if fd, err := os.Open(filePath); err != nil {
|
||||
|
|
@ -53,22 +57,22 @@ func (e Exercice) ImportFile(filePath string) (EFile, error) {
|
|||
}
|
||||
|
||||
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) {
|
||||
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, path, name, sha1, size) VALUES (?, ?, ?, ?, ?)", e.Id, path, name, checksum, size); err != nil {
|
||||
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, origin, path, name, sha1, size) VALUES (?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, size); err != nil {
|
||||
return EFile{}, err
|
||||
} else if fid, err := res.LastInsertId(); err != nil {
|
||||
return EFile{}, err
|
||||
} 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) {
|
||||
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
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
|
|
|
|||
Reference in a new issue