9e9124ecb0
Build and Deploy / build-and-deploy (push) Successful in 56s
Enables users to set a podcast contact email from the dashboard settings. This email is included in the RSS feed as itunes:owner/itunes:email, which podcast directories like Spotify and Apple Podcasts require for indexing and verification.
101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
|
|
"podstalk/store"
|
|
)
|
|
|
|
var slugRe = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)
|
|
|
|
type SettingsHandler struct {
|
|
Store *store.Store
|
|
UploadDir string
|
|
}
|
|
|
|
func (h *SettingsHandler) ServeSettings(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
userID := UserIDFromContext(r)
|
|
user, err := h.Store.GetUser(userID)
|
|
if err != nil {
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
return
|
|
}
|
|
|
|
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
|
http.Redirect(w, r, "/?error=file+too+large", http.StatusSeeOther)
|
|
return
|
|
}
|
|
|
|
title := r.FormValue("podcast_title")
|
|
if title == "" {
|
|
title = user.PodcastTitle
|
|
}
|
|
|
|
slug := r.FormValue("podcast_slug")
|
|
if slug == "" {
|
|
slug = user.PodcastSlug
|
|
}
|
|
if slug != "" {
|
|
if !slugRe.MatchString(slug) {
|
|
http.Redirect(w, r, "/?error=slug+must+be+lowercase+letters+numbers+and+hyphens", http.StatusSeeOther)
|
|
return
|
|
}
|
|
taken, err := h.Store.SlugTaken(slug, userID)
|
|
if err != nil {
|
|
http.Redirect(w, r, "/?error=internal+error", http.StatusSeeOther)
|
|
return
|
|
}
|
|
if taken {
|
|
http.Redirect(w, r, "/?error=slug+already+taken", http.StatusSeeOther)
|
|
return
|
|
}
|
|
}
|
|
|
|
author := r.FormValue("podcast_author")
|
|
if author == "" {
|
|
author = user.PodcastAuthor
|
|
}
|
|
|
|
email := r.FormValue("podcast_email")
|
|
if email == "" {
|
|
email = user.PodcastEmail
|
|
}
|
|
|
|
image := user.PodcastImage
|
|
|
|
// Handle podcast image upload
|
|
file, header, err := r.FormFile("podcast_image")
|
|
if err == nil {
|
|
defer file.Close()
|
|
|
|
if image != "" {
|
|
os.Remove(filepath.Join(h.UploadDir, image))
|
|
}
|
|
|
|
catDir := filepath.Join(h.UploadDir, "images")
|
|
os.MkdirAll(catDir, 0755)
|
|
|
|
ext := filepath.Ext(header.Filename)
|
|
if ext == "" {
|
|
ext = ".png"
|
|
}
|
|
dst, err := os.Create(filepath.Join(catDir, "podcast"+ext))
|
|
if err == nil {
|
|
defer dst.Close()
|
|
dst.ReadFrom(file)
|
|
image = "images/podcast" + ext
|
|
}
|
|
}
|
|
|
|
h.Store.UpdateUserPodcast(userID, title, slug, image, author, email)
|
|
http.Redirect(w, r, "/?success=podcast+settings+saved", http.StatusSeeOther)
|
|
}
|