|
|
|
@ -44,6 +44,8 @@ type EFile struct {
|
|
|
|
|
ChecksumShown []byte `json:"checksum_shown"`
|
|
|
|
|
// Size contains the cached size of the file
|
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
|
// Published indicates if the file should be shown or not
|
|
|
|
|
Published bool `json:"published"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewDummyFile creates an EFile, without any link to an actual Exercice File.
|
|
|
|
@ -58,12 +60,13 @@ func (e *Exercice) NewDummyFile(origin string, dest string, checksum []byte, siz
|
|
|
|
|
Checksum: checksum,
|
|
|
|
|
ChecksumShown: nil,
|
|
|
|
|
Size: size,
|
|
|
|
|
Published: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFiles returns a list of all files living in the database.
|
|
|
|
|
func GetFiles() ([]*EFile, error) {
|
|
|
|
|
if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, cksum_shown, size FROM exercice_files"); err != nil {
|
|
|
|
|
if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files"); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
defer rows.Close()
|
|
|
|
@ -71,7 +74,7 @@ func GetFiles() ([]*EFile, error) {
|
|
|
|
|
files := []*EFile{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
f := &EFile{}
|
|
|
|
|
if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size); err != nil {
|
|
|
|
|
if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
files = append(files, f)
|
|
|
|
@ -87,13 +90,13 @@ func GetFiles() ([]*EFile, error) {
|
|
|
|
|
// GetFile retrieves the file with the given id.
|
|
|
|
|
func GetFile(id int64) (f *EFile, err error) {
|
|
|
|
|
f = &EFile{}
|
|
|
|
|
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size FROM exercice_files WHERE id_file = ?", id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size)
|
|
|
|
|
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_file = ?", id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exercice) GetFile(id int64) (f *EFile, err error) {
|
|
|
|
|
f = &EFile{}
|
|
|
|
|
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size FROM exercice_files WHERE id_file = ? AND id_exercice = ?", id, e.Id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size)
|
|
|
|
|
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_file = ? AND id_exercice = ?", id, e.Id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,7 +105,7 @@ func GetFileByPath(path string) (*EFile, error) {
|
|
|
|
|
path = strings.TrimPrefix(path, FilesDir)
|
|
|
|
|
|
|
|
|
|
f := &EFile{}
|
|
|
|
|
if err := DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size FROM exercice_files WHERE path = ?", path).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size); err != nil {
|
|
|
|
|
if err := DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size, published FROM exercice_files WHERE path = ?", path).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil {
|
|
|
|
|
return f, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -114,14 +117,14 @@ func (e *Exercice) GetFileByFilename(filename string) (f *EFile, err error) {
|
|
|
|
|
filename = path.Base(filename)
|
|
|
|
|
|
|
|
|
|
f = &EFile{}
|
|
|
|
|
err = DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size FROM exercice_files WHERE id_exercice = ? AND origin LIKE ?", e.Id, "%/"+filename).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size)
|
|
|
|
|
err = DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_exercice = ? AND origin LIKE ?", e.Id, "%/"+filename).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFiles returns a list of files coming with the challenge.
|
|
|
|
|
func (e *Exercice) GetFiles() ([]*EFile, error) {
|
|
|
|
|
if rows, err := DBQuery("SELECT id_file, origin, path, name, cksum, cksum_shown, size FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
|
|
|
|
|
if rows, err := DBQuery("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
defer rows.Close()
|
|
|
|
@ -130,7 +133,7 @@ func (e *Exercice) GetFiles() ([]*EFile, error) {
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
f := &EFile{}
|
|
|
|
|
f.IdExercice = e.Id
|
|
|
|
|
if err := rows.Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size); err != nil {
|
|
|
|
|
if err := rows.Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
files = append(files, f)
|
|
|
|
@ -148,7 +151,7 @@ func (e *Exercice) GetFileByPath(path string) (*EFile, error) {
|
|
|
|
|
path = strings.TrimPrefix(path, FilesDir)
|
|
|
|
|
|
|
|
|
|
f := &EFile{}
|
|
|
|
|
if err := DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size FROM exercice_files WHERE id_exercice = ? AND path = ?", e.Id, path).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size); err != nil {
|
|
|
|
|
if err := DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_exercice = ? AND path = ?", e.Id, path).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -229,13 +232,13 @@ func checkFileHash(filePath string, digest []byte) (dgst []byte, size int64, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ImportFile registers (ou updates if it already exists in database) the file in database.
|
|
|
|
|
func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, digestshown []byte) (interface{}, error) {
|
|
|
|
|
func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, digestshown []byte, published bool) (interface{}, error) {
|
|
|
|
|
if result512, size, err := checkFileHash(filePath, digest); !OptionalDigest && err != nil {
|
|
|
|
|
return EFile{}, err
|
|
|
|
|
} else {
|
|
|
|
|
dPath := strings.TrimPrefix(filePath, FilesDir)
|
|
|
|
|
if f, err := e.GetFileByPath(dPath); err != nil {
|
|
|
|
|
return e.AddFile(dPath, origin, path.Base(filePath), result512, digestshown, size)
|
|
|
|
|
return e.AddFile(dPath, origin, path.Base(filePath), result512, digestshown, size, published)
|
|
|
|
|
} else {
|
|
|
|
|
// Don't need to update Path and Name, because they are related to dPath
|
|
|
|
|
|
|
|
|
@ -244,6 +247,7 @@ func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, dig
|
|
|
|
|
f.Checksum = result512
|
|
|
|
|
f.ChecksumShown = digestshown
|
|
|
|
|
f.Size = size
|
|
|
|
|
f.Published = published
|
|
|
|
|
|
|
|
|
|
if _, err := f.Update(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
@ -255,19 +259,19 @@ func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, dig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddFile creates and fills a new struct File and registers it into the database.
|
|
|
|
|
func (e *Exercice) AddFile(path string, origin string, name string, checksum []byte, checksumshown []byte, size int64) (*EFile, error) {
|
|
|
|
|
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, origin, path, name, cksum, cksum_shown, size) VALUES (?, ?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, checksumshown, size); err != nil {
|
|
|
|
|
func (e *Exercice) AddFile(path string, origin string, name string, checksum []byte, checksumshown []byte, size int64, published bool) (*EFile, error) {
|
|
|
|
|
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, origin, path, name, cksum, cksum_shown, size, published) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, checksumshown, size, published); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else if fid, err := res.LastInsertId(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
return &EFile{fid, origin, path, e.Id, name, checksum, checksumshown, size}, nil
|
|
|
|
|
return &EFile{fid, origin, path, e.Id, name, checksum, checksumshown, size, published}, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update applies modifications back to the database.
|
|
|
|
|
func (f *EFile) Update() (int64, error) {
|
|
|
|
|
if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, origin = ?, path = ?, name = ?, cksum = ?, cksum_shown = ?, size = ? WHERE id_file = ?", f.IdExercice, f.origin, f.Path, f.Name, f.Checksum, f.ChecksumShown, f.Size, f.Id); err != nil {
|
|
|
|
|
if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, origin = ?, path = ?, name = ?, cksum = ?, cksum_shown = ?, size = ?, published = ? WHERE id_file = ?", f.IdExercice, f.origin, f.Path, f.Name, f.Checksum, f.ChecksumShown, f.Size, f.Published, f.Id); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|