Lot 3 (4/5) : API admin S3, variables S3_*, tests et documentation
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
This commit is contained in:
1 parent
53972f9762
commit
e79a813272
6 files changed
+95
No files matched your search
@@ -0,0 +1,65 @@
|
||||
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())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user