Files
Atlas/internal/store/files.go
T
renjue 2b020bb782
CI / docker (push) Successful in 2m58s
实现 Atlas AI 平台一期能力并完成品牌化改造。
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 23:35:19 +08:00

56 lines
1.9 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) GetFileByID(ctx context.Context, 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 = ?`, id,
).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) 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
}