(ctx context.Context, userID entities.UserID, messageID uuid.UUID, attachments []ServiceAttachment)
| 631 | } |
| 632 | |
| 633 | func (service *MessageService) uploadAttachments(ctx context.Context, userID entities.UserID, messageID uuid.UUID, attachments []ServiceAttachment) ([]string, error) { |
| 634 | if len(attachments) == 0 { |
| 635 | return []string{}, nil |
| 636 | } |
| 637 | |
| 638 | ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) |
| 639 | defer span.End() |
| 640 | |
| 641 | g, gCtx := errgroup.WithContext(ctx) |
| 642 | urls := make([]string, len(attachments)) |
| 643 | paths := make([]string, len(attachments)) |
| 644 | |
| 645 | for i, attachment := range attachments { |
| 646 | i, attachment := i, attachment |
| 647 | g.Go(func() error { |
| 648 | decoded, err := base64.StdEncoding.DecodeString(attachment.Content) |
| 649 | if err != nil { |
| 650 | return stacktrace.Propagate(err, fmt.Sprintf("cannot decode base64 content for attachment [%d]", i)) |
| 651 | } |
| 652 | |
| 653 | sanitizedName := repositories.SanitizeFilename(attachment.Name, i) |
| 654 | ext := repositories.ExtensionFromContentType(attachment.ContentType) |
| 655 | filename := sanitizedName + ext |
| 656 | |
| 657 | path := fmt.Sprintf("attachments/%s/%s/%d/%s", userID, messageID, i, filename) |
| 658 | paths[i] = path |
| 659 | |
| 660 | if err = service.attachmentRepository.Upload(gCtx, path, decoded, attachment.ContentType); err != nil { |
| 661 | return stacktrace.Propagate(err, fmt.Sprintf("cannot upload attachment [%d] to path [%s]", i, path)) |
| 662 | } |
| 663 | |
| 664 | urls[i] = fmt.Sprintf("%s/v1/attachments/%s/%s/%d/%s", service.apiBaseURL, userID, messageID, i, filename) |
| 665 | ctxLogger.Info(fmt.Sprintf("uploaded attachment [%d] to [%s]", i, path)) |
| 666 | return nil |
| 667 | }) |
| 668 | } |
| 669 | |
| 670 | if err := g.Wait(); err != nil { |
| 671 | for _, path := range paths { |
| 672 | if path != "" { |
| 673 | _ = service.attachmentRepository.Delete(ctx, path) |
| 674 | } |
| 675 | } |
| 676 | return nil, stacktrace.Propagate(err, "cannot upload attachments") |
| 677 | } |
| 678 | |
| 679 | return urls, nil |
| 680 | } |
| 681 | |
| 682 | // HandleMessageParams are parameters for handling a message event |
| 683 | type HandleMessageParams struct { |
no test coverage detected