|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go/aws" |
| 9 | + "github.com/aws/aws-sdk-go/aws/session" |
| 10 | + "github.com/aws/aws-sdk-go/service/s3" |
| 11 | + "github.com/google/uuid" |
| 12 | + "github.com/labstack/echo/v4" |
| 13 | + |
| 14 | + "github.com/USACE/cumulus-api/api/middleware" |
| 15 | + "github.com/USACE/cumulus-api/api/models" |
| 16 | + |
| 17 | + _ "github.com/jackc/pgx/v4" |
| 18 | + "github.com/jackc/pgx/v4/pgxpool" |
| 19 | +) |
| 20 | + |
| 21 | +// ListProductfilesCOG returns the productfiles for a product over a time range as |
| 22 | +// directly-readable COG proxy URLs (each backed by StreamProductfileCOG, which is |
| 23 | +// Range-capable so a GDAL /vsicurl/ client can read tiles). Same query params as |
| 24 | +// ListProductfiles: 'after' and 'before' (RFC3339). |
| 25 | +func ListProductfilesCOG(db *pgxpool.Pool) echo.HandlerFunc { |
| 26 | + return func(c echo.Context) error { |
| 27 | + id, err := uuid.Parse(c.Param("product_id")) |
| 28 | + if err != nil { |
| 29 | + return c.String(http.StatusBadRequest, "Malformed ID") |
| 30 | + } |
| 31 | + after := c.QueryParam("after") |
| 32 | + before := c.QueryParam("before") |
| 33 | + if after == "" || before == "" { |
| 34 | + return c.String( |
| 35 | + http.StatusBadRequest, |
| 36 | + "Missing query parameter 'after' or 'before'", |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + ff, err := models.ListProductfiles(db, id, after, before) |
| 41 | + if err != nil { |
| 42 | + return c.String(http.StatusInternalServerError, err.Error()) |
| 43 | + } |
| 44 | + |
| 45 | + cogs := make([]models.ProductfileCOG, 0, len(ff)) |
| 46 | + for _, f := range ff { |
| 47 | + cogs = append(cogs, models.ProductfileCOG{ |
| 48 | + ID: f.ID, |
| 49 | + Datetime: f.Datetime, |
| 50 | + Version: f.Version, |
| 51 | + CogURL: fmt.Sprintf("/api/products/%s/cog/%s", id.String(), f.ID.String()), |
| 52 | + }) |
| 53 | + } |
| 54 | + return c.JSON(http.StatusOK, cogs) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// StreamProductfileCOG streams a productfile's COG object from S3, passing the |
| 59 | +// inbound HTTP Range header through to S3 and returning 206 Partial Content with |
| 60 | +// Content-Range/Accept-Ranges so a GDAL /vsicurl/ client can read tiles directly |
| 61 | +// (no full download). HEAD returns size + range support for /vsicurl/ probing. |
| 62 | +// Every request is authenticated (private route) and logged for metering. |
| 63 | +func StreamProductfileCOG(db *pgxpool.Pool, awsCfg *aws.Config) echo.HandlerFunc { |
| 64 | + return func(c echo.Context) error { |
| 65 | + pfID, err := uuid.Parse(c.Param("productfile_id")) |
| 66 | + if err != nil { |
| 67 | + return c.String(http.StatusBadRequest, "Malformed productfile ID") |
| 68 | + } |
| 69 | + |
| 70 | + obj, err := models.GetProductfileObject(db, pfID) |
| 71 | + if err != nil { |
| 72 | + return c.String(http.StatusNotFound, "Productfile not found") |
| 73 | + } |
| 74 | + |
| 75 | + client := s3.New(session.New(awsCfg)) |
| 76 | + |
| 77 | + // HEAD: metadata only (size + range support) for /vsicurl/ probing. |
| 78 | + if c.Request().Method == http.MethodHead { |
| 79 | + head, err := client.HeadObject(&s3.HeadObjectInput{Bucket: &obj.Bucket, Key: &obj.Key}) |
| 80 | + if err != nil { |
| 81 | + return c.String(http.StatusInternalServerError, err.Error()) |
| 82 | + } |
| 83 | + h := c.Response().Header() |
| 84 | + h.Set("Accept-Ranges", "bytes") |
| 85 | + if head.ContentLength != nil { |
| 86 | + h.Set(echo.HeaderContentLength, fmt.Sprintf("%d", *head.ContentLength)) |
| 87 | + } |
| 88 | + c.Response().WriteHeader(http.StatusOK) |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + in := &s3.GetObjectInput{Bucket: &obj.Bucket, Key: &obj.Key} |
| 93 | + if rangeHeader := c.Request().Header.Get("Range"); rangeHeader != "" { |
| 94 | + in.Range = aws.String(rangeHeader) |
| 95 | + } |
| 96 | + |
| 97 | + out, err := client.GetObject(in) |
| 98 | + if err != nil { |
| 99 | + return c.String(http.StatusInternalServerError, err.Error()) |
| 100 | + } |
| 101 | + defer out.Body.Close() |
| 102 | + |
| 103 | + h := c.Response().Header() |
| 104 | + h.Set("Accept-Ranges", "bytes") |
| 105 | + status := http.StatusOK |
| 106 | + if out.ContentRange != nil { |
| 107 | + h.Set("Content-Range", *out.ContentRange) |
| 108 | + status = http.StatusPartialContent |
| 109 | + } |
| 110 | + if out.ContentLength != nil { |
| 111 | + h.Set(echo.HeaderContentLength, fmt.Sprintf("%d", *out.ContentLength)) |
| 112 | + } |
| 113 | + |
| 114 | + // Metering hook: who pulled which productfile and how many bytes. This is the |
| 115 | + // natural place to enforce a future per-user rate limit / quota. |
| 116 | + var bytesServed int64 |
| 117 | + if out.ContentLength != nil { |
| 118 | + bytesServed = *out.ContentLength |
| 119 | + } |
| 120 | + logCOGAccess(c, pfID, bytesServed) |
| 121 | + |
| 122 | + return c.Stream(status, "application/octet-stream", out.Body) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func logCOGAccess(c echo.Context, productfileID uuid.UUID, bytes int64) { |
| 127 | + sub := "key-auth" |
| 128 | + if ui, ok := c.Get("userInfo").(middleware.UserInfo); ok && ui.Sub != nil { |
| 129 | + sub = ui.Sub.String() |
| 130 | + } |
| 131 | + log.Printf("cog-access sub=%s productfile=%s bytes=%d", sub, productfileID.String(), bytes) |
| 132 | +} |
0 commit comments