Compare commits

...

2 Commits

Author SHA1 Message Date
jbrechtel 9011a00475 Fix dashboard failing to load episodes after RSS import
Build and Deploy / build-and-deploy (push) Successful in 52s
The root cause was that modernc.org/sqlite stores time.Time as strings
but cannot scan strings back into time.Time, causing ListEpisodes to
fail. Changed PublishedAt and CreatedAt in store structs from time.Time
to string, with format/parse handled at the application level.

Also fixed a secondary issue where the published_at format stored during
import had a duplicated timezone offset.
2026-07-12 16:17:51 -04:00
jbrechtel b075433e6e Attemping to fix bulk upload problem 2026-07-12 16:11:02 -04:00
10 changed files with 96 additions and 27 deletions
+3
View File
@@ -0,0 +1,3 @@
uploads
podstalk.db
podstalk
+16 -5
View File
@@ -15,6 +15,9 @@ import (
) )
const maxUploadSize = 200 << 20 // 200 MB 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 { type EpisodeHandler struct {
Store *store.Store Store *store.Store
@@ -47,16 +50,20 @@ func (h *EpisodeHandler) ServeDashboard(w http.ResponseWriter, r *http.Request)
Description string Description string
AudioPath string AudioPath string
ImageURL template.URL ImageURL template.URL
PublishedAt time.Time PublishedAt string
} }
var epViews []epView var epViews []epView
for _, ep := range episodes { 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{ ev := epView{
ID: ep.ID, ID: ep.ID,
Title: ep.Title, Title: ep.Title,
Description: ep.Description, Description: ep.Description,
AudioPath: ep.AudioPath, AudioPath: ep.AudioPath,
PublishedAt: ep.PublishedAt, PublishedAt: pubFormatted,
} }
if ep.ImagePath != "" { if ep.ImagePath != "" {
ev.ImageURL = template.URL("/uploads/" + ep.ImagePath) ev.ImageURL = template.URL("/uploads/" + ep.ImagePath)
@@ -126,7 +133,7 @@ func (h *EpisodeHandler) ServeNew(w http.ResponseWriter, r *http.Request) {
imagePath, _ := h.saveUploadedFile(r, "image_file", "images") 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 { if err != nil {
h.Tpl.Render(w, "episode_form", map[string]any{ h.Tpl.Render(w, "episode_form", map[string]any{
"Editing": false, "Editing": false,
@@ -152,10 +159,14 @@ func (h *EpisodeHandler) ServeEdit(w http.ResponseWriter, r *http.Request) {
http.Error(w, "episode not found", http.StatusNotFound) http.Error(w, "episode not found", http.StatusNotFound)
return 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{ h.Tpl.Render(w, "episode_form", map[string]any{
"Editing": true, "Editing": true,
"Episode": ep, "Episode": ep,
"PublishedAt": ep.PublishedAt.Format("2006-01-02T15:04"), "PublishedAt": pubStr,
}) })
return return
} }
@@ -206,7 +217,7 @@ func (h *EpisodeHandler) ServeEdit(w http.ResponseWriter, r *http.Request) {
imagePath = newPath 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) http.Error(w, "update failed", http.StatusInternalServerError)
return return
} }
+38 -4
View File
@@ -1,6 +1,7 @@
package handler package handler
import ( import (
"context"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "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) { 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 { if err != nil {
return nil, err 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 { if err != nil {
http.Redirect(w, r, "/import?error=failed+to+fetch+rss", http.StatusSeeOther) http.Redirect(w, r, "/import?error=failed+to+fetch+rss", http.StatusSeeOther)
return return
@@ -221,7 +243,7 @@ func (h *ImportHandler) importSelected(w http.ResponseWriter, r *http.Request, r
publishedAt = t 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 { if err != nil {
errors = append(errors, fmt.Sprintf("%s: database error: %v", item.Title, err)) errors = append(errors, fmt.Sprintf("%s: database error: %v", item.Title, err))
h.removeFile(audioPath) h.removeFile(audioPath)
@@ -255,8 +277,20 @@ func (h *ImportHandler) episodeImageURL(item *importItem, channel *importChannel
return "" return ""
} }
var importHTTPClient = &http.Client{
Timeout: 5 * time.Minute,
}
func (h *ImportHandler) downloadFile(url, category string) (string, error) { 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 { if err != nil {
return "", err return "", err
} }
+5 -1
View File
@@ -104,6 +104,10 @@ func (h *RSSHandler) ServeRSS(w http.ResponseWriter, r *http.Request) {
itemImage = &rssItunesImage{Href: h.fullURL(epImage)} 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{ items = append(items, rssItem{
Title: ep.Title, Title: ep.Title,
Description: ep.Description, Description: ep.Description,
@@ -111,7 +115,7 @@ func (h *RSSHandler) ServeRSS(w http.ResponseWriter, r *http.Request) {
URL: h.fullURL(ep.AudioPath), URL: h.fullURL(ep.AudioPath),
Type: "audio/mpeg", Type: "audio/mpeg",
}, },
PubDate: ep.PublishedAt.Format(time.RFC1123Z), PubDate: pubStr,
GUID: h.fullURL(ep.AudioPath), GUID: h.fullURL(ep.AudioPath),
ItunesImage: itemImage, ItunesImage: itemImage,
}) })
+8 -3
View File
@@ -1,9 +1,10 @@
package handler package handler
import ( import (
"bytes"
"html/template" "html/template"
"io"
"log" "log"
"net/http"
) )
type Templates struct { type Templates struct {
@@ -13,8 +14,12 @@ type Templates struct {
func NewTemplates(tmpl *template.Template) *Templates { func NewTemplates(tmpl *template.Template) *Templates {
return &Templates{tmpl: tmpl} return &Templates{tmpl: tmpl}
} }
func (t *Templates) Render(w io.Writer, name string, data any) { func (t *Templates) Render(w http.ResponseWriter, name string, data any) {
if err := t.tmpl.ExecuteTemplate(w, name+".html", data); err != nil { var buf bytes.Buffer
if err := t.tmpl.ExecuteTemplate(&buf, name+".html", data); err != nil {
log.Printf("template error: %s: %v", name, err) log.Printf("template error: %s: %v", name, err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
} }
w.Write(buf.Bytes())
} }
+20 -7
View File
@@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"runtime/debug"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@@ -83,13 +84,13 @@ func main() {
http.HandleFunc("/signin", authMid.RedirectIfAuthed(authH.ServeSignin)) http.HandleFunc("/signin", authMid.RedirectIfAuthed(authH.ServeSignin))
http.HandleFunc("/signout", authH.ServeSignout) http.HandleFunc("/signout", authH.ServeSignout)
// Protected routes. // Protected routes with panic recovery.
http.HandleFunc("/", authMid.RequireAuth(epH.ServeDashboard)) http.HandleFunc("/", recovery(authMid.RequireAuth(epH.ServeDashboard)))
http.HandleFunc("/episodes/new", authMid.RequireAuth(epH.ServeNew)) http.HandleFunc("/episodes/new", recovery(authMid.RequireAuth(epH.ServeNew)))
http.HandleFunc("/episodes/edit", authMid.RequireAuth(epH.ServeEdit)) http.HandleFunc("/episodes/edit", recovery(authMid.RequireAuth(epH.ServeEdit)))
http.HandleFunc("/episodes/delete", authMid.RequireAuth(epH.ServeDelete)) http.HandleFunc("/episodes/delete", recovery(authMid.RequireAuth(epH.ServeDelete)))
http.HandleFunc("/settings", authMid.RequireAuth(setH.ServeSettings)) http.HandleFunc("/settings", recovery(authMid.RequireAuth(setH.ServeSettings)))
http.HandleFunc("/import", authMid.RequireAuth(importH.ServeImport)) http.HandleFunc("/import", recovery(authMid.RequireAuth(importH.ServeImport)))
// Public RSS: /rss/my-slug or /rss?slug=my-slug or /rss?user=N (legacy) // Public RSS: /rss/my-slug or /rss?slug=my-slug or /rss?user=N (legacy)
http.HandleFunc("/rss", rssH.ServeRSS) http.HandleFunc("/rss", rssH.ServeRSS)
@@ -106,6 +107,18 @@ func main() {
log.Fatal(http.ListenAndServe(addr, nil)) 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() { func runAddUser() {
if len(os.Args) != 4 { if len(os.Args) != 4 {
log.Fatalf("Usage: podstalk adduser <email> <password>") log.Fatalf("Usage: podstalk adduser <email> <password>")
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+5 -6
View File
@@ -2,7 +2,6 @@ package store
import ( import (
"database/sql" "database/sql"
"time"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
) )
@@ -15,7 +14,7 @@ type User struct {
PodcastSlug string PodcastSlug string
PodcastImage string PodcastImage string
PodcastAuthor string PodcastAuthor string
CreatedAt time.Time CreatedAt string
} }
type Episode struct { type Episode struct {
@@ -25,8 +24,8 @@ type Episode struct {
Description string Description string
AudioPath string AudioPath string
ImagePath string ImagePath string
PublishedAt time.Time PublishedAt string
CreatedAt time.Time CreatedAt string
} }
type Store struct { type Store struct {
@@ -173,7 +172,7 @@ func (s *Store) GetEpisode(id int64) (*Episode, error) {
return e, nil 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 (?, ?, ?, ?, ?, ?)", 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) userID, title, description, audioPath, imagePath, publishedAt)
if err != nil { if err != nil {
@@ -182,7 +181,7 @@ func (s *Store) CreateEpisode(userID int64, title, description, audioPath, image
return res.LastInsertId() 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=?", _, err := s.db.Exec("UPDATE episodes SET title=?, description=?, audio_path=?, image_path=?, published_at=? WHERE id=?",
title, description, audioPath, imagePath, publishedAt, id) title, description, audioPath, imagePath, publishedAt, id)
return err return err
+1 -1
View File
@@ -138,7 +138,7 @@
<td> <td>
{{if .ImageURL}}<img src="{{.ImageURL}}" class="ep-thumb" />{{else}}—{{end}} {{if .ImageURL}}<img src="{{.ImageURL}}" class="ep-thumb" />{{else}}—{{end}}
</td> </td>
<td>{{.PublishedAt.Format "Jan 2, 2006"}}</td> <td>{{.PublishedAt}}</td>
<td>{{if .AudioPath}}✓{{else}}—{{end}}</td> <td>{{if .AudioPath}}✓{{else}}—{{end}}</td>
<td> <td>
<div class="episode-actions"> <div class="episode-actions">