lab5 done
This commit is contained in:
@@ -2,6 +2,7 @@ package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
@@ -19,32 +20,35 @@ const UploadTimeout = 45 * time.Second
|
||||
const MinimumGrace = 90 * time.Second
|
||||
|
||||
type Attachment struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
Size int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Key string `json:"-"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
Size int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Key string `json:"-"`
|
||||
PipelineVersion int `json:"-"`
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Pool *pgxpool.Pool
|
||||
Store *Store
|
||||
slots chan struct{}
|
||||
Pool *pgxpool.Pool
|
||||
Store *Store
|
||||
slots chan struct{}
|
||||
Async bool
|
||||
Options Options
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool, store *Store) *Service {
|
||||
return &Service{Pool: pool, Store: store, slots: make(chan struct{}, 1)}
|
||||
}
|
||||
func (s *Service) Ready(ctx context.Context) error { return s.Store.Ready(ctx) }
|
||||
func (s *Service) Upload(ctx context.Context, chatID, userID, name, contentType string, body io.Reader) (Attachment, error) {
|
||||
func (s *Service) uploadSync(ctx context.Context, chatID, userID, name, contentType string, body io.Reader) (Attachment, error) {
|
||||
if err := ValidateFilename(name); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
@@ -98,17 +102,19 @@ func (s *Service) Get(ctx context.Context, id, userID string) (Attachment, error
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
a := Attachment{Variants: []any{}}
|
||||
err = s.Pool.QueryRow(ctx, `SELECT a.id::text,a.chat_id::text,a.uploader_id::text,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.status,a.error_code,a.created_at FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=$1 AND m.user_id=$2`, parsed.String(), userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.Key, &a.ContentType, &a.Size, &a.SHA256, &a.Status, &a.ErrorCode, &a.CreatedAt)
|
||||
var variants []byte
|
||||
err = s.Pool.QueryRow(ctx, `SELECT a.id::text,a.chat_id::text,a.uploader_id::text,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.status,a.error_code,a.created_at,a.updated_at,a.variants,a.pipeline_version FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=$1 AND m.user_id=$2`, parsed.String(), userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.Key, &a.ContentType, &a.Size, &a.SHA256, &a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt, &variants, &a.PipelineVersion)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return Attachment{}, appError(domain.ErrorNotFound, "Объект не найден")
|
||||
}
|
||||
if err != nil {
|
||||
return Attachment{}, unavailable()
|
||||
}
|
||||
a.UpdatedAt = a.CreatedAt
|
||||
// Keep the mandatory lab4 ready-only metadata schema. Failed originals are
|
||||
// represented by 409; internal status remains inspectable in reconciliation.
|
||||
if a.Status != "ready" {
|
||||
if err = json.Unmarshal(variants, &a.Variants); err != nil {
|
||||
return Attachment{}, unavailable()
|
||||
}
|
||||
// The legacy fixture service preserves lab4 errors; production Async exposes all states.
|
||||
if a.Status != "ready" && !s.Async {
|
||||
return Attachment{}, appError(domain.ErrorConflict, "Оригинал вложения недоступен")
|
||||
}
|
||||
return a, nil
|
||||
|
||||
Reference in New Issue
Block a user