feat: add ui and handler for export

This commit is contained in:
Gabe Farrell 2025-06-18 08:25:15 -04:00
parent 3157db8b6d
commit f14c25c52f
6 changed files with 108 additions and 20 deletions

33
engine/handlers/export.go Normal file
View file

@ -0,0 +1,33 @@
package handlers
import (
"net/http"
"github.com/gabehf/koito/engine/middleware"
"github.com/gabehf/koito/internal/db"
"github.com/gabehf/koito/internal/export"
"github.com/gabehf/koito/internal/logger"
"github.com/gabehf/koito/internal/utils"
)
func ExportHandler(store db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", `attachment; filename="koito_export.json"`)
ctx := r.Context()
l := logger.FromContext(ctx)
l.Debug().Msg("ExportHandler: Recieved request for export file")
u := middleware.GetUserFromContext(ctx)
if u == nil {
l.Debug().Msg("ExportHandler: Unauthorized access")
utils.WriteError(w, "unauthorized", http.StatusUnauthorized)
return
}
err := export.ExportData(ctx, u, store, w)
if err != nil {
l.Err(err).Msg("ExportHandler: Failed to create export file")
utils.WriteError(w, "failed to create export file", http.StatusInternalServerError)
return
}
}
}

View file

@ -70,6 +70,7 @@ func bindRoutes(
r.Group(func(r chi.Router) {
r.Use(middleware.ValidateSession(db))
r.Get("/export", handlers.ExportHandler(db))
r.Post("/replace-image", handlers.ReplaceImageHandler(db))
r.Patch("/album", handlers.UpdateAlbumHandler(db))
r.Post("/merge/tracks", handlers.MergeTracksHandler(db))