Fix dashboard failing to load episodes after RSS import
Build and Deploy / build-and-deploy (push) Successful in 52s
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.
This commit is contained in:
+16
-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)
|
||||
@@ -126,7 +133,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 +159,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 +217,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
|
||||
}
|
||||
|
||||
+1
-1
@@ -243,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)
|
||||
|
||||
+5
-1
@@ -104,6 +104,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 +115,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,
|
||||
})
|
||||
|
||||
+5
-6
@@ -2,7 +2,6 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -15,7 +14,7 @@ type User struct {
|
||||
PodcastSlug string
|
||||
PodcastImage string
|
||||
PodcastAuthor string
|
||||
CreatedAt time.Time
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Episode struct {
|
||||
@@ -25,8 +24,8 @@ type Episode struct {
|
||||
Description string
|
||||
AudioPath string
|
||||
ImagePath string
|
||||
PublishedAt time.Time
|
||||
CreatedAt time.Time
|
||||
PublishedAt string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
@@ -173,7 +172,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 +181,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
|
||||
|
||||
@@ -138,7 +138,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