45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package store
|
|
|
|
import "context"
|
|
|
|
type FileRecord struct {
|
|
ID int64 `json:"id"`
|
|
SessionID string `json:"session_id"`
|
|
Kind string `json:"kind"`
|
|
Mime string `json:"mime"`
|
|
StoragePath string `json:"storage_path"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
SHA256 string `json:"sha256"`
|
|
ExpiresAt *string `json:"expires_at"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
func (s *Store) InsertFile(ctx context.Context, f *FileRecord) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO files (session_id, kind, mime, storage_path, size_bytes, sha256, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
f.SessionID, f.Kind, f.Mime, f.StoragePath, f.SizeBytes, f.SHA256, f.ExpiresAt,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func (s *Store) GetFile(ctx context.Context, sessionID string, id int64) (*FileRecord, error) {
|
|
var f FileRecord
|
|
err := s.db.QueryRowContext(ctx,
|
|
`SELECT id, session_id, kind, mime, storage_path, size_bytes, sha256, expires_at, created_at FROM files WHERE id = ? AND session_id = ?`, id, sessionID,
|
|
).Scan(&f.ID, &f.SessionID, &f.Kind, &f.Mime, &f.StoragePath, &f.SizeBytes, &f.SHA256, &f.ExpiresAt, &f.CreatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &f, nil
|
|
}
|
|
|
|
func (s *Store) CountFiles(ctx context.Context) (int, int64, error) {
|
|
var count int
|
|
var total int64
|
|
err := s.db.QueryRowContext(ctx, `SELECT COUNT(*), COALESCE(SUM(size_bytes),0) FROM files`).Scan(&count, &total)
|
|
return count, total, err
|
|
}
|