feat(backend): handle processing errors and add PWA routes
- Block file downloads and previews with a 424 StatusFailedDependency if file processing failed or the box has issues. - Register routes for `/service-worker.js` and `/share-target` to support PWA features. - Update README.md with an AI usage disclosure.
This commit is contained in:
@@ -369,19 +369,20 @@ func (s *UploadService) FinalizeProcessingResumableSession(ctx context.Context,
|
||||
}
|
||||
backend, err := s.storage.Backend(box.StorageBackendID)
|
||||
if err != nil {
|
||||
_ = s.markProcessingBoxFailed(box, err)
|
||||
return UploadResult{}, err
|
||||
}
|
||||
for i, incoming := range staged {
|
||||
source, err := incoming.Open()
|
||||
if err != nil {
|
||||
_ = s.markProcessingBoxFailed(box, err)
|
||||
return UploadResult{}, err
|
||||
}
|
||||
file := box.Files[i]
|
||||
if err := s.writeUploadedObject(ctx, backend, file.ObjectKey, source, incoming.Size(), 0, incoming.ContentType()); err != nil {
|
||||
source.Close()
|
||||
_ = backend.Delete(context.Background(), file.ObjectKey)
|
||||
box.Files[i].ProcessingError = err.Error()
|
||||
_ = s.saveBoxRecord(box)
|
||||
_ = s.markProcessingBoxFailed(box, err)
|
||||
return UploadResult{}, err
|
||||
}
|
||||
source.Close()
|
||||
@@ -406,6 +407,35 @@ func (s *UploadService) FinalizeProcessingResumableSession(ctx context.Context,
|
||||
return s.resultForBox(box, ""), nil
|
||||
}
|
||||
|
||||
func (s *UploadService) markProcessingBoxFailed(box Box, cause error) error {
|
||||
message := "upload processing failed"
|
||||
if cause != nil && strings.TrimSpace(cause.Error()) != "" {
|
||||
message = cause.Error()
|
||||
}
|
||||
s.logger.Warn("resumable upload box marked failed", "source", "user-upload", "severity", "warn", "code", 4021, "box_id", box.ID, "backend_id", s.BoxStorageBackendID(box), "files", len(box.Files), "error", message)
|
||||
now := time.Now().UTC()
|
||||
box.Trouble = true
|
||||
box.TroubleReason = message
|
||||
for i := range box.Files {
|
||||
if box.Files[i].Processing || box.Files[i].ProcessingError == "" {
|
||||
box.Files[i].Processing = false
|
||||
box.Files[i].ProcessingError = message
|
||||
if box.Files[i].UploadedAt.IsZero() {
|
||||
box.Files[i].UploadedAt = now
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := s.saveBoxRecord(box); err != nil {
|
||||
s.logger.Warn("failed to save failed upload box state", "source", "user-upload", "severity", "warn", "code", 4022, "box_id", box.ID, "backend_id", s.BoxStorageBackendID(box), "error", err.Error())
|
||||
return err
|
||||
}
|
||||
if err := s.writeBoxMetadata(box); err != nil {
|
||||
s.logger.Warn("failed to write failed upload box metadata", "source", "user-upload", "severity", "warn", "code", 4023, "box_id", box.ID, "backend_id", s.BoxStorageBackendID(box), "error", err.Error())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UploadService) CompleteUploadedResumableSession(ctx context.Context, sessionID string) (UploadResult, ResumableSession, error) {
|
||||
session, err := s.GetResumableSession(sessionID)
|
||||
if err != nil {
|
||||
|
||||
@@ -35,26 +35,35 @@ func (b *s3StorageBackend) ID() string { return b.cfg.ID }
|
||||
func (b *s3StorageBackend) Type() string { return StorageBackendS3 }
|
||||
|
||||
func (b *s3StorageBackend) Put(ctx context.Context, key string, body io.Reader, size int64, contentType string) error {
|
||||
cleanKey := cleanObjectKey(key)
|
||||
opts := minio.PutObjectOptions{ContentType: contentType}
|
||||
_, err := b.client.PutObject(ctx, b.cfg.Bucket, cleanObjectKey(key), body, size, opts)
|
||||
return err
|
||||
_, err := b.client.PutObject(ctx, b.cfg.Bucket, cleanKey, body, size, opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("s3 put object %q in bucket %q failed: %w", cleanKey, b.cfg.Bucket, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *s3StorageBackend) Get(ctx context.Context, key string) (StorageObject, error) {
|
||||
object, err := b.client.GetObject(ctx, b.cfg.Bucket, cleanObjectKey(key), minio.GetObjectOptions{})
|
||||
cleanKey := cleanObjectKey(key)
|
||||
object, err := b.client.GetObject(ctx, b.cfg.Bucket, cleanKey, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return StorageObject{}, err
|
||||
return StorageObject{}, fmt.Errorf("s3 get object %q from bucket %q failed: %w", cleanKey, b.cfg.Bucket, err)
|
||||
}
|
||||
info, err := object.Stat()
|
||||
if err != nil {
|
||||
object.Close()
|
||||
return StorageObject{}, err
|
||||
return StorageObject{}, fmt.Errorf("s3 stat object %q in bucket %q failed: %w", cleanKey, b.cfg.Bucket, err)
|
||||
}
|
||||
return StorageObject{Key: key, Size: info.Size, ContentType: info.ContentType, ModTime: info.LastModified, Body: object}, nil
|
||||
}
|
||||
|
||||
func (b *s3StorageBackend) Delete(ctx context.Context, key string) error {
|
||||
return b.client.RemoveObject(ctx, b.cfg.Bucket, cleanObjectKey(key), minio.RemoveObjectOptions{})
|
||||
cleanKey := cleanObjectKey(key)
|
||||
if err := b.client.RemoveObject(ctx, b.cfg.Bucket, cleanKey, minio.RemoveObjectOptions{}); err != nil {
|
||||
return fmt.Errorf("s3 delete object %q from bucket %q failed: %w", cleanKey, b.cfg.Bucket, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *s3StorageBackend) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -62,7 +71,7 @@ func (b *s3StorageBackend) DeletePrefix(ctx context.Context, prefix string) erro
|
||||
objects := b.client.ListObjects(ctx, b.cfg.Bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true})
|
||||
for object := range objects {
|
||||
if object.Err != nil {
|
||||
return object.Err
|
||||
return fmt.Errorf("s3 list prefix %q in bucket %q failed: %w", prefix, b.cfg.Bucket, object.Err)
|
||||
}
|
||||
if err := b.Delete(ctx, object.Key); err != nil {
|
||||
return err
|
||||
@@ -75,7 +84,7 @@ func (b *s3StorageBackend) Usage(ctx context.Context) (int64, error) {
|
||||
var total int64
|
||||
for object := range b.client.ListObjects(ctx, b.cfg.Bucket, minio.ListObjectsOptions{Recursive: true}) {
|
||||
if object.Err != nil {
|
||||
return 0, object.Err
|
||||
return 0, fmt.Errorf("s3 usage list bucket %q failed: %w", b.cfg.Bucket, object.Err)
|
||||
}
|
||||
total += object.Size
|
||||
}
|
||||
@@ -85,7 +94,7 @@ func (b *s3StorageBackend) Usage(ctx context.Context) (int64, error) {
|
||||
func (b *s3StorageBackend) Test(ctx context.Context) error {
|
||||
exists, err := b.client.BucketExists(ctx, b.cfg.Bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("s3 bucket check for %q failed: %w", b.cfg.Bucket, err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("bucket %q does not exist", b.cfg.Bucket)
|
||||
|
||||
@@ -117,6 +117,8 @@ type Box struct {
|
||||
Obfuscate bool `json:"obfuscate"`
|
||||
CreatorIP string `json:"creatorIp,omitempty"`
|
||||
StorageBackendID string `json:"storageBackendId,omitempty"`
|
||||
Trouble bool `json:"trouble,omitempty"`
|
||||
TroubleReason string `json:"troubleReason,omitempty"`
|
||||
Files []File `json:"files"`
|
||||
}
|
||||
|
||||
@@ -139,6 +141,37 @@ type File struct {
|
||||
UploadedAt time.Time `json:"uploadedAt"`
|
||||
}
|
||||
|
||||
func BoxHasTrouble(box Box) bool {
|
||||
if box.Trouble || strings.TrimSpace(box.TroubleReason) != "" {
|
||||
return true
|
||||
}
|
||||
for _, file := range box.Files {
|
||||
if FileHasTrouble(file) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func BoxTroubleReason(box Box) string {
|
||||
if strings.TrimSpace(box.TroubleReason) != "" {
|
||||
return box.TroubleReason
|
||||
}
|
||||
for _, file := range box.Files {
|
||||
if strings.TrimSpace(file.ProcessingError) != "" {
|
||||
return file.ProcessingError
|
||||
}
|
||||
}
|
||||
if box.Trouble {
|
||||
return "box has failed processing"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func FileHasTrouble(file File) bool {
|
||||
return strings.TrimSpace(file.ProcessingError) != ""
|
||||
}
|
||||
|
||||
type UploadResult struct {
|
||||
BoxID string `json:"boxId"`
|
||||
BoxURL string `json:"boxUrl"`
|
||||
|
||||
@@ -230,6 +230,47 @@ func TestResumableCompleteRejectsMissingChunks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessingResumableFailureMarksBoxFailed(t *testing.T) {
|
||||
service := newTestUploadService(t)
|
||||
session, err := service.CreateResumableSession([]ResumableFileInput{{
|
||||
Name: "note.txt",
|
||||
Size: 4,
|
||||
ContentType: "text/plain",
|
||||
}}, UploadOptions{MaxDays: 1, StorageBackendID: "missing"}, 4, time.Hour, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateResumableSession returned error: %v", err)
|
||||
}
|
||||
if _, err := service.PutResumableChunk(testContext(), session.ID, session.Files[0].ID, 0, strings.NewReader("note")); err != nil {
|
||||
t.Fatalf("PutResumableChunk returned error: %v", err)
|
||||
}
|
||||
result, processing, err := service.CreateProcessingBoxFromResumable(session.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateProcessingBoxFromResumable returned error: %v", err)
|
||||
}
|
||||
if processing.Status != ResumableStatusProcessing {
|
||||
t.Fatalf("session status = %q, want processing", processing.Status)
|
||||
}
|
||||
if _, err := service.FinalizeProcessingResumableSession(testContext(), session.ID); err == nil {
|
||||
t.Fatalf("FinalizeProcessingResumableSession accepted missing backend")
|
||||
}
|
||||
box := getTestBox(t, service, result.BoxID)
|
||||
if len(box.Files) != 1 {
|
||||
t.Fatalf("box files = %+v", box.Files)
|
||||
}
|
||||
if box.Files[0].Processing {
|
||||
t.Fatalf("failed file is still marked processing: %+v", box.Files[0])
|
||||
}
|
||||
if box.Files[0].ProcessingError == "" {
|
||||
t.Fatalf("failed file did not store processing error: %+v", box.Files[0])
|
||||
}
|
||||
if !box.Trouble {
|
||||
t.Fatalf("failed box was not marked as trouble: %+v", box)
|
||||
}
|
||||
if box.TroubleReason == "" {
|
||||
t.Fatalf("failed box did not store trouble reason: %+v", box)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResumablePartialCompleteKeepsOnlyFinishedFiles(t *testing.T) {
|
||||
service := newTestUploadService(t)
|
||||
session, err := service.CreateResumableSession([]ResumableFileInput{
|
||||
|
||||
Reference in New Issue
Block a user