Compare commits
3 Commits
119d4100b5
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e9124ecb0 | |||
| 9011a00475 | |||
| b075433e6e |
@@ -0,0 +1,3 @@
|
||||
uploads
|
||||
podstalk.db
|
||||
podstalk
|
||||
+17
-5
@@ -15,6 +15,9 @@ import (
|
||||
)
|
||||
|
||||
const maxUploadSize = 200 << 20 // 200 MB
|
||||
const timeLayout = "2006-01-02 15:04:05"
|
||||
|
||||
func fmtTime(t time.Time) string { return t.Format(timeLayout) }
|
||||
|
||||
type EpisodeHandler struct {
|
||||
Store *store.Store
|
||||
@@ -47,16 +50,20 @@ func (h *EpisodeHandler) ServeDashboard(w http.ResponseWriter, r *http.Request)
|
||||
Description string
|
||||
AudioPath string
|
||||
ImageURL template.URL
|
||||
PublishedAt time.Time
|
||||
PublishedAt string
|
||||
}
|
||||
var epViews []epView
|
||||
for _, ep := range episodes {
|
||||
pubFormatted := ep.PublishedAt
|
||||
if pt, err := time.Parse(timeLayout, ep.PublishedAt); err == nil {
|
||||
pubFormatted = pt.Format("Jan 2, 2006")
|
||||
}
|
||||
ev := epView{
|
||||
ID: ep.ID,
|
||||
Title: ep.Title,
|
||||
Description: ep.Description,
|
||||
AudioPath: ep.AudioPath,
|
||||
PublishedAt: ep.PublishedAt,
|
||||
PublishedAt: pubFormatted,
|
||||
}
|
||||
if ep.ImagePath != "" {
|
||||
ev.ImageURL = template.URL("/uploads/" + ep.ImagePath)
|
||||
@@ -77,6 +84,7 @@ func (h *EpisodeHandler) ServeDashboard(w http.ResponseWriter, r *http.Request)
|
||||
"PodcastSlug": user.PodcastSlug,
|
||||
"PodcastImageURL": podcastImageURL,
|
||||
"PodcastAuthor": user.PodcastAuthor,
|
||||
"PodcastEmail": user.PodcastEmail,
|
||||
"RSSURL": template.URL(h.BaseURL + "/rss/" + user.PodcastSlug),
|
||||
})
|
||||
}
|
||||
@@ -126,7 +134,7 @@ func (h *EpisodeHandler) ServeNew(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
imagePath, _ := h.saveUploadedFile(r, "image_file", "images")
|
||||
|
||||
_, err = h.Store.CreateEpisode(userID, title, description, audioPath, imagePath, publishedAt)
|
||||
_, err = h.Store.CreateEpisode(userID, title, description, audioPath, imagePath, fmtTime(publishedAt))
|
||||
if err != nil {
|
||||
h.Tpl.Render(w, "episode_form", map[string]any{
|
||||
"Editing": false,
|
||||
@@ -152,10 +160,14 @@ func (h *EpisodeHandler) ServeEdit(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "episode not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
pubStr := ""
|
||||
if pt, err := time.Parse(timeLayout, ep.PublishedAt); err == nil {
|
||||
pubStr = pt.Format("2006-01-02T15:04")
|
||||
}
|
||||
h.Tpl.Render(w, "episode_form", map[string]any{
|
||||
"Editing": true,
|
||||
"Episode": ep,
|
||||
"PublishedAt": ep.PublishedAt.Format("2006-01-02T15:04"),
|
||||
"PublishedAt": pubStr,
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -206,7 +218,7 @@ func (h *EpisodeHandler) ServeEdit(w http.ResponseWriter, r *http.Request) {
|
||||
imagePath = newPath
|
||||
}
|
||||
|
||||
if err := h.Store.UpdateEpisode(id, title, description, audioPath, imagePath, publishedAt); err != nil {
|
||||
if err := h.Store.UpdateEpisode(id, title, description, audioPath, imagePath, fmtTime(publishedAt)); err != nil {
|
||||
http.Error(w, "update failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
+38
-4
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -126,8 +127,20 @@ func (h *ImportHandler) ServeImport(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
var rssHTTPClient = &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
func (h *ImportHandler) fetchRSS(rssURL string) ([]ImportEpisodeView, error) {
|
||||
resp, err := http.Get(rssURL)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rssURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := rssHTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -180,7 +193,16 @@ func (h *ImportHandler) importSelected(w http.ResponseWriter, r *http.Request, r
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := http.Get(rssURL)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rssURL, nil)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/import?error=failed+to+fetch+rss", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := rssHTTPClient.Do(req)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/import?error=failed+to+fetch+rss", http.StatusSeeOther)
|
||||
return
|
||||
@@ -221,7 +243,7 @@ func (h *ImportHandler) importSelected(w http.ResponseWriter, r *http.Request, r
|
||||
publishedAt = t
|
||||
}
|
||||
|
||||
_, err = h.Store.CreateEpisode(userID, item.Title, item.Description, audioPath, imagePath, publishedAt)
|
||||
_, err = h.Store.CreateEpisode(userID, item.Title, item.Description, audioPath, imagePath, publishedAt.Format("2006-01-02 15:04:05"))
|
||||
if err != nil {
|
||||
errors = append(errors, fmt.Sprintf("%s: database error: %v", item.Title, err))
|
||||
h.removeFile(audioPath)
|
||||
@@ -255,8 +277,20 @@ func (h *ImportHandler) episodeImageURL(item *importItem, channel *importChannel
|
||||
return ""
|
||||
}
|
||||
|
||||
var importHTTPClient = &http.Client{
|
||||
Timeout: 5 * time.Minute,
|
||||
}
|
||||
|
||||
func (h *ImportHandler) downloadFile(url, category string) (string, error) {
|
||||
resp, err := http.Get(url)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := importHTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
+21
-1
@@ -26,11 +26,18 @@ type rssChannel struct {
|
||||
Description string `xml:"description"`
|
||||
Language string `xml:"language"`
|
||||
Author string `xml:"itunes:author,omitempty"`
|
||||
ItunesOwner *rssItunesOwner `xml:"itunes:owner,omitempty"`
|
||||
Image *rssImage `xml:"image,omitempty"`
|
||||
ItunesImage *rssItunesImage `xml:"itunes:image,omitempty"`
|
||||
ItunesType string `xml:"itunes:type,omitempty"`
|
||||
Items []rssItem `xml:"item"`
|
||||
}
|
||||
|
||||
type rssItunesOwner struct {
|
||||
Name string `xml:"itunes:name"`
|
||||
Email string `xml:"itunes:email"`
|
||||
}
|
||||
|
||||
type rssImage struct {
|
||||
URL string `xml:"url"`
|
||||
Title string `xml:"title"`
|
||||
@@ -104,6 +111,10 @@ func (h *RSSHandler) ServeRSS(w http.ResponseWriter, r *http.Request) {
|
||||
itemImage = &rssItunesImage{Href: h.fullURL(epImage)}
|
||||
}
|
||||
|
||||
pubStr := ep.PublishedAt
|
||||
if pt, err := time.Parse("2006-01-02 15:04:05", ep.PublishedAt); err == nil {
|
||||
pubStr = pt.Format(time.RFC1123Z)
|
||||
}
|
||||
items = append(items, rssItem{
|
||||
Title: ep.Title,
|
||||
Description: ep.Description,
|
||||
@@ -111,7 +122,7 @@ func (h *RSSHandler) ServeRSS(w http.ResponseWriter, r *http.Request) {
|
||||
URL: h.fullURL(ep.AudioPath),
|
||||
Type: "audio/mpeg",
|
||||
},
|
||||
PubDate: ep.PublishedAt.Format(time.RFC1123Z),
|
||||
PubDate: pubStr,
|
||||
GUID: h.fullURL(ep.AudioPath),
|
||||
ItunesImage: itemImage,
|
||||
})
|
||||
@@ -128,6 +139,14 @@ func (h *RSSHandler) ServeRSS(w http.ResponseWriter, r *http.Request) {
|
||||
chanItunesImage = &rssItunesImage{Href: h.fullURL(user.PodcastImage)}
|
||||
}
|
||||
|
||||
var owner *rssItunesOwner
|
||||
if user.PodcastEmail != "" || author != "" {
|
||||
owner = &rssItunesOwner{
|
||||
Name: author,
|
||||
Email: user.PodcastEmail,
|
||||
}
|
||||
}
|
||||
|
||||
feed := rssFeed{
|
||||
Version: "2.0",
|
||||
ItunesNS: "http://www.itunes.com/dtds/podcast-1.0.dtd",
|
||||
@@ -137,6 +156,7 @@ func (h *RSSHandler) ServeRSS(w http.ResponseWriter, r *http.Request) {
|
||||
Description: title,
|
||||
Language: "en-us",
|
||||
Author: author,
|
||||
ItunesOwner: owner,
|
||||
Image: chanImage,
|
||||
ItunesImage: chanItunesImage,
|
||||
Items: items,
|
||||
|
||||
+6
-1
@@ -64,6 +64,11 @@ func (h *SettingsHandler) ServeSettings(w http.ResponseWriter, r *http.Request)
|
||||
author = user.PodcastAuthor
|
||||
}
|
||||
|
||||
email := r.FormValue("podcast_email")
|
||||
if email == "" {
|
||||
email = user.PodcastEmail
|
||||
}
|
||||
|
||||
image := user.PodcastImage
|
||||
|
||||
// Handle podcast image upload
|
||||
@@ -90,6 +95,6 @@ func (h *SettingsHandler) ServeSettings(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
h.Store.UpdateUserPodcast(userID, title, slug, image, author)
|
||||
h.Store.UpdateUserPodcast(userID, title, slug, image, author, email)
|
||||
http.Redirect(w, r, "/?success=podcast+settings+saved", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
+8
-3
@@ -1,9 +1,10 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Templates struct {
|
||||
@@ -13,8 +14,12 @@ type Templates struct {
|
||||
func NewTemplates(tmpl *template.Template) *Templates {
|
||||
return &Templates{tmpl: tmpl}
|
||||
}
|
||||
func (t *Templates) Render(w io.Writer, name string, data any) {
|
||||
if err := t.tmpl.ExecuteTemplate(w, name+".html", data); err != nil {
|
||||
func (t *Templates) Render(w http.ResponseWriter, name string, data any) {
|
||||
var buf bytes.Buffer
|
||||
if err := t.tmpl.ExecuteTemplate(&buf, name+".html", data); err != nil {
|
||||
log.Printf("template error: %s: %v", name, err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(buf.Bytes())
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
@@ -83,13 +84,13 @@ func main() {
|
||||
http.HandleFunc("/signin", authMid.RedirectIfAuthed(authH.ServeSignin))
|
||||
http.HandleFunc("/signout", authH.ServeSignout)
|
||||
|
||||
// Protected routes.
|
||||
http.HandleFunc("/", authMid.RequireAuth(epH.ServeDashboard))
|
||||
http.HandleFunc("/episodes/new", authMid.RequireAuth(epH.ServeNew))
|
||||
http.HandleFunc("/episodes/edit", authMid.RequireAuth(epH.ServeEdit))
|
||||
http.HandleFunc("/episodes/delete", authMid.RequireAuth(epH.ServeDelete))
|
||||
http.HandleFunc("/settings", authMid.RequireAuth(setH.ServeSettings))
|
||||
http.HandleFunc("/import", authMid.RequireAuth(importH.ServeImport))
|
||||
// Protected routes with panic recovery.
|
||||
http.HandleFunc("/", recovery(authMid.RequireAuth(epH.ServeDashboard)))
|
||||
http.HandleFunc("/episodes/new", recovery(authMid.RequireAuth(epH.ServeNew)))
|
||||
http.HandleFunc("/episodes/edit", recovery(authMid.RequireAuth(epH.ServeEdit)))
|
||||
http.HandleFunc("/episodes/delete", recovery(authMid.RequireAuth(epH.ServeDelete)))
|
||||
http.HandleFunc("/settings", recovery(authMid.RequireAuth(setH.ServeSettings)))
|
||||
http.HandleFunc("/import", recovery(authMid.RequireAuth(importH.ServeImport)))
|
||||
|
||||
// Public RSS: /rss/my-slug or /rss?slug=my-slug or /rss?user=N (legacy)
|
||||
http.HandleFunc("/rss", rssH.ServeRSS)
|
||||
@@ -106,6 +107,18 @@ func main() {
|
||||
log.Fatal(http.ListenAndServe(addr, nil))
|
||||
}
|
||||
|
||||
func recovery(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("panic: %v\n%s", err, debug.Stack())
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}()
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func runAddUser() {
|
||||
if len(os.Args) != 4 {
|
||||
log.Fatalf("Usage: podstalk adduser <email> <password>")
|
||||
|
||||
BIN
Binary file not shown.
+16
-15
@@ -2,7 +2,6 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -15,7 +14,8 @@ type User struct {
|
||||
PodcastSlug string
|
||||
PodcastImage string
|
||||
PodcastAuthor string
|
||||
CreatedAt time.Time
|
||||
PodcastEmail string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Episode struct {
|
||||
@@ -25,8 +25,8 @@ type Episode struct {
|
||||
Description string
|
||||
AudioPath string
|
||||
ImagePath string
|
||||
PublishedAt time.Time
|
||||
CreatedAt time.Time
|
||||
PublishedAt string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
@@ -57,6 +57,7 @@ func migrate(db *sql.DB) error {
|
||||
podcast_slug TEXT NOT NULL DEFAULT '',
|
||||
podcast_image TEXT NOT NULL DEFAULT '',
|
||||
podcast_author TEXT NOT NULL DEFAULT '',
|
||||
podcast_email TEXT NOT NULL DEFAULT '',
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
@@ -97,8 +98,8 @@ func (s *Store) CreateUser(email, passwordHash string) (int64, error) {
|
||||
|
||||
func (s *Store) UserByEmail(email string) (*User, error) {
|
||||
u := &User{}
|
||||
err := s.db.QueryRow("SELECT id, email, password_hash, podcast_title, podcast_slug, podcast_image, podcast_author, created_at FROM users WHERE email = ?", email).
|
||||
Scan(&u.ID, &u.Email, &u.PasswordHash, &u.PodcastTitle, &u.PodcastSlug, &u.PodcastImage, &u.PodcastAuthor, &u.CreatedAt)
|
||||
err := s.db.QueryRow("SELECT id, email, password_hash, podcast_title, podcast_slug, podcast_image, podcast_author, podcast_email, created_at FROM users WHERE email = ?", email).
|
||||
Scan(&u.ID, &u.Email, &u.PasswordHash, &u.PodcastTitle, &u.PodcastSlug, &u.PodcastImage, &u.PodcastAuthor, &u.PodcastEmail, &u.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -107,8 +108,8 @@ func (s *Store) UserByEmail(email string) (*User, error) {
|
||||
|
||||
func (s *Store) GetUser(id int64) (*User, error) {
|
||||
u := &User{}
|
||||
err := s.db.QueryRow("SELECT id, email, password_hash, podcast_title, podcast_slug, podcast_image, podcast_author, created_at FROM users WHERE id = ?", id).
|
||||
Scan(&u.ID, &u.Email, &u.PasswordHash, &u.PodcastTitle, &u.PodcastSlug, &u.PodcastImage, &u.PodcastAuthor, &u.CreatedAt)
|
||||
err := s.db.QueryRow("SELECT id, email, password_hash, podcast_title, podcast_slug, podcast_image, podcast_author, podcast_email, created_at FROM users WHERE id = ?", id).
|
||||
Scan(&u.ID, &u.Email, &u.PasswordHash, &u.PodcastTitle, &u.PodcastSlug, &u.PodcastImage, &u.PodcastAuthor, &u.PodcastEmail, &u.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -117,8 +118,8 @@ func (s *Store) GetUser(id int64) (*User, error) {
|
||||
|
||||
func (s *Store) UserBySlug(slug string) (*User, error) {
|
||||
u := &User{}
|
||||
err := s.db.QueryRow("SELECT id, email, password_hash, podcast_title, podcast_slug, podcast_image, podcast_author, created_at FROM users WHERE podcast_slug = ?", slug).
|
||||
Scan(&u.ID, &u.Email, &u.PasswordHash, &u.PodcastTitle, &u.PodcastSlug, &u.PodcastImage, &u.PodcastAuthor, &u.CreatedAt)
|
||||
err := s.db.QueryRow("SELECT id, email, password_hash, podcast_title, podcast_slug, podcast_image, podcast_author, podcast_email, created_at FROM users WHERE podcast_slug = ?", slug).
|
||||
Scan(&u.ID, &u.Email, &u.PasswordHash, &u.PodcastTitle, &u.PodcastSlug, &u.PodcastImage, &u.PodcastAuthor, &u.PodcastEmail, &u.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,9 +138,9 @@ func (s *Store) SlugTaken(slug string, excludeUserID int64) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdateUserPodcast(id int64, title, slug, image, author string) error {
|
||||
_, err := s.db.Exec("UPDATE users SET podcast_title=?, podcast_slug=?, podcast_image=?, podcast_author=? WHERE id=?",
|
||||
title, slug, image, author, id)
|
||||
func (s *Store) UpdateUserPodcast(id int64, title, slug, image, author, email string) error {
|
||||
_, err := s.db.Exec("UPDATE users SET podcast_title=?, podcast_slug=?, podcast_image=?, podcast_author=?, podcast_email=? WHERE id=?",
|
||||
title, slug, image, author, email, id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -173,7 +174,7 @@ func (s *Store) GetEpisode(id int64) (*Episode, error) {
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateEpisode(userID int64, title, description, audioPath, imagePath string, publishedAt time.Time) (int64, error) {
|
||||
func (s *Store) CreateEpisode(userID int64, title, description, audioPath, imagePath, publishedAt string) (int64, error) {
|
||||
res, err := s.db.Exec("INSERT INTO episodes (user_id, title, description, audio_path, image_path, published_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
userID, title, description, audioPath, imagePath, publishedAt)
|
||||
if err != nil {
|
||||
@@ -182,7 +183,7 @@ func (s *Store) CreateEpisode(userID int64, title, description, audioPath, image
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
||||
func (s *Store) UpdateEpisode(id int64, title, description, audioPath, imagePath string, publishedAt time.Time) error {
|
||||
func (s *Store) UpdateEpisode(id int64, title, description, audioPath, imagePath, publishedAt string) error {
|
||||
_, err := s.db.Exec("UPDATE episodes SET title=?, description=?, audio_path=?, image_path=?, published_at=? WHERE id=?",
|
||||
title, description, audioPath, imagePath, publishedAt, id)
|
||||
return err
|
||||
|
||||
@@ -42,6 +42,11 @@
|
||||
<input type="text" id="podcast_author" name="podcast_author" class="nb-input blue"
|
||||
value="{{.PodcastAuthor}}" placeholder="Podcast Host" />
|
||||
</div>
|
||||
<div class="form-group form-flex-1">
|
||||
<label for="podcast_email">Podcast Email</label>
|
||||
<input type="email" id="podcast_email" name="podcast_email" class="nb-input blue"
|
||||
value="{{.PodcastEmail}}" placeholder="podcast@example.com" />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="podcast_image">Podcast Image</label>
|
||||
<div class="file-upload">
|
||||
@@ -138,7 +143,7 @@
|
||||
<td>
|
||||
{{if .ImageURL}}<img src="{{.ImageURL}}" class="ep-thumb" />{{else}}—{{end}}
|
||||
</td>
|
||||
<td>{{.PublishedAt.Format "Jan 2, 2006"}}</td>
|
||||
<td>{{.PublishedAt}}</td>
|
||||
<td>{{if .AudioPath}}✓{{else}}—{{end}}</td>
|
||||
<td>
|
||||
<div class="episode-actions">
|
||||
|
||||
Reference in New Issue
Block a user