2017-07-25 15:39:49 +01:00
|
|
|
/* admin.go
|
2017-09-21 20:10:16 +01:00
|
|
|
*
|
2017-07-25 15:39:49 +01:00
|
|
|
* Here is where all the neccesary functions for managing episodes
|
|
|
|
* live, e.g adding removing etc.
|
2017-09-11 20:12:24 +01:00
|
|
|
*/
|
2017-07-25 15:39:49 +01:00
|
|
|
|
2017-10-05 06:48:02 +01:00
|
|
|
package admin
|
2017-07-15 16:06:37 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-16 18:43:47 +01:00
|
|
|
"io"
|
2017-09-21 20:10:16 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2017-09-29 19:29:36 +01:00
|
|
|
"os"
|
2017-09-21 20:10:16 +01:00
|
|
|
"strings"
|
2017-10-05 06:48:02 +01:00
|
|
|
|
2017-10-05 20:27:55 +01:00
|
|
|
"github.com/gmemstr/pogo/common"
|
2017-09-11 20:12:24 +01:00
|
|
|
)
|
2017-09-19 20:28:59 +01:00
|
|
|
|
2017-09-11 18:10:35 +01:00
|
|
|
// Write custom CSS to disk or send it back to the client if GET
|
2017-10-05 06:48:02 +01:00
|
|
|
|
|
|
|
func CustomCss() common.Handler {
|
|
|
|
|
|
|
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
|
|
|
|
|
|
|
if r.Method == "GET" {
|
|
|
|
return common.ReadAndServeFile("assets/web/static/custom.css", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
if err != nil {
|
|
|
|
return &common.HTTPError{
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:17 +01:00
|
|
|
css := strings.Join(r.Form["css"], "")
|
|
|
|
|
|
|
|
filename := "custom.css"
|
|
|
|
|
2017-10-05 06:48:02 +01:00
|
|
|
err = ioutil.WriteFile("./assets/web/static/"+filename, []byte(css), 0644)
|
|
|
|
|
2017-09-21 20:10:16 +01:00
|
|
|
if err != nil {
|
2017-09-07 21:45:21 +01:00
|
|
|
w.Write([]byte("<script>window.location = '/admin#failed';</script>"))
|
2017-09-21 20:10:16 +01:00
|
|
|
panic(err)
|
2017-09-03 17:53:17 +01:00
|
|
|
} else {
|
2017-09-21 20:10:16 +01:00
|
|
|
w.Write([]byte("<script>window.location = '/admin#cssupdated';</script>"))
|
2017-09-03 17:53:17 +01:00
|
|
|
}
|
2017-10-05 06:48:02 +01:00
|
|
|
return nil
|
2017-09-03 17:53:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 06:48:02 +01:00
|
|
|
func CreateEpisode() common.Handler {
|
|
|
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
if err != nil {
|
|
|
|
return &common.HTTPError{
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 04:02:14 +01:00
|
|
|
|
|
|
|
// Build filename for episode
|
2017-07-16 18:43:47 +01:00
|
|
|
date := strings.Join(r.Form["date"], "")
|
|
|
|
title := strings.Join(r.Form["title"], "")
|
|
|
|
|
2017-09-21 20:10:16 +01:00
|
|
|
name := fmt.Sprintf("%v_%v", date, title)
|
2017-07-16 18:43:47 +01:00
|
|
|
filename := name + ".mp3"
|
|
|
|
shownotes := name + "_SHOWNOTES.md"
|
|
|
|
fmt.Println(name)
|
|
|
|
description := strings.Join(r.Form["description"], "")
|
|
|
|
fmt.Println(description)
|
2017-09-21 20:10:16 +01:00
|
|
|
// Finish building filenames
|
2017-07-16 18:43:47 +01:00
|
|
|
|
2017-10-05 06:48:02 +01:00
|
|
|
err = ioutil.WriteFile("./podcasts/"+shownotes, []byte(description), 0644)
|
2017-09-21 20:10:16 +01:00
|
|
|
if err != nil {
|
2017-09-07 21:45:21 +01:00
|
|
|
w.Write([]byte("<script>window.location = '/admin#failed';</script>"))
|
2017-09-21 20:10:16 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-07-16 18:43:47 +01:00
|
|
|
|
|
|
|
file, handler, err := r.FormFile("file")
|
2017-09-21 20:10:16 +01:00
|
|
|
if err != nil {
|
2017-09-07 21:45:21 +01:00
|
|
|
w.Write([]byte("<script>window.location = '/admin#failed';</script>"))
|
|
|
|
|
2017-09-21 20:10:16 +01:00
|
|
|
fmt.Println(err)
|
2017-10-05 06:48:02 +01:00
|
|
|
return nil
|
2017-09-21 20:10:16 +01:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
fmt.Println(handler.Header)
|
|
|
|
f, err := os.OpenFile("./podcasts/"+filename, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
2017-09-07 21:45:21 +01:00
|
|
|
w.Write([]byte("<script>window.location = '/admin#failed';</script>"))
|
|
|
|
|
2017-09-21 20:10:16 +01:00
|
|
|
fmt.Println(err)
|
2017-10-05 06:48:02 +01:00
|
|
|
return nil
|
2017-09-21 20:10:16 +01:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
io.Copy(f, file)
|
|
|
|
w.Write([]byte("<script>window.location = '/admin#published';</script>"))
|
2017-10-05 06:48:02 +01:00
|
|
|
|
|
|
|
return nil
|
2017-07-16 18:43:47 +01:00
|
|
|
}
|
2017-07-25 15:39:49 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 06:48:02 +01:00
|
|
|
func RemoveEpisode() common.Handler {
|
|
|
|
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
|
|
|
|
// Episode should be the full MP3 filename
|
|
|
|
// Remove MP3 first
|
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
if err != nil {
|
|
|
|
return &common.HTTPError{
|
|
|
|
Message: err.Error(),
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
episode := strings.Join(r.Form["episode"], "")
|
|
|
|
os.Remove(episode)
|
|
|
|
sn := strings.Replace(episode, ".mp3", "_SHOWNOTES.md", 2)
|
|
|
|
os.Remove(sn)
|
2017-09-07 04:03:24 +01:00
|
|
|
|
2017-10-05 06:48:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-09-11 18:10:35 +01:00
|
|
|
}
|