GET/PUT /api/admin/s3 et POST /api/admin/s3/test (administrateurs) ; S3_ENDPOINT, S3_REGION, S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY (_FILE), S3_INSECURE ; miniatures S3 sous DATA_DIR/.s3cache. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LNfwm4pfYR9C914C2EeiuB
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/vblogio/upandown/internal/storage"
|
|
)
|
|
|
|
// Configuration de la racine S3 (administrateurs). La clé secrète n'est
|
|
// jamais renvoyée ; un champ fixé par l'environnement ne peut être modifié.
|
|
|
|
func (h *Handler) adminS3(w http.ResponseWriter, r *http.Request) {
|
|
if !h.admin(w, r) {
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
writeJSON(w, http.StatusOK, h.Spaces.S3Status())
|
|
}
|
|
|
|
func (h *Handler) adminSaveS3(w http.ResponseWriter, r *http.Request) {
|
|
if !h.admin(w, r) {
|
|
return
|
|
}
|
|
var p storage.S3Patch
|
|
if !h.decode(w, r, &p) {
|
|
return
|
|
}
|
|
st, err := h.Spaces.SaveS3(p)
|
|
if err != nil {
|
|
s3Fail(w, err)
|
|
return
|
|
}
|
|
slog.Info("racine S3 : configuration modifiée", "by", userID(r))
|
|
writeJSON(w, http.StatusOK, st)
|
|
}
|
|
|
|
func (h *Handler) adminTestS3(w http.ResponseWriter, r *http.Request) {
|
|
if !h.admin(w, r) {
|
|
return
|
|
}
|
|
var p storage.S3Patch
|
|
if !h.decode(w, r, &p) {
|
|
return
|
|
}
|
|
if err := h.Spaces.TestS3(p); err != nil {
|
|
s3Fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]bool{"ok": true})
|
|
}
|
|
|
|
// s3Fail : les erreurs de connexion au bucket sont renvoyées telles quelles
|
|
// (utiles à l'administrateur).
|
|
func s3Fail(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, storage.ErrS3NotEmpty):
|
|
writeErr(w, http.StatusConflict, err.Error())
|
|
case errors.Is(err, storage.ErrInvalidPath):
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
default:
|
|
writeErr(w, http.StatusBadGateway, err.Error())
|
|
}
|
|
}
|