Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -3,14 +3,17 @@ package attachment
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/faultbarrier"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
@@ -23,8 +26,9 @@ type Service struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
MaxBytes int64
|
||||
MaxPixels int64
|
||||
MaxBytes int64
|
||||
MaxPixels int64
|
||||
BarrierDir string
|
||||
}
|
||||
|
||||
// A nil blobs dependency allows the text API to run without configured S3.
|
||||
@@ -70,7 +74,11 @@ func (s *Service) Upload(ctx context.Context, actor uuid.UUID, input UploadInput
|
||||
}
|
||||
id := ident.New()
|
||||
stage := "staging/" + id.String()
|
||||
discard := true
|
||||
defer func() {
|
||||
if !discard {
|
||||
return
|
||||
}
|
||||
cleanup, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
// A failed cleanup leaves a staging object for the reconciler's grace-based collection.
|
||||
@@ -119,37 +127,30 @@ func (s *Service) Upload(ctx context.Context, actor uuid.UUID, input UploadInput
|
||||
FileHash: hash.Sum(nil),
|
||||
SizeBytes: input.SizeBytes,
|
||||
MIMEType: input.MIMEType,
|
||||
Status: FileReady,
|
||||
Status: FileQueued,
|
||||
UpdatedAt: ident.TimeOf(id),
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
}
|
||||
if err := s.attachments.WithPublication(ctx, a, func(p Publication) error {
|
||||
if err := s.publishBlob(ctx, a, stage, p.FileReady()); err != nil {
|
||||
return err
|
||||
if s.config.BarrierDir != "" {
|
||||
base := filepath.Join(s.config.BarrierDir, hex.EncodeToString(a.FileHash)+"-staged")
|
||||
if err := faultbarrier.Wait(ctx, base, a.ID.String()); err != nil {
|
||||
return Attachment{}, unavailable(err)
|
||||
}
|
||||
return p.SaveReady(ctx)
|
||||
}); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *Service) publishBlob(ctx context.Context, a Attachment, stagingKey string, fileReady bool) error {
|
||||
blobKey := BlobKey(a.FileHash)
|
||||
if fileReady {
|
||||
obj, err := s.blobs.Stat(ctx, blobKey)
|
||||
if err == nil && obj.Size == a.SizeBytes {
|
||||
return nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, ErrObjectMissing) {
|
||||
// COMMIT can succeed even if its reply is lost. From this point only the
|
||||
// collector, after consulting durable job state, may delete this source.
|
||||
discard = false
|
||||
err = s.attachments.Enqueue(ctx, a, stage, func() error {
|
||||
obj, err := s.blobs.Stat(ctx, stage)
|
||||
if err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
}
|
||||
// The hash is known after streaming. Copy the validated staging object to
|
||||
// its permanent CAS key while the repository's publication lock is held.
|
||||
if err := s.blobs.Copy(ctx, stagingKey, blobKey); err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
return nil
|
||||
if obj.Size != a.SizeBytes {
|
||||
return unavailable(errors.New("staging size changed"))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return a, err
|
||||
}
|
||||
|
||||
func (s *Service) validate(ctx context.Context, stage, mime string) (err error) {
|
||||
@@ -189,25 +190,38 @@ func (r *storageReader) Read(p []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, actor, id uuid.UUID) (Attachment, error) {
|
||||
a, err := s.attachments.GetForMember(ctx, id, actor)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if a.Status != FileReady {
|
||||
return Attachment{}, apperr.Conflict("attachment is not ready")
|
||||
}
|
||||
return a, nil
|
||||
return s.attachments.GetForMember(ctx, id, actor)
|
||||
}
|
||||
|
||||
func (s *Service) Content(ctx context.Context, actor, id uuid.UUID) (Attachment, io.ReadCloser, error) {
|
||||
func (s *Service) Content(ctx context.Context, actor, id uuid.UUID, name string) (Attachment, io.ReadCloser, error) {
|
||||
a, err := s.Get(ctx, actor, id)
|
||||
if err != nil {
|
||||
return Attachment{}, nil, err
|
||||
}
|
||||
if a.Status != FileReady {
|
||||
return Attachment{}, nil, apperr.Conflict("attachment is not ready")
|
||||
}
|
||||
key := BlobKey(a.FileHash)
|
||||
if name != "original" {
|
||||
found := false
|
||||
for _, v := range a.Variants {
|
||||
if name == v.Name {
|
||||
key, a.MIMEType, a.SizeBytes = v.ObjectKey, v.MIMEType, v.SizeBytes
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return Attachment{}, nil, apperr.NotFound("variant not found")
|
||||
}
|
||||
}
|
||||
if s.blobs == nil {
|
||||
return Attachment{}, nil, apperr.Unavailable("object storage is unavailable")
|
||||
}
|
||||
r, err := s.blobs.Open(ctx, BlobKey(a.FileHash))
|
||||
r, err := s.blobs.Open(ctx, key)
|
||||
if name != "original" && errors.Is(err, ErrObjectMissing) {
|
||||
return Attachment{}, nil, apperr.NotFound("variant not found")
|
||||
}
|
||||
if err != nil {
|
||||
return Attachment{}, nil, unavailable(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user