81 lines
1.2 KiB
Go
81 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
// External
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
// Standard
|
|
"time"
|
|
)
|
|
|
|
type File struct {
|
|
ID int
|
|
UserID int `db:"user_id"`
|
|
NodeID int `db:"node_id"`
|
|
Filename string
|
|
Size int64
|
|
MIME string
|
|
MD5 string
|
|
Uploaded time.Time
|
|
}
|
|
|
|
func AddFile(userID int, file *File) (err error) { // {{{
|
|
file.UserID = userID
|
|
|
|
var rows *sqlx.Rows
|
|
rows, err = db.Queryx(`
|
|
INSERT INTO file(user_id, node_id, filename, size, mime, md5)
|
|
VALUES($1, $2, $3, $4, $5, $6)
|
|
RETURNING id
|
|
`,
|
|
file.UserID,
|
|
file.NodeID,
|
|
file.Filename,
|
|
file.Size,
|
|
file.MIME,
|
|
file.MD5,
|
|
)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
rows.Next()
|
|
err = rows.Scan(&file.ID)
|
|
return
|
|
} // }}}
|
|
func Files(userID int, nodeUUID string, fileID int) (files []File, err error) { // {{{
|
|
var rows *sqlx.Rows
|
|
rows, err = db.Queryx(
|
|
`SELECT *
|
|
FROM file
|
|
WHERE
|
|
user_id = $1 AND
|
|
node_uuid = $2 AND
|
|
CASE $3::int
|
|
WHEN 0 THEN true
|
|
ELSE id = $3
|
|
END`,
|
|
userID,
|
|
nodeUUID,
|
|
fileID,
|
|
)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
files = []File{}
|
|
for rows.Next() {
|
|
file := File{}
|
|
if err = rows.StructScan(&file); err != nil {
|
|
return
|
|
}
|
|
files = append(files, file)
|
|
}
|
|
|
|
return
|
|
} // }}}
|
|
|
|
// vim: foldmethod=marker
|