Files
where_woof/frontend/internal/handlers/photo.go

40 lines
880 B
Go

package handlers
import (
"errors"
"io"
"net/http"
"strings"
"github.com/minio/minio-go/v7"
)
// ServePhoto streams a photo object from object storage.
// Used by tags.photo_url values of the form "/photos/tags/{id}.{ext}".
func (a *App) ServePhoto(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.PathValue("key"), "/")
if key == "" || a.Storage == nil {
http.NotFound(w, r)
return
}
obj, ct, err := a.Storage.Get(r.Context(), key)
if err != nil {
var me minio.ErrorResponse
if errors.As(err, &me) && me.Code == "NoSuchKey" {
http.NotFound(w, r)
return
}
http.Error(w, "photo unavailable", http.StatusInternalServerError)
return
}
defer obj.Close()
if ct == "" {
ct = "application/octet-stream"
}
w.Header().Set("Content-Type", ct)
w.Header().Set("Cache-Control", "public, max-age=86400")
_, _ = io.Copy(w, obj)
}