171 lines
3.7 KiB
Go
171 lines
3.7 KiB
Go
package db
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"strconv"
|
|
)
|
|
|
|
// newWithProperties Creates a new file with the given properties
|
|
func (file *File) newWithProperties(name string, url string, comment string, blob string) (*File, error) {
|
|
file = &File{
|
|
Name: name,
|
|
Comment: &comment,
|
|
Blob: blob,
|
|
Properties: FileProperties{
|
|
Url: url,
|
|
},
|
|
}
|
|
if err := conn.Create(file).Error; err != nil {
|
|
return file, err
|
|
|
|
}
|
|
|
|
return file, nil
|
|
}
|
|
|
|
// creates new empty file
|
|
func (file *File) new() (*File,error) {
|
|
if err := conn.Create(file).Error; err != nil {
|
|
return file, err
|
|
}
|
|
return file, nil
|
|
}
|
|
|
|
// save writes the given file to the Database
|
|
func (file *File) Save() error {
|
|
if err := conn.Save(file).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (file *File) delete() error {
|
|
if err := conn.Delete(file).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Status() gives the status of the file because GORM currently doesn't do ManyToOne, so to make this work well I'll
|
|
// have to ditch the ORM and go raw SQL
|
|
func (file *File) Status() (Status) {
|
|
var status Status
|
|
if err := conn.First(&status, file.StatusID); err != nil {
|
|
log.Printf("Could not get Status of File %d: %v", file.ID, err)
|
|
status.ID = 18446744073709551615
|
|
status.Name = "Unknown"
|
|
}
|
|
return status
|
|
}
|
|
|
|
func (file *File) Creator() (User) {
|
|
var user User
|
|
if err := conn.First(&user, file.CreatorID); err != nil {
|
|
log.Printf("Could not get Creator of File %d: %v", file.ID, err)
|
|
user.ID = 18446744073709551615
|
|
user.Name = "Unknown"
|
|
}
|
|
return user
|
|
}
|
|
|
|
|
|
func (file *File) Assignee() (User) {
|
|
var user User
|
|
if err := conn.First(&user, file.AssigneeID); err != nil {
|
|
log.Printf("Could not get Creator of File %d: %v", file.ID, err)
|
|
user.ID = 18446744073709551615
|
|
user.Name = "Unknown"
|
|
}
|
|
return user
|
|
}
|
|
|
|
func CreateFile(name string, url string, comment string, blob string) (uint, error) {
|
|
file := File{
|
|
Name: name,
|
|
Comment: &comment,
|
|
Blob: blob,
|
|
Properties: FileProperties{
|
|
Url: url,
|
|
},
|
|
}
|
|
if err := conn.Create(&file).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return file.ID, nil
|
|
}
|
|
|
|
func UpdateOriginalName(id uint, originalName string) {
|
|
var file File
|
|
conn.First(&file, id)
|
|
file.Properties.OriginalName = originalName
|
|
conn.Save(&file)
|
|
}
|
|
|
|
func SetSimpleAttributes(id uint, mime string, size int64, sha256 string, sha1 string, md5 string, fileCmd string, extension string) {
|
|
var file File
|
|
conn.First(&file, id)
|
|
file.Properties.Mime = mime
|
|
file.Properties.Size = size
|
|
file.Properties.Sha256 = sha256
|
|
file.Properties.Sha1 = sha1
|
|
file.Properties.Md5 = md5
|
|
file.Properties.FileCmd = fileCmd
|
|
file.Properties.Extension = extension
|
|
conn.Save(&file)
|
|
}
|
|
|
|
func CountFilesEntries() int64 {
|
|
var count int64
|
|
var file File
|
|
conn.Find(&file).Count(&count)
|
|
return count
|
|
}
|
|
|
|
func GetFileByID(id uint) *File {
|
|
var file File
|
|
conn.First(&file, id)
|
|
return &file
|
|
}
|
|
|
|
func GetFileByBlob(blob string) *File {
|
|
var file File
|
|
conn.Where("blob = ?", blob).First(&file)
|
|
return &file
|
|
}
|
|
|
|
func GetFileBlob(id uint) string {
|
|
var file File
|
|
conn.Select("blob").First(&file, id)
|
|
return file.Blob
|
|
}
|
|
|
|
func GetFileList(page int, count int, c *gin.Context) ([]*File, error) {
|
|
query, err := searchQueryBuilder(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var list []*File
|
|
conn.Limit(page * count).Where(query).Find(&list)
|
|
|
|
return list, nil
|
|
}
|
|
|
|
func searchQueryBuilder(c *gin.Context) (*File, error) {
|
|
// TODO Add parsing for all File properties
|
|
var query File
|
|
if c.Query("id") != "" {
|
|
id, err := strconv.ParseUint(c.Query("id"), 10, 0)
|
|
if err != nil {
|
|
log.Printf("Could not Convert ID from filter to int: %v", err)
|
|
return nil, err
|
|
}
|
|
query.ID = uint(id)
|
|
}
|
|
if c.Query("name") != "" {
|
|
query.Name = c.Query("name")
|
|
}
|
|
|
|
return &query, nil
|
|
}
|