Skip to content

Commit

Permalink
feat: pass reader around and store images png files
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jul 20, 2024
1 parent 88c5902 commit 80c0c8c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
29 changes: 12 additions & 17 deletions pkg/apis/strava/map.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package strava

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"

Expand All @@ -14,7 +14,7 @@ import (

const bucketName = "mapbox-maps"

func FetchMap(polyline string) []byte {
func FetchMap(polyline string) io.Reader {
var (
lineWidth = 2.0
lineColor = "000"
Expand All @@ -32,26 +32,21 @@ func FetchMap(polyline string) []byte {
return nil
}

var b bytes.Buffer
_, err = b.ReadFrom(resp.Body)
if err != nil {
lumber.Error(err, "failed to read data from request")
return nil
}

return b.Bytes()
return resp.Body
}

func UploadMap(minioClient minio.Client, id uint64, data []byte) {
reader := bytes.NewReader(data)
size := int64(len(data))
func UploadMap(minioClient minio.Client, id uint64, reader io.Reader) {
data, err := io.ReadAll(reader)
if err != nil {
lumber.Error(err, "reading from mapbox image failed")
}

_, err := minioClient.PutObject(
_, err = minioClient.PutObject(
context.Background(),
bucketName,
fmt.Sprintf("%d.jpg", id),
fmt.Sprintf("%d.png", id),
reader,
size,
int64(len(data)),
minio.PutObjectOptions{ContentType: "image/png"},
)
if err != nil {
Expand All @@ -62,7 +57,7 @@ func UploadMap(minioClient minio.Client, id uint64, data []byte) {
func RemoveOldMaps(minioClient minio.Client, activities []Activity) {
var validKeys []string
for _, activity := range activities {
validKeys = append(validKeys, fmt.Sprintf("%d.jpg", activity.ID))
validKeys = append(validKeys, fmt.Sprintf("%d.png", activity.ID))
}

objects := minioClient.ListObjects(context.Background(), bucketName, minio.ListObjectsOptions{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (c *Cache[T]) Route() http.HandlerFunc {
w.WriteHeader(http.StatusUnauthorized)
return
}
c.mutex.RLock()
w.Header().Set("Content-Type", "application/json")
c.mutex.RLock()
err := json.NewEncoder(w).Encode(response[T]{Data: c.data, Updated: c.updated})
c.mutex.RUnlock()
if err != nil {
Expand Down

0 comments on commit 80c0c8c

Please sign in to comment.