| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | ) |
| 12 | |
| 13 | // AttachmentStreamLimitError reports the effective disk safety ceiling. |
| 14 | type AttachmentStreamLimitError struct { |
| 15 | LimitBytes int64 |
| 16 | LimitSource string |
| 17 | } |
| 18 | |
| 19 | func (e *AttachmentStreamLimitError) Error() string { |
| 20 | return fmt.Sprintf("attachment exceeds %d byte stream limit (%s)", e.LimitBytes, e.LimitSource) |
| 21 | } |
| 22 | |
| 23 | // SaveAttachmentStreamInRoot streams bytes with bounded memory and a byte-counted |
| 24 | // disk ceiling. Omit the policy for 1 GiB; an explicit zero permits unlimited storage. |
| 25 | func SaveAttachmentStreamInRoot(ctx context.Context, root, name string, source io.Reader, limits ...config.AttachmentLimit) (string, error) { |
| 26 | if len(limits) > 1 { |
| 27 | return "", fmt.Errorf("only one attachment stream limit may be supplied") |
| 28 | } |
| 29 | limit := (*config.Config)(nil).AttachmentStreamLimit() |
| 30 | if len(limits) == 1 { |
| 31 | limit = limits[0] |
| 32 | } |
| 33 | if limit.Bytes < 0 { |
| 34 | limit = (*config.Config)(nil).AttachmentStreamLimit() |
| 35 | } |
| 36 | if err := ctx.Err(); err != nil { |
| 37 | return "", err |
| 38 | } |
| 39 | ext := strings.ToLower(filepath.Ext(name)) |
| 40 | if !safeAttachmentExt.MatchString(ext) { |
| 41 | ext = ".bin" |
| 42 | } |
| 43 | owned, err := createOwnedAttachmentFile(root, ext) |
| 44 | if err != nil { |
| 45 | return "", err |
| 46 | } |
| 47 | defer owned.cleanup() |
| 48 | |
| 49 | buf := make([]byte, 64<<10) |
| 50 | var total int64 |
| 51 | wrote := false |
| 52 | for { |
| 53 | if err := ctx.Err(); err != nil { |
| 54 | return "", err |
| 55 | } |
| 56 | readBuf := buf |
| 57 | if limit.Bytes > 0 && limit.Bytes-total < int64(len(buf)) { |
| 58 | // Add the probe only after bounding remaining below len(buf), avoiding |
| 59 | // limit+1 overflow even when the configured limit is MaxInt64. |
| 60 | readBuf = buf[:int(limit.Bytes-total)+1] |
| 61 | } |
| 62 | n, readErr := source.Read(readBuf) |
| 63 | if n > 0 { |
| 64 | if err := ctx.Err(); err != nil { |
| 65 | return "", err |
| 66 | } |
| 67 | if limit.Bytes > 0 && int64(n) > limit.Bytes-total { |
| 68 | return "", &AttachmentStreamLimitError{LimitBytes: limit.Bytes, LimitSource: limit.Source} |
| 69 | } |
| 70 | if written, err := owned.Write(buf[:n]); err != nil { |
| 71 | return "", err |
| 72 | } else if written != n { |
| 73 | return "", io.ErrShortWrite |
| 74 | } |
| 75 | wrote = true |
| 76 | if limit.Bytes > 0 { |
| 77 | total += int64(n) |
| 78 | } |
| 79 | } |
| 80 | if readErr == io.EOF { |
| 81 | break |
| 82 | } |
| 83 | if readErr != nil { |
| 84 | return "", readErr |
| 85 | } |
| 86 | } |
| 87 | if err := ctx.Err(); err != nil { |
| 88 | return "", err |
| 89 | } |
| 90 | if !wrote { |
| 91 | return "", fmt.Errorf("attachment is empty") |
| 92 | } |
| 93 | if err := owned.finish(); err != nil { |
| 94 | return "", err |
| 95 | } |
| 96 | return filepath.ToSlash(owned.rel), nil |
| 97 | } |
| 98 |