| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "reasonix/desktop/internal/draftstate" |
| 11 | "reasonix/internal/control" |
| 12 | ) |
| 13 | |
| 14 | func draftAdmissionError(err error) error { |
| 15 | stable := inboxBridgeError(err) |
| 16 | var coded *inboxCodedError |
| 17 | if errors.As(stable, &coded) { |
| 18 | return stable |
| 19 | } |
| 20 | return fmt.Errorf("draft submission not admitted: %w", err) |
| 21 | } |
| 22 | |
| 23 | func validateDraftAttachments(record draftstate.Draft) error { |
| 24 | var content struct { |
| 25 | Attachments []struct { |
| 26 | Path string `json:"path"` |
| 27 | } `json:"attachments"` |
| 28 | } |
| 29 | if err := json.Unmarshal([]byte(record.ContentJSON), &content); err != nil { |
| 30 | return fmt.Errorf("decode session draft attachments: %w", err) |
| 31 | } |
| 32 | root := record.WorkspaceRoot |
| 33 | if record.Scope != "project" { |
| 34 | root = globalWorkspaceRoot() |
| 35 | } |
| 36 | base, err := workspaceBaseFromRoot(root) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | for _, item := range content.Attachments { |
| 41 | if err := control.ValidateAttachmentInRoot(base, item.Path); err != nil { |
| 42 | if draftAttachmentLooksLikeImage(item.Path) { |
| 43 | return control.ImageReferenceFailures{control.ImageReferenceFailureForPath(item.Path, err)} |
| 44 | } |
| 45 | return fmt.Errorf("attachment %q is unavailable: %w", filepath.Base(item.Path), err) |
| 46 | } |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | func draftAttachmentLooksLikeImage(path string) bool { |
| 52 | switch strings.ToLower(filepath.Ext(path)) { |
| 53 | case ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg", ".tif", ".tiff": |
| 54 | return true |
| 55 | default: |
| 56 | return false |
| 57 | } |
| 58 | } |
| 59 |