Skip to content

Commit

Permalink
chore: split the 'sync' command into muliptle maintenance actions
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 12, 2020
1 parent e32e7d9 commit 4a0ed06
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/sgtm/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
58 changes: 45 additions & 13 deletions pkg/sgtm/page_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/sgtm/page_post.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ <h1>{{.Post.Post.Title}}</h1>
<div class="card mb-3 bg-danger text-white">
<div class="card-header"><span class="fa fa-user-lock"></span> Admin</div>
<div class="p-2">
<div><a href="{{.Post.Post.CanonicalURL}}/sync" class="text-white"><span class="fa fa-sync"></span> Sync <span class="fab fa-soundcloud"></span></a></div>
<div><a href="{{.Post.Post.CanonicalURL}}/maintenance?extract_bpm=1" class="text-white"><span class="fa fa-sync"></span> Extract BPM</a></div>
<div><a href="{{.Post.Post.CanonicalURL}}/maintenance?detect_relationships=1" class="text-white"><span class="fa fa-sync"></span> Detect Relationships</a></div>
{{ if .Post.Post.IsSoundCloud }}
<div><a href="{{.Post.Post.CanonicalURL}}/maintenance?resync_soundcloud=1" class="text-white"><span class="fa fa-sync"></span> Resync SoundCloud</a></div>
{{ end }}
{{if not (eq .Post.Post.Author.ID .User.ID)}}
<div><a href="{{.Post.Post.CanonicalURL}}/edit" class="text-white"><span class="fa fa-edit"></span> Edit</a></div>
<div><a href="{{.Post.Post.URL}}" class="text-white"><span class="fab fa-soundcloud"></span> See original</a></div>
Expand Down

0 comments on commit 4a0ed06

Please sign in to comment.