| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo/blobs" |
| 11 | ) |
| 12 | |
| 13 | // The images' config (images spec decision 4, §3.4). CARLOS delivers a |
| 14 | // store as CARLOS_STORE_BUCKET and _REGION, with _ACCESS_KEY and |
| 15 | // _SECRET_KEY as secrets (and _ENDPOINT only where there is one). No |
| 16 | // store boots with uploads at 503; so does a partly delivered one, |
| 17 | // which LogImageSetup names loudly. An images fault never takes the |
| 18 | // knowledge base down. |
| 19 | |
| 20 | // storeVars are the variables a store needs, in the order named. |
| 21 | var storeVars = []string{"CARLOS_STORE_BUCKET", "CARLOS_STORE_REGION", |
| 22 | "CARLOS_STORE_ACCESS_KEY", "CARLOS_STORE_SECRET_KEY"} |
| 23 | |
| 24 | func loadImages(getenv func(string) string) (ImageSetup, error) { |
| 25 | var setup ImageSetup |
| 26 | var err error |
| 27 | setup.QuotaPerson, err = quota(getenv, "MAPA_IMAGE_QUOTA_PERSON", |
| 28 | DefaultQuotaPerson) |
| 29 | if err != nil { |
| 30 | return setup, err |
| 31 | } |
| 32 | setup.QuotaTeam, err = quota(getenv, "MAPA_IMAGE_QUOTA_TEAM", |
| 33 | DefaultQuotaTeam) |
| 34 | if err != nil { |
| 35 | return setup, err |
| 36 | } |
| 37 | present := 0 |
| 38 | for _, name := range storeVars { |
| 39 | if getenv(name) == "" { |
| 40 | setup.Missing = append(setup.Missing, name) |
| 41 | } else { |
| 42 | present++ |
| 43 | } |
| 44 | } |
| 45 | switch { |
| 46 | case present == 0: |
| 47 | setup.Missing = nil |
| 48 | case len(setup.Missing) == 0: |
| 49 | setup.Objects, err = bucketFrom(getenv) |
| 50 | } |
| 51 | return setup, err |
| 52 | } |
| 53 | |
| 54 | // quota reads an allowance in bytes, or with a KiB, MiB or GiB suffix. |
| 55 | func quota(getenv func(string) string, name string, |
| 56 | fallback int64) (int64, error) { |
| 57 | raw := strings.TrimSpace(getenv(name)) |
| 58 | if raw == "" { |
| 59 | return fallback, nil |
| 60 | } |
| 61 | scale := int64(1) |
| 62 | for suffix, size := range map[string]int64{"KiB": 1 << 10, |
| 63 | "MiB": 1 << 20, "GiB": 1 << 30} { |
| 64 | if trimmed, ok := strings.CutSuffix(raw, suffix); ok { |
| 65 | raw, scale = strings.TrimSpace(trimmed), size |
| 66 | } |
| 67 | } |
| 68 | value, err := strconv.ParseInt(raw, 10, 64) |
| 69 | if err != nil || value <= 0 || value > (1<<62)/scale { |
| 70 | return 0, fmt.Errorf("mapa: %s is %q; it is a size in bytes, "+ |
| 71 | "or a number of KiB, MiB or GiB", name, getenv(name)) |
| 72 | } |
| 73 | return value * scale, nil |
| 74 | } |
| 75 | |
| 76 | // LocalImages is the development store, blobs.Dir beside the database |
| 77 | // (decision 4), for a loopback origin with no store bound; nil in |
| 78 | // production, where uploads then answer 503. |
| 79 | func (config Config) LocalImages(dir string) (blobs.Store, error) { |
| 80 | if config.Images.Objects != nil || len(config.Images.Missing) > 0 || |
| 81 | !isLocal(config.Origin) { |
| 82 | return nil, nil |
| 83 | } |
| 84 | return blobs.Dir(dir) |
| 85 | } |
| 86 | |
| 87 | // bucketFrom is the delivered bucket, its Puts Mapa's own (bucket). |
| 88 | func bucketFrom(getenv func(string) string) (blobs.Store, error) { |
| 89 | s3, err := blobs.NewS3(blobs.S3Config{ |
| 90 | Bucket: getenv("CARLOS_STORE_BUCKET"), |
| 91 | Region: getenv("CARLOS_STORE_REGION"), |
| 92 | Endpoint: getenv("CARLOS_STORE_ENDPOINT"), |
| 93 | AccessKey: getenv("CARLOS_STORE_ACCESS_KEY"), |
| 94 | SecretKey: getenv("CARLOS_STORE_SECRET_KEY")}) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | return bucket{S3: s3, client: &http.Client{Timeout: 60 * time.Second}}, |
| 99 | nil |
| 100 | } |
| 101 | |