MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / uploadAttachments

Method uploadAttachments

api/pkg/services/message_service.go:633–680  ·  view source on GitHub ↗
(ctx context.Context, userID entities.UserID, messageID uuid.UUID, attachments []ServiceAttachment)

Source from the content-addressed store, hash-verified

631}
632
633func (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
683type HandleMessageParams struct {

Callers 1

ReceiveMessageMethod · 0.95

Calls 4

StartWithLoggerMethod · 0.65
UploadMethod · 0.65
InfoMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected