This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"podstalk/store"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 != "" {
|
||||
h.Store.SetSetting("podcast_title", title)
|
||||
}
|
||||
|
||||
// Handle podcast image upload
|
||||
file, header, err := r.FormFile("podcast_image")
|
||||
if err == nil {
|
||||
defer file.Close()
|
||||
|
||||
catDir := filepath.Join(h.UploadDir, "images")
|
||||
os.MkdirAll(catDir, 0755)
|
||||
|
||||
ext := filepath.Ext(header.Filename)
|
||||
dst, err := os.Create(filepath.Join(catDir, "podcast"+ext))
|
||||
if err == nil {
|
||||
defer dst.Close()
|
||||
dst.ReadFrom(file)
|
||||
h.Store.SetSetting("podcast_image", "images/podcast"+ext)
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
Reference in New Issue
Block a user