lab5: исправления второго ревью

Reprocess of a queued/processing/failed upload now sources from verified staging instead of the not-yet-written final original, which used to supersede v1 and strand the attachment in queued. PNG uploads now inflate the concatenated IDAT stream (bounded by IHDR dimensions) before acceptance, so a structurally valid PNG whose IDAT is not a zlib stream is rejected with 415 instead of 202.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 00:41:38 +03:00
parent ecdd91a15b
commit b965350cc9
13 changed files with 311 additions and 44 deletions
@@ -1338,9 +1338,24 @@ func (r *PipelineRepository) Reprocess(
return fmt.Errorf("%w: target pipeline version must be newer", domain.ErrConflict)
}
sourceKey := attachment.ObjectKey
if attachment.Status == domain.AttachmentPending {
return fmt.Errorf("%w: attachment upload is not complete", domain.ErrConflict)
}
// The verified upload lives in staging until the first pipeline
// publishes the final original. A queued/processing/failed attachment
// must reprocess from staging: object_key points at a final original
// that may not exist yet (worker stopped before the first run), and
// choosing it would supersede v1 and leave the attachment queued with
// no jobs after v2 fails with source_missing.
sourceKey := attachment.StagingKey
if attachment.Status == domain.AttachmentReady {
// Ready means the final original was published; staging may have
// passed its retention window already.
sourceKey = attachment.ObjectKey
}
if sourceKey == "" {
sourceKey = attachment.StagingKey
sourceKey = attachment.ObjectKey
}
if sourceKey == "" {
return fmt.Errorf("%w: attachment has no source object", domain.ErrConflict)
@@ -158,6 +158,69 @@ func (s *PipelineSuite) TestAdmissionCountsPendingPresignAndIsAtomic() {
s.Require().ErrorIs(err, domain.ErrRateLimited)
}
// A queued upload whose worker never ran still has its verified bytes only in
// staging. Reprocessing it must use staging as the source; pointing at the
// not-yet-written final original left the attachment queued forever after the
// v1 job was superseded (v2 then failed with source_missing).
func (s *PipelineSuite) TestReprocessQueuedUsesStagingSource() {
alice := s.createUser("alice")
bob := s.createUser("bob")
chat := s.createChat(alice, bob)
draft, job := s.attachmentDraft(chat, alice, 1)
repo := s.store.Pipeline()
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
reprocessID, err := uuid.NewV7()
s.Require().NoError(err)
created, err := repo.Reprocess(s.T().Context(), draft.ID, 2, 5, reprocessID)
s.Require().NoError(err)
s.Equal(2, created.PipelineVersion)
s.Equal(draft.StagingKey, created.SourceKey)
}
// Once a pipeline has published the final original, the staging copy may be
// past its retention window; a reprocess of a ready attachment must source
// from the final object that actually exists.
func (s *PipelineSuite) TestReprocessReadyUsesObjectSource() {
alice := s.createUser("alice")
chat := s.createChat(alice)
draft, job := s.attachmentDraft(chat, alice, 1)
repo := s.store.Pipeline()
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
message := domain.NewJobMessage(job)
claim, err := repo.ClaimJob(s.T().Context(), message, "lease-1", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
variants := []domain.Variant{
{
Name: domain.VariantThumbnail, ContentType: "image/jpeg", SizeBytes: 11,
Width: 256, Height: 160, SHA256: strings.Repeat("b", 64),
ObjectKey: domain.VariantObjectKey(chat.ID, draft.ID, 1, domain.VariantThumbnail),
},
}
ok, err := repo.CompleteJob(s.T().Context(), job.ID, 1, "lease-1", variants)
s.Require().NoError(err)
s.True(ok)
reprocessID, err := uuid.NewV7()
s.Require().NoError(err)
created, err := repo.Reprocess(s.T().Context(), draft.ID, 2, 5, reprocessID)
s.Require().NoError(err)
s.Equal(2, created.PipelineVersion)
s.Equal(draft.ObjectKey, created.SourceKey)
}
func (s *PipelineSuite) TestReconcileQueuedJobsRepublishesLostIntent() {
alice := s.createUser("alice")
chat := s.createChat(alice)