From 4a0ed06dc51a78b867c52cf4c716c4ca5e822ee5 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Thu, 12 Nov 2020 01:36:49 +0100 Subject: [PATCH] chore: split the 'sync' command into muliptle maintenance actions --- pkg/sgtm/http_server.go | 2 +- pkg/sgtm/page_post.go | 58 ++++++++++++++++++++++++++++-------- pkg/sgtm/page_post.tmpl.html | 6 +++- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/pkg/sgtm/http_server.go b/pkg/sgtm/http_server.go index c43a70d..72180d7 100644 --- a/pkg/sgtm/http_server.go +++ b/pkg/sgtm/http_server.go @@ -215,7 +215,7 @@ func (svc *Service) httpServer() (*http.Server, error) { r.Post("/post/{post_slug}", svc.postPage(srcBox)) r.Get("/post/{post_slug}/edit", svc.postEditPage(srcBox)) r.Post("/post/{post_slug}/edit", svc.postEditPage(srcBox)) - r.Get("/post/{post_slug}/sync", svc.postSyncPage(srcBox)) + r.Get("/post/{post_slug}/maintenance", svc.postMaintenancePage(srcBox)) r.Get("/post/{post_slug}/download", svc.postDownloadPage(srcBox)) // FIXME: r.Use(ModeratorOnly) + r.Get("/moderator") // FIXME: r.Use(AdminOnly) + r.Get("/admin") diff --git a/pkg/sgtm/page_post.go b/pkg/sgtm/page_post.go index 765415d..c03cf25 100644 --- a/pkg/sgtm/page_post.go +++ b/pkg/sgtm/page_post.go @@ -118,9 +118,22 @@ func (svc *Service) postPage(box *packr.Box) func(w http.ResponseWriter, r *http } } -func (svc *Service) postSyncPage(box *packr.Box) func(w http.ResponseWriter, r *http.Request) { +func (svc *Service) postMaintenancePage(box *packr.Box) func(w http.ResponseWriter, r *http.Request) { tmpl := loadTemplates(box, "base.tmpl.html", "dummy.tmpl.html") return func(w http.ResponseWriter, r *http.Request) { + var ( + shouldExtractBpm = r.URL.Query().Get("extract_bpm") == "1" + shouldDetectRelationships = r.URL.Query().Get("detect_relationships") == "1" + shouldResyncSoundCloud = r.URL.Query().Get("resync_soundcloud") == "1" + shouldDL = shouldExtractBpm + shouldDoSomething = shouldExtractBpm || shouldDetectRelationships || shouldResyncSoundCloud + ) + if !shouldDoSomething { + svc.error404Page(box)(w, r) + return + } + + // common init started := time.Now() data, err := svc.newTemplateData(w, r) if err != nil { @@ -150,21 +163,40 @@ func (svc *Service) postSyncPage(box *packr.Box) func(w http.ResponseWriter, r * return } - // FIXME: do the sync here - dl, err := DownloadPost(&post, false) - if err != nil { - svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity) - return + // dl file + var dl *Download + if shouldDL { + var err error + dl, err = DownloadPost(&post, false) + if err != nil { + svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity) + return + } + svc.logger.Debug("file downloaded", zap.String("path", dl.Path)) } - svc.logger.Debug("file downloaded", zap.String("path", dl.Path)) - bpm, err := ExtractBPM(dl.Path) - if err != nil { - svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity) + + // resync soundcloud + if shouldResyncSoundCloud { + svc.error404Page(box)(w, r) return } - svc.logger.Debug("BPM extracted", zap.Float64("bpm", bpm)) - if err := svc.rwdb().Model(&post).Update("bpm", bpm).Error; err != nil { - svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity) + + // extract bpm + if shouldExtractBpm { + bpm, err := ExtractBPM(dl.Path) + if err != nil { + svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity) + return + } + svc.logger.Debug("BPM extracted", zap.Float64("bpm", bpm)) + if err := svc.rwdb().Model(&post).Update("bpm", bpm).Error; err != nil { + svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity) + return + } + } + + if shouldDetectRelationships { + svc.error404Page(box)(w, r) return } diff --git a/pkg/sgtm/page_post.tmpl.html b/pkg/sgtm/page_post.tmpl.html index fe0d7d6..f68dc9a 100644 --- a/pkg/sgtm/page_post.tmpl.html +++ b/pkg/sgtm/page_post.tmpl.html @@ -125,7 +125,11 @@