feat(lab4): MinIO S3 private bucket, streaming image validation, attachment endpoints, Grade 5 Track 2 reconciler
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,130 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/internal/model"
|
||||
"messenger/internal/s3store"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrBadRequest = errors.New("bad request")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrUnavailable = errors.New("service unavailable")
|
||||
|
||||
usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
IdempotencyKeyRegex = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
)
|
||||
|
||||
const TimestampFormat = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
func FormatTime(t time.Time) string {
|
||||
return t.UTC().Truncate(time.Microsecond).Format(TimestampFormat)
|
||||
}
|
||||
|
||||
func NowUTC() time.Time {
|
||||
return time.Now().UTC().Truncate(time.Microsecond)
|
||||
}
|
||||
|
||||
type MigrationFile struct {
|
||||
Version string
|
||||
SQL string
|
||||
}
|
||||
|
||||
type ReconcileReport struct {
|
||||
DryRun bool `json:"dry_run"`
|
||||
GracePeriodSeconds int64 `json:"grace_period_seconds"`
|
||||
LockAcquired bool `json:"lock_acquired"`
|
||||
S3ObjectsScanned int `json:"s3_objects_scanned"`
|
||||
DBRowsScanned int `json:"db_rows_scanned"`
|
||||
OrphanS3Found []string `json:"orphan_s3_found"`
|
||||
OrphanS3Deleted []string `json:"orphan_s3_deleted"`
|
||||
OrphanS3SkippedGrace []string `json:"orphan_s3_skipped_grace"`
|
||||
MissingS3Found []string `json:"missing_s3_found"`
|
||||
MissingS3MarkedFailed []string `json:"missing_s3_marked_failed"`
|
||||
MissingS3Deleted []string `json:"missing_s3_deleted"`
|
||||
MissingS3SkippedGrace []string `json:"missing_s3_skipped_grace"`
|
||||
HealthyPreserved int `json:"healthy_preserved"`
|
||||
S3ListCalls int `json:"s3_list_calls"`
|
||||
S3RemoveCalls int `json:"s3_remove_calls"`
|
||||
DBQueries int `json:"db_queries"`
|
||||
DurationMs float64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
type StoreStats struct {
|
||||
SQLQueries int64 `json:"sql_queries"`
|
||||
CacheHits int64 `json:"cache_hits"`
|
||||
CacheMisses int64 `json:"cache_misses"`
|
||||
SingleflightShared int64 `json:"singleflight_shared"`
|
||||
CacheMode string `json:"cache_mode"`
|
||||
WriteMode string `json:"write_mode"`
|
||||
RedisConnected bool `json:"redis_connected"`
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
Ready(ctx context.Context) error
|
||||
RunMigrations(ctx context.Context, migrations []MigrationFile) error
|
||||
CreateUser(ctx context.Context, req model.CreateUserRequest) (*model.User, error)
|
||||
AuthenticateAndCreateSession(ctx context.Context, username, password string, ttlSeconds int) (*model.Session, error)
|
||||
GetUserByToken(ctx context.Context, rawToken string) (*model.User, error)
|
||||
RevokeSession(ctx context.Context, rawToken string) error
|
||||
CreateChat(ctx context.Context, callerID string, req model.CreateChatRequest) (*model.Chat, error)
|
||||
ListChats(ctx context.Context, callerID string, limit int, cursorStr string) (*model.ChatPage, error)
|
||||
GetChat(ctx context.Context, callerID, chatID string) (*model.Chat, error)
|
||||
IsChatMember(ctx context.Context, callerID, chatID string) (bool, error)
|
||||
CreateAttachment(ctx context.Context, att model.Attachment) (*model.Attachment, error)
|
||||
GetAttachment(ctx context.Context, callerID, attachmentID string) (*model.Attachment, error)
|
||||
Reconcile(ctx context.Context, s3 *s3store.Client, gracePeriod time.Duration, dryRun bool) (*ReconcileReport, error)
|
||||
CreateMessage(ctx context.Context, callerID, chatID, idempotencyKey string, idempotencyTTLSeconds int, req model.CreateMessageRequest) (*model.Message, error)
|
||||
ListMessages(ctx context.Context, callerID, chatID string, limit int, cursorStr string) (*model.MessagePage, error)
|
||||
Stats(ctx context.Context) StoreStats
|
||||
ResetStats()
|
||||
SetCacheMode(mode string)
|
||||
Close()
|
||||
}
|
||||
|
||||
func EncodeCursor(scope, timestamp, id string) string {
|
||||
data := model.CursorData{
|
||||
Scope: scope,
|
||||
Time: timestamp,
|
||||
ID: id,
|
||||
}
|
||||
b, _ := json.Marshal(data)
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
func DecodeCursor(cursorStr, expectedScope string) (time.Time, string, error) {
|
||||
b, err := base64.RawURLEncoding.DecodeString(cursorStr)
|
||||
if err != nil {
|
||||
b, err = base64.URLEncoding.DecodeString(cursorStr)
|
||||
if err != nil {
|
||||
b, err = base64.StdEncoding.DecodeString(cursorStr)
|
||||
if err != nil {
|
||||
return time.Time{}, "", ErrBadRequest
|
||||
}
|
||||
}
|
||||
}
|
||||
var data model.CursorData
|
||||
if err := json.Unmarshal(b, &data); err != nil {
|
||||
return time.Time{}, "", ErrBadRequest
|
||||
}
|
||||
if data.Scope != expectedScope || data.Time == "" || data.ID == "" {
|
||||
return time.Time{}, "", ErrBadRequest
|
||||
}
|
||||
if _, err := uuid.Parse(data.ID); err != nil {
|
||||
return time.Time{}, "", ErrBadRequest
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, data.Time)
|
||||
if err != nil {
|
||||
return time.Time{}, "", ErrBadRequest
|
||||
}
|
||||
return t.UTC().Truncate(time.Microsecond), data.ID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user