photo-object-storage: MinIO on .13 (9010/9011), Go upload+serve /photos, admin S3 disk + Filament upload, migrated, deployed
This commit is contained in:
39
frontend/internal/handlers/photo.go
Normal file
39
frontend/internal/handlers/photo.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user