package cabana import ( "context" "errors" "fmt" "io" "net/http" "strconv" "strings" "sync" "time" "git.golem15.com/golem15/summercms/modules/bouncer" "git.golem15.com/golem15/summercms/modules/cooler" "git.golem15.com/golem15/summercms/modules/lagoon" "git.golem15.com/golem15/summercms/modules/lagoon/attach" "git.golem15.com/golem15/summercms/modules/postcard" "gorm.io/gorm" ) // AdminMailImage is one library image with absolute public URLs. type AdminMailImage struct { ID uint `json:"id"` FileName string `json:"fileName"` Title string `json:"title"` URL string `json:"url"` ThumbURL string `json:"thumbUrl,omitempty"` } // AdminMailImagesMeta is the meta of GET /mail/images. type AdminMailImagesMeta struct { CanUpload bool `json:"canUpload"` Total int64 `json:"total"` Page int `json:"page"` PerPage int `json:"perPage"` } // AdminMailImagesResponse is the GET /mail/images answer (custom meta). type AdminMailImagesResponse struct { Data []AdminMailImage `json:"data"` Meta AdminMailImagesMeta `json:"meta"` } const ( mailImagesPerPage = 40 mailImageMaxBytes = 5 << 20 ) var mailImageLimits = attach.Limits{ MaxBytes: mailImageMaxBytes, Extensions: attach.DefaultImageExtensions, Image: true, } // mailImagesList serves GET /mail/images: library images with absolute URLs, // for holders of either mail permission. canUpload is true only for layouts. func (s *service) mailImagesList(w http.ResponseWriter, r *http.Request) { principal, ok := bouncer.User(r.Context()) if !ok || principal == nil || !principal.Backend { WriteError(w, http.StatusUnauthorized, "unauthenticated", msgUnauthenticated) return } if !tokenRouteGate(principal, w) { return } if !Allows(principal, []string{PermissionManageMailTemplates, PermissionManageMailLayouts}) { WriteError(w, http.StatusForbidden, "forbidden", msgForbidden) return } cfg := s.config() if _, err := attach.AbsolutePublicURL(cfg, "probe"); errors.Is(err, attach.ErrNoAbsoluteURL) { WriteError(w, http.StatusConflict, "conflict", s.mailMessage(r.Context(), mailLang+"images.no_public_url")) return } db, err := s.db() if err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } q := strings.TrimSpace(r.URL.Query().Get("q")) page := 1 if raw := r.URL.Query().Get("page"); raw != "" { if n, err := strconv.Atoi(raw); err == nil && n > 0 { page = n } } query := db.WithContext(r.Context()).Model(&attach.File{}). Where("attachment_type = ? AND field = ?", mailImageLibraryMorph, "images") if q != "" { like := "%" + q + "%" query = query.Where("file_name ILIKE ? OR title ILIKE ?", like, like) } var total int64 if err := query.Count(&total).Error; err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } var files []attach.File if err := query.Order("id DESC").Offset((page - 1) * mailImagesPerPage).Limit(mailImagesPerPage).Find(&files).Error; err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } items := make([]AdminMailImage, 0, len(files)) for i := range files { item, err := s.mailImageItem(r.Context(), &files[i]) if err != nil { if errors.Is(err, attach.ErrNoAbsoluteURL) { WriteError(w, http.StatusConflict, "conflict", s.mailMessage(r.Context(), mailLang+"images.no_public_url")) return } WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } items = append(items, item) } WriteData(w, http.StatusOK, items, AdminMailImagesMeta{ CanUpload: Allows(principal, []string{PermissionManageMailLayouts}), Total: total, Page: page, PerPage: mailImagesPerPage, }) } // mailImagesShow serves GET /mail/images/{id}: one library image, or 404. func (s *service) mailImagesShow(w http.ResponseWriter, r *http.Request) { principal, ok := bouncer.User(r.Context()) if !ok || principal == nil || !principal.Backend { WriteError(w, http.StatusUnauthorized, "unauthenticated", msgUnauthenticated) return } if !tokenRouteGate(principal, w) { return } if !Allows(principal, []string{PermissionManageMailTemplates, PermissionManageMailLayouts}) { WriteError(w, http.StatusForbidden, "forbidden", msgForbidden) return } id, err := strconv.ParseUint(r.PathValue("id"), 10, 64) if err != nil || id == 0 { writeNotFound(w, r) return } db, err := s.db() if err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } var f attach.File err = db.WithContext(r.Context()). Where("id = ? AND attachment_type = ? AND field = ?", id, mailImageLibraryMorph, "images"). Take(&f).Error if errors.Is(err, gorm.ErrRecordNotFound) { writeNotFound(w, r) return } if err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } item, err := s.mailImageItem(r.Context(), &f) if err != nil { if errors.Is(err, attach.ErrNoAbsoluteURL) { WriteError(w, http.StatusConflict, "conflict", s.mailMessage(r.Context(), mailLang+"images.no_public_url")) return } WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } WriteData(w, http.StatusOK, item, nil) } // mailImagesUpload serves POST /mail/images: layouts holders only, multipart // "file", attached to the library host with an absolute public URL. func (s *service) mailImagesUpload(w http.ResponseWriter, r *http.Request) { principal, ok := bouncer.User(r.Context()) if !ok || principal == nil || !principal.Backend { WriteError(w, http.StatusUnauthorized, "unauthenticated", msgUnauthenticated) return } if !tokenRouteGate(principal, w) { return } if !Allows(principal, []string{PermissionManageMailLayouts}) { WriteError(w, http.StatusForbidden, "forbidden", msgForbidden) return } if _, err := attach.AbsolutePublicURL(s.config(), "probe"); errors.Is(err, attach.ErrNoAbsoluteURL) { WriteError(w, http.StatusConflict, "conflict", s.mailMessage(r.Context(), mailLang+"images.no_public_url")) return } db, err := s.db() if err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } bucket := s.bucket() if bucket == nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } body := &bodyReader{r: http.MaxBytesReader(w, r.Body, s.uploadCap(nil))} r.Body = io.NopCloser(body) mr, err := r.MultipartReader() if err != nil { writeCRUDError(w, invalidBody()) return } part, err := mr.NextPart() if err != nil { writeCRUDError(w, invalidBody()) return } if part.FormName() != "file" || strings.TrimSpace(part.FileName()) == "" { writeCRUDError(w, invalidBody()) return } var stored *attach.File var item AdminMailImage err = lagoon.Transaction(r.Context(), db, func(ctx context.Context, tx *gorm.DB) error { host, err := mailImagesAdmin{}.SingletonRecord(ctx, tx) if err != nil { return err } library, ok := host.(*MailImageLibrary) if !ok || library == nil || library.ID == 0 { return errors.New("cabana: mail image library host missing") } f, err := attach.Store(ctx, tx, bucket, attach.Upload{FileName: part.FileName(), Body: part, Public: true}, mailImageLimits) if err != nil { return err } stored = f if _, err := mr.NextPart(); !errors.Is(err, io.EOF) { if err == nil { return invalidBody() } return err } if err := tx.Session(&gorm.Session{NewDB: true, Context: ctx}).Model(&attach.File{}).Where("id = ?", f.ID).Updates(map[string]any{ "attachment_id": uitoa(library.ID), "attachment_type": mailImageLibraryMorph, "field": "images", }).Error; err != nil { return err } f.AttachmentID = uitoa(library.ID) f.AttachmentType = mailImageLibraryMorph f.Field = "images" item, err = s.mailImageItem(ctx, f) return err }) if err != nil { if stored != nil { _ = attach.DeleteKeys(context.WithoutCancel(r.Context()), bucket, attach.BlobKeys(*stored)) } if errors.Is(err, attach.ErrNoAbsoluteURL) { WriteError(w, http.StatusConflict, "conflict", s.mailMessage(r.Context(), mailLang+"images.no_public_url")) return } if errors.Is(err, attach.ErrNotImage) || errors.Is(err, attach.ErrFileType) || errors.Is(err, attach.ErrMIMEType) || errors.Is(err, attach.ErrTooLarge) { writeCRUDError(w, &ValidationError{Details: map[string]any{"file": []string{err.Error()}}}) return } writeCRUDError(w, err) return } WriteData(w, http.StatusCreated, item, nil) } func (s *service) mailImageItem(ctx context.Context, f *attach.File) (AdminMailImage, error) { url, err := attach.AbsolutePublicURL(s.config(), attach.BlobKey(f.DiskName)) if err != nil { return AdminMailImage{}, err } title := f.FileName if f.Title != nil && *f.Title != "" { title = *f.Title } item := AdminMailImage{ID: f.ID, FileName: f.FileName, Title: title, URL: url} if bucket := s.bucket(); bucket != nil { if key, err := f.ThumbKey(ctx, bucket, defaultThumbEdge, defaultThumbEdge, "auto"); err == nil && key != "" { if abs, err := attach.AbsolutePublicURL(s.config(), key); err == nil { item.ThumbURL = abs } } } return item, nil } // mailMessage translates a mail phrase key in the request locale. func (s *service) mailMessage(ctx context.Context, key string) string { tr := s.translator() if tr == nil { return key } return translateKey(ctx, tr, key) } // AdminMailPreviewRequest is the body of POST /mail/preview and // POST /mail/test-send: the draft mode, optional record id and the form // values. Recipient fields are refused (decodeStrictBody); test-send always // uses the signed-in administrator's own email. type AdminMailPreviewRequest struct { Mode string `json:"mode"` ID uint64 `json:"id"` Values map[string]any `json:"values"` } // AdminMailPreviewResult is the data of a successful preview render. type AdminMailPreviewResult struct { Subject string `json:"subject"` HTML string `json:"html"` Text string `json:"text"` } // AdminMailTestSendResult is the data of a successful test send: the address // the draft was delivered to (the administrator's own). type AdminMailTestSendResult struct { Email string `json:"email"` } const ( mailPreviewModeTemplate = "template" mailPreviewModeLayout = "layout" mailPreviewModePartial = "partial" mailPreviewModeBranding = "branding" mailTestMax = 5 mailTestDecay = 10 * time.Minute mailTestKeyFmt = "mail_test:%d" msgMailPreviewFailed = "The draft could not be previewed." msgMailServerError = "An unexpected error occurred." ) // mailPreview serves POST /mail/preview: renders an unsaved draft through // postcard.Editor.Preview with sample values. func (s *service) mailPreview(w http.ResponseWriter, r *http.Request) { principal, ok := bouncer.User(r.Context()) if !ok || principal == nil || !principal.Backend { WriteError(w, http.StatusUnauthorized, "unauthenticated", msgUnauthenticated) return } if !tokenRouteGate(principal, w) { return } var body AdminMailPreviewRequest if err := s.decodeStrictBody(w, r, &body); err != nil { writeCRUDError(w, err) return } if !s.mailModeAllowed(principal, body.Mode) { WriteError(w, http.StatusForbidden, "forbidden", msgForbidden) return } editor, ok := postcard.EditorFrom(s.app) if !ok || editor == nil { WriteError(w, http.StatusServiceUnavailable, "error", msgMailServerError) return } draft, err := s.mailDraftFromRequest(r.Context(), body) if err != nil { writeCRUDError(w, err) return } preview, err := editor.Preview(r.Context(), draft) if err != nil { s.writeMailDraftError(w, r, err) return } WriteData(w, http.StatusOK, AdminMailPreviewResult{ Subject: preview.Subject, HTML: preview.HTML, Text: preview.Text, }, nil) } // mailTestSend serves POST /mail/test-send: interactive sessions only, sends // the draft to the signed-in administrator's own email, limited to // mailTestMax per mailTestDecay per administrator. func (s *service) mailTestSend(w http.ResponseWriter, r *http.Request) { principal, ok := bouncer.User(r.Context()) if !ok || principal == nil || !principal.Backend { WriteError(w, http.StatusUnauthorized, "unauthenticated", msgUnauthenticated) return } if !tokenRouteGate(principal, w) { return } var body AdminMailPreviewRequest if err := s.decodeStrictBody(w, r, &body); err != nil { writeCRUDError(w, err) return } switch body.Mode { case mailPreviewModeTemplate, mailPreviewModeLayout, mailPreviewModePartial: default: WriteError(w, http.StatusForbidden, "forbidden", msgForbidden) return } if !s.mailModeAllowed(principal, body.Mode) { WriteError(w, http.StatusForbidden, "forbidden", msgForbidden) return } db, err := s.db() if err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } var user BackendUser if err := db.WithContext(r.Context()).Select("id", "email").Where("id = ?", principal.ID).Take(&user).Error; err != nil { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } email := strings.TrimSpace(user.Email) if email == "" { WriteError(w, http.StatusInternalServerError, "error", msgServerError) return } allowed, retryAfter := s.mailTestAttempt(principal.ID) if !allowed { secs := int(retryAfter.Seconds()) if secs < 1 { secs = 1 } w.Header().Set("Retry-After", strconv.Itoa(secs)) WriteError(w, http.StatusTooManyRequests, "rate_limited", s.mailMessage(r.Context(), mailLang+"preview.rate_limited")) return } editor, ok := postcard.EditorFrom(s.app) if !ok || editor == nil { WriteError(w, http.StatusServiceUnavailable, "error", msgMailServerError) return } draft, err := s.mailDraftFromRequest(r.Context(), body) if err != nil { writeCRUDError(w, err) return } if err := editor.SendDraft(r.Context(), draft, email); err != nil { var de *postcard.DraftError if errors.As(err, &de) { s.writeMailDraftError(w, r, err) return } WriteError(w, http.StatusBadGateway, "error", s.mailMessage(r.Context(), mailLang+"preview.test_failed")) return } WriteData(w, http.StatusOK, AdminMailTestSendResult{Email: email}, nil) } func (s *service) mailModeAllowed(principal *bouncer.Principal, mode string) bool { switch mode { case mailPreviewModeTemplate: return Allows(principal, []string{PermissionManageMailTemplates}) case mailPreviewModeLayout, mailPreviewModePartial, mailPreviewModeBranding: return Allows(principal, []string{PermissionManageMailLayouts}) default: return false } } func (s *service) writeMailDraftError(w http.ResponseWriter, r *http.Request, err error) { var de *postcard.DraftError if !errors.As(err, &de) || de == nil { WriteError(w, http.StatusInternalServerError, "error", msgMailPreviewFailed) return } msg := s.mailDraftMessage(r.Context(), de) WriteErrorDetails(w, http.StatusUnprocessableEntity, "validation_failed", msg, map[string]any{ "field": de.Field, "rule": de.Rule, "message": de.Message, }) } func (s *service) mailDraftMessage(ctx context.Context, de *postcard.DraftError) string { tr := s.translator() key := mailLang + "invalid_" + de.Rule if tr == nil || !tr.Has(key) { if de.Message != "" { return de.Message } return key } return tr.Get(ctx, key, map[string]string{"message": de.Message, "name": de.Name}) } func (s *service) mailDraftFromRequest(ctx context.Context, body AdminMailPreviewRequest) (postcard.Draft, error) { values := body.Values if values == nil { values = map[string]any{} } switch body.Mode { case mailPreviewModeTemplate: return s.mailTemplateDraft(ctx, body.ID, values) case mailPreviewModeLayout: return s.mailLayoutDraft(ctx, body.ID, values) case mailPreviewModePartial: return s.mailPartialDraft(ctx, body.ID, values) case mailPreviewModeBranding: return s.mailBrandDraft(ctx, values) default: return postcard.Draft{}, &ForbiddenError{Message: msgForbidden} } } func (s *service) mailTemplateDraft(ctx context.Context, id uint64, values map[string]any) (postcard.Draft, error) { name, err := s.mailRecordCode(ctx, postcard.TemplatesTable, id) if err != nil { return postcard.Draft{}, err } if editor, ok := postcard.EditorFrom(s.app); ok && editor != nil { if _, ok := editor.Catalog().Template(name); !ok { return postcard.Draft{}, recordNotFound{} } } layoutID, _ := asUint(values["layout"]) layout := "" if layoutID > 0 { code, err := s.mailLayoutCodeByID(ctx, layoutID) if err != nil { return postcard.Draft{}, err } layout = code } return postcard.Draft{ Kind: postcard.KindTemplate, Name: name, Subject: stringValue(values["subject"]), Body: stringValue(values["body"]), Layout: layout, }, nil } func (s *service) mailLayoutDraft(ctx context.Context, id uint64, values map[string]any) (postcard.Draft, error) { name := stringValue(values["code"]) if name == "" { name = stringValue(values["name"]) } if id > 0 { code, err := s.mailRecordCode(ctx, postcard.LayoutsTable, id) if err != nil { return postcard.Draft{}, err } name = code } return postcard.Draft{ Kind: postcard.KindLayout, Name: name, HTML: stringValue(values["content_html"]), Text: stringValue(values["content_text"]), CSS: stringValue(values["content_css"]), }, nil } func (s *service) mailPartialDraft(ctx context.Context, id uint64, values map[string]any) (postcard.Draft, error) { name, err := s.mailRecordCode(ctx, postcard.PartialsTable, id) if err != nil { return postcard.Draft{}, err } if editor, ok := postcard.EditorFrom(s.app); ok && editor != nil { if _, ok := editor.Catalog().Partial(name); !ok { return postcard.Draft{}, recordNotFound{} } } return postcard.Draft{ Kind: postcard.KindPartial, Name: name, HTML: stringValue(values["content_html"]), Text: stringValue(values["content_text"]), }, nil } func (s *service) mailBrandDraft(ctx context.Context, values map[string]any) (postcard.Draft, error) { brand := postcard.Brand{ BodyBg: stringValue(values["body_bg"]), ContentBg: stringValue(values["content_bg"]), HeaderColor: stringValue(values["header_color"]), FooterColor: stringValue(values["footer_color"]), HeadingColor: stringValue(values["heading_color"]), TextColor: stringValue(values["text_color"]), LinkColor: stringValue(values["link_color"]), ButtonTextColor: stringValue(values["button_text_color"]), ButtonPrimaryBg: stringValue(values["button_primary_bg"]), ButtonPositiveBg: stringValue(values["button_positive_bg"]), ButtonNegativeBg: stringValue(values["button_negative_bg"]), PanelBg: stringValue(values["panel_bg"]), TableBorderColor: stringValue(values["table_border_color"]), } if raw, ok := values["logo_file_id"]; ok && raw != nil { id, err := asUint(raw) if err == nil && id > 0 { url, err := s.mailLogoAbsoluteURL(ctx, id) if err != nil { return postcard.Draft{}, err } brand.LogoURL = url } } return postcard.Draft{Kind: postcard.KindBrand, Brand: &brand}, nil } func (s *service) mailLogoAbsoluteURL(ctx context.Context, id uint) (string, error) { db, err := s.db() if err != nil { return "", err } var f attach.File err = db.WithContext(ctx). Where("id = ? AND attachment_type = ? AND field = ?", id, mailImageLibraryMorph, "images"). Take(&f).Error if errors.Is(err, gorm.ErrRecordNotFound) { return "", &ValidationError{Details: map[string]any{"logo_file_id": []string{"backend::lang.mailimage.missing"}}} } if err != nil { return "", err } return attach.AbsolutePublicURL(s.config(), attach.BlobKey(f.DiskName)) } func (s *service) mailRecordCode(ctx context.Context, table string, id uint64) (string, error) { if id == 0 { return "", recordNotFound{} } db, err := s.db() if err != nil { return "", err } var codes []string if err := db.WithContext(ctx).Table(table).Where("id = ?", id).Pluck("code", &codes).Error; err != nil { return "", err } if len(codes) == 0 { return "", recordNotFound{} } return codes[0], nil } func (s *service) mailLayoutCodeByID(ctx context.Context, id uint) (string, error) { db, err := s.db() if err != nil { return "", err } var codes []string if err := db.WithContext(ctx).Table(postcard.LayoutsTable).Where("id = ?", id).Pluck("code", &codes).Error; err != nil { return "", err } if len(codes) == 0 { return "", &ValidationError{Details: map[string]any{mailLayoutField: []string{s.mailMessage(ctx, mailLang+"invalid_layout")}}} } return codes[0], nil } func stringValue(v any) string { if v == nil { return "" } switch s := v.(type) { case string: return s case fmt.Stringer: return s.String() default: return fmt.Sprint(s) } } // mailTestAttempt admits one test-send for adminID through a published // cooler.Attempter when present, otherwise the process-local fixed window. func (s *service) mailTestAttempt(adminID uint) (allowed bool, retryAfter time.Duration) { key := fmt.Sprintf(mailTestKeyFmt, adminID) if s != nil && s.app != nil { if attempter, ok := s.app.Lookup[cooler.Attempter](); ok && attempter != nil { ok, _, retry := attempter.Attempt(key, mailTestMax, mailTestDecay) return ok, retry } } ok, _, retry := mailTestLocal.Attempt(key, mailTestMax, mailTestDecay) return ok, retry } // mailTestLocal is the in-process fixed-window fallback for test-send when // surf has not published a cooler.Attempter (tests, memory store). var mailTestLocal = newMailTestWindow() type mailTestWindow struct { mu sync.Mutex by map[string]mailTestBucket } type mailTestBucket struct { count int start time.Time } func newMailTestWindow() *mailTestWindow { return &mailTestWindow{by: map[string]mailTestBucket{}} } func (w *mailTestWindow) Attempt(key string, max int, decay time.Duration) (bool, int, time.Duration) { w.mu.Lock() defer w.mu.Unlock() now := time.Now() b, ok := w.by[key] if !ok || now.Sub(b.start) >= decay { w.by[key] = mailTestBucket{count: 1, start: now} return true, 1, decay } if b.count >= max { retry := decay - now.Sub(b.start) if retry < 0 { retry = 0 } return false, b.count, retry } b.count++ w.by[key] = b return true, b.count, decay - now.Sub(b.start) }