Cache file size

This commit is contained in:
nemunaire 2016-01-18 19:38:59 +01:00
parent 989d5a6349
commit c33c2b8e8a
2 changed files with 12 additions and 8 deletions

View File

@ -61,6 +61,7 @@ CREATE TABLE IF NOT EXISTS exercice_files(
id_exercice INTEGER NOT NULL,
name TEXT NOT NULL,
sha1 BLOB NOT NULL,
size INTEGER NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
CREATE TABLE IF NOT EXISTS exercice_keys(

View File

@ -13,10 +13,11 @@ type EFile struct {
IdExercice int64 `json:"idExercice"`
Name string `json:"name"`
Checksum []byte `json:"checksum"`
Size int64 `json:"size"`
}
func (e Exercice) GetFiles() ([]EFile, error) {
if rows, err := DBQuery("SELECT id_file, path, name, sha1 FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
if rows, err := DBQuery("SELECT id_file, path, name, sha1, size FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -25,7 +26,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); err != nil {
if err := rows.Scan(&f.Id, &f.Path, &f.Name, &f.Checksum, &f.Size); err != nil {
return nil, err
}
files = append(files, f)
@ -39,7 +40,9 @@ func (e Exercice) GetFiles() ([]EFile, error) {
}
func (e Exercice) ImportFile(filePath string) (EFile, error) {
if fd, err := os.Open(filePath); err != nil {
if fi, err := os.Stat(filePath); err != nil {
return EFile{}, err
} else if fd, err := os.Open(filePath); err != nil {
return EFile{}, err
} else {
defer fd.Close()
@ -50,22 +53,22 @@ func (e Exercice) ImportFile(filePath string) (EFile, error) {
}
var result []byte
return e.AddFile(filePath, path.Base(filePath), hash.Sum(result))
return e.AddFile(filePath, path.Base(filePath), hash.Sum(result), fi.Size())
}
}
func (e Exercice) AddFile(path string, name string, checksum []byte) (EFile, error) {
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, path, name, sha1) VALUES (?, ?, ?, ?)", e.Id, path, name, checksum); err != nil {
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 {
return EFile{}, err
} else if fid, err := res.LastInsertId(); err != nil {
return EFile{}, err
} else {
return EFile{fid, path, e.Id, name, checksum}, nil
return EFile{fid, 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 = ? WHERE id_file = ?", f.IdExercice, f.Path, f.Name, f.Checksum, f.Id); err != nil {
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 {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err