diff --git a/admin/admin.go b/admin/admin.go index f4d3fe0..2178d90 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -32,6 +32,14 @@ type User struct { type UserList struct { Users []User } +type Config struct { + Name string + Host string + Email string + Description string + Image string + PodcastUrl string +} /* * The following is a set of admin commands @@ -362,7 +370,16 @@ func EditEpisode() common.Handler { func CreateEpisode() common.Handler { return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError { - err := r.ParseMultipartForm(32 << 20) + d, err := ioutil.ReadFile("assets/config/config.json") + if err != nil { + panic(err) + } + var config Config + err = json.Unmarshal(d, &config) + if err != nil { + panic(err) + } + err = r.ParseMultipartForm(32 << 20) if err != nil { return &common.HTTPError{ Message: err.Error(), @@ -373,11 +390,36 @@ func CreateEpisode() common.Handler { // Build filename for episode date := strings.Join(r.Form["date"], "") title := strings.Join(r.Form["title"], "") + db, err := sql.Open("sqlite3", "assets/config/users.db") + defer db.Close() + author := config.Host + if err != nil { + fmt.Println("Error getting user from database", err) + } + + statement, err := db.Prepare("SELECT realname FROM users WHERE username=?") + if err != nil { + fmt.Println("Error getting user from database", err) + } + + rows, err := statement.Query(rc.User.Username) + if err != nil { + fmt.Println("Error getting user from database", err) + } + + var realname string + for rows.Next() { + err = rows.Scan(&realname) + if err != nil { + fmt.Println("Error getting user from database", err) + } + author = realname + } name := fmt.Sprintf("%v_%v", date, title) filename := name + ".mp3" shownotes := name + "_SHOWNOTES.md" - description := strings.Join(r.Form["description"], "") + description := author + "\n" + strings.Join(r.Form["description"], "") // Finish building filenames err = ioutil.WriteFile("./podcasts/"+shownotes, []byte(description), 0644) diff --git a/generate_rss.go b/generate_rss.go index f4a655e..b159a2f 100644 --- a/generate_rss.go +++ b/generate_rss.go @@ -8,13 +8,15 @@ package main import ( + "bufio" + "encoding/json" "fmt" "io/ioutil" "log" - "strings" + "os" "strconv" + "strings" "time" - "encoding/json" "github.com/fsnotify/fsnotify" "github.com/gorilla/feeds" @@ -66,7 +68,7 @@ func watch() { } // Iterate through podcasts directory and build feed -// object, then compile as json and rss and write to file +// object, then compile as json and rss and write to file func GenerateRss() { d, err := ioutil.ReadFile("assets/config/config.json") if err != nil { @@ -99,7 +101,9 @@ func GenerateRss() { s := strings.Split(file.Name(), "_") t := strings.Split(s[1], ".") title := t[0] - description, err := ioutil.ReadFile("podcasts/" + strings.Replace(file.Name(), ".mp3", "_SHOWNOTES.md", 2)) + descfilelines := File2lines("podcasts/" + strings.Replace(file.Name(), ".mp3", "_SHOWNOTES.md", 2)) + author := descfilelines[0] + description := descfilelines[1] if err != nil { log.Fatal(err) } @@ -111,16 +115,16 @@ func GenerateRss() { link := podcasturl + "/download/" + file.Name() feed.Add( &feeds.Item{ - Id: strconv.Itoa(i), + Id: strconv.Itoa(i), Title: title, Link: &feeds.Link{Href: link, Length: size, Type: "audio/mpeg"}, Enclosure: &feeds.Enclosure{Url: link, Length: size, Type: "audio/mpeg"}, - Description: string(description), - Author: &feeds.Author{Name: config.Host, Email: config.Email}, + Description: description, + Author: &feeds.Author{Name: author, Email: config.Email}, // Replace with author in shownotes Created: date, }, ) - i = i + 1; + i = i + 1 } } @@ -143,3 +147,23 @@ func GenerateRss() { json_byte := []byte(json) ioutil.WriteFile("assets/web/feed.json", json_byte, 0644) } + +// From https://siongui.github.io/2016/04/06/go-readlines-from-file-or-string/ +func File2lines(filePath string) []string { + f, err := os.Open(filePath) + if err != nil { + panic(err) + } + defer f.Close() + + var lines []string + scanner := bufio.NewScanner(f) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, err) + } + + return lines +}