refactor: update scanner, scanner tests, mockfs
closes #165 closes #163
This commit is contained in:
@@ -24,16 +24,25 @@ import (
|
||||
"go.senan.xyz/gonic/server/scanner/tags"
|
||||
)
|
||||
|
||||
const DownloadAllWaitInterval = 3 * time.Second
|
||||
const downloadAllWaitInterval = 3 * time.Second
|
||||
|
||||
type Podcasts struct {
|
||||
DB *db.DB
|
||||
PodcastBasePath string
|
||||
db *db.DB
|
||||
baseDir string
|
||||
tagger tags.Reader
|
||||
}
|
||||
|
||||
func New(db *db.DB, base string, tagger tags.Reader) *Podcasts {
|
||||
return &Podcasts{
|
||||
db: db,
|
||||
baseDir: base,
|
||||
tagger: tagger,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Podcasts) GetPodcastOrAll(userID int, id int, includeEpisodes bool) ([]*db.Podcast, error) {
|
||||
podcasts := []*db.Podcast{}
|
||||
q := p.DB.Where("user_id=?", userID)
|
||||
q := p.db.Where("user_id=?", userID)
|
||||
if id != 0 {
|
||||
q = q.Where("id=?", id)
|
||||
}
|
||||
@@ -55,7 +64,7 @@ func (p *Podcasts) GetPodcastOrAll(userID int, id int, includeEpisodes bool) ([]
|
||||
|
||||
func (p *Podcasts) GetPodcastEpisodes(podcastID int) ([]*db.PodcastEpisode, error) {
|
||||
episodes := []*db.PodcastEpisode{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("podcast_id=?", podcastID).
|
||||
Order("publish_date DESC").
|
||||
Find(&episodes).
|
||||
@@ -75,12 +84,12 @@ func (p *Podcasts) AddNewPodcast(rssURL string, feed *gofeed.Feed,
|
||||
Title: feed.Title,
|
||||
URL: rssURL,
|
||||
}
|
||||
podPath := podcast.Fullpath(p.PodcastBasePath)
|
||||
podPath := podcast.Fullpath(p.baseDir)
|
||||
err := os.Mkdir(podPath, 0755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.DB.Save(&podcast).Error; err != nil {
|
||||
if err := p.db.Save(&podcast).Error; err != nil {
|
||||
return &podcast, err
|
||||
}
|
||||
if err := p.AddNewEpisodes(&podcast, feed.Items); err != nil {
|
||||
@@ -96,7 +105,7 @@ func (p *Podcasts) AddNewPodcast(rssURL string, feed *gofeed.Feed,
|
||||
|
||||
func (p *Podcasts) SetAutoDownload(podcastID int, setting db.PodcastAutoDownload) error {
|
||||
podcast := db.Podcast{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("id=?", podcastID).
|
||||
First(&podcast).
|
||||
Error
|
||||
@@ -104,7 +113,7 @@ func (p *Podcasts) SetAutoDownload(podcastID int, setting db.PodcastAutoDownload
|
||||
return err
|
||||
}
|
||||
podcast.AutoDownload = setting
|
||||
if err := p.DB.Save(&podcast).Error; err != nil {
|
||||
if err := p.db.Save(&podcast).Error; err != nil {
|
||||
return fmt.Errorf("save setting: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -123,7 +132,7 @@ func getEntriesAfterDate(feed []*gofeed.Item, after time.Time) []*gofeed.Item {
|
||||
|
||||
func (p *Podcasts) AddNewEpisodes(podcast *db.Podcast, items []*gofeed.Item) error {
|
||||
podcastEpisode := db.PodcastEpisode{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("podcast_id=?", podcast.ID).
|
||||
Order("publish_date DESC").
|
||||
First(&podcastEpisode).Error
|
||||
@@ -192,13 +201,13 @@ func (p *Podcasts) AddEpisode(podcastID int, item *gofeed.Item) (*db.PodcastEpis
|
||||
}
|
||||
|
||||
if episode, ok := p.findEnclosureAudio(podcastID, duration, item); ok {
|
||||
if err := p.DB.Save(episode).Error; err != nil {
|
||||
if err := p.db.Save(episode).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return episode, nil
|
||||
}
|
||||
if episode, ok := p.findMediaAudio(podcastID, duration, item); ok {
|
||||
if err := p.DB.Save(episode).Error; err != nil {
|
||||
if err := p.db.Save(episode).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return episode, nil
|
||||
@@ -259,7 +268,7 @@ func (p *Podcasts) findMediaAudio(podcastID, duration int,
|
||||
|
||||
func (p *Podcasts) RefreshPodcasts() error {
|
||||
podcasts := []*db.Podcast{}
|
||||
if err := p.DB.Find(&podcasts).Error; err != nil {
|
||||
if err := p.db.Find(&podcasts).Error; err != nil {
|
||||
return fmt.Errorf("find podcasts: %w", err)
|
||||
}
|
||||
var errs *multierr.Err
|
||||
@@ -271,7 +280,7 @@ func (p *Podcasts) RefreshPodcasts() error {
|
||||
|
||||
func (p *Podcasts) RefreshPodcastsForUser(userID int) error {
|
||||
podcasts := []*db.Podcast{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("user_id=?", userID).
|
||||
Find(&podcasts).
|
||||
Error
|
||||
@@ -304,7 +313,7 @@ func (p *Podcasts) refreshPodcasts(podcasts []*db.Podcast) error {
|
||||
|
||||
func (p *Podcasts) DownloadPodcastAll(podcastID int) error {
|
||||
podcastEpisodes := []db.PodcastEpisode{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("podcast_id=?", podcastID).
|
||||
Find(&podcastEpisodes).
|
||||
Error
|
||||
@@ -322,7 +331,7 @@ func (p *Podcasts) DownloadPodcastAll(podcastID int) error {
|
||||
continue
|
||||
}
|
||||
log.Printf("finished downloading episode: %q", episode.Title)
|
||||
time.Sleep(DownloadAllWaitInterval)
|
||||
time.Sleep(downloadAllWaitInterval)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
@@ -331,14 +340,14 @@ func (p *Podcasts) DownloadPodcastAll(podcastID int) error {
|
||||
func (p *Podcasts) DownloadEpisode(episodeID int) error {
|
||||
podcastEpisode := db.PodcastEpisode{}
|
||||
podcast := db.Podcast{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("id=?", episodeID).
|
||||
First(&podcastEpisode).
|
||||
Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("get podcast episode by id: %w", err)
|
||||
}
|
||||
err = p.DB.
|
||||
err = p.db.
|
||||
Where("id=?", podcastEpisode.PodcastID).
|
||||
First(&podcast).
|
||||
Error
|
||||
@@ -350,7 +359,7 @@ func (p *Podcasts) DownloadEpisode(episodeID int) error {
|
||||
return nil
|
||||
}
|
||||
podcastEpisode.Status = db.PodcastEpisodeStatusDownloading
|
||||
p.DB.Save(&podcastEpisode)
|
||||
p.db.Save(&podcastEpisode)
|
||||
// nolint: bodyclose
|
||||
resp, err := http.Get(podcastEpisode.AudioURL)
|
||||
if err != nil {
|
||||
@@ -365,14 +374,14 @@ func (p *Podcasts) DownloadEpisode(episodeID int) error {
|
||||
filename = path.Base(audioURL.Path)
|
||||
}
|
||||
filename = p.findUniqueEpisodeName(&podcast, &podcastEpisode, filename)
|
||||
audioFile, err := os.Create(path.Join(podcast.Fullpath(p.PodcastBasePath), filename))
|
||||
audioFile, err := os.Create(path.Join(podcast.Fullpath(p.baseDir), filename))
|
||||
if err != nil {
|
||||
return fmt.Errorf("create audio file: %w", err)
|
||||
}
|
||||
podcastEpisode.Filename = filename
|
||||
sanTitle := strings.ReplaceAll(podcast.Title, "/", "_")
|
||||
podcastEpisode.Path = path.Join(sanTitle, filename)
|
||||
p.DB.Save(&podcastEpisode)
|
||||
p.db.Save(&podcastEpisode)
|
||||
go func() {
|
||||
if err := p.doPodcastDownload(&podcastEpisode, audioFile, resp.Body); err != nil {
|
||||
log.Printf("error downloading podcast: %v", err)
|
||||
@@ -385,18 +394,18 @@ func (p *Podcasts) findUniqueEpisodeName(
|
||||
podcast *db.Podcast,
|
||||
podcastEpisode *db.PodcastEpisode,
|
||||
filename string) string {
|
||||
podcastPath := path.Join(podcast.Fullpath(p.PodcastBasePath), filename)
|
||||
podcastPath := path.Join(podcast.Fullpath(p.baseDir), filename)
|
||||
if _, err := os.Stat(podcastPath); os.IsNotExist(err) {
|
||||
return filename
|
||||
}
|
||||
sanitizedTitle := strings.ReplaceAll(podcastEpisode.Title, "/", "_")
|
||||
titlePath := fmt.Sprintf("%s%s", sanitizedTitle, filepath.Ext(filename))
|
||||
podcastPath = path.Join(podcast.Fullpath(p.PodcastBasePath), titlePath)
|
||||
podcastPath = path.Join(podcast.Fullpath(p.baseDir), titlePath)
|
||||
if _, err := os.Stat(podcastPath); os.IsNotExist(err) {
|
||||
return titlePath
|
||||
}
|
||||
// try to find a filename like FILENAME (1).mp3 incrementing
|
||||
return findEpisode(podcast.Fullpath(p.PodcastBasePath), filename, 1)
|
||||
return findEpisode(podcast.Fullpath(p.baseDir), filename, 1)
|
||||
}
|
||||
|
||||
func findEpisode(base, filename string, count int) string {
|
||||
@@ -442,7 +451,7 @@ func (p *Podcasts) downloadPodcastCover(podPath string, podcast *db.Podcast) err
|
||||
podcastPath := filepath.Clean(strings.ReplaceAll(podcast.Title, "/", "_"))
|
||||
podcastFilename := fmt.Sprintf("cover%s", ext)
|
||||
podcast.ImagePath = path.Join(podcastPath, podcastFilename)
|
||||
if err := p.DB.Save(podcast).Error; err != nil {
|
||||
if err := p.db.Save(podcast).Error; err != nil {
|
||||
return fmt.Errorf("save podcast: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -454,24 +463,24 @@ func (p *Podcasts) doPodcastDownload(podcastEpisode *db.PodcastEpisode, file *os
|
||||
}
|
||||
defer file.Close()
|
||||
stat, _ := file.Stat()
|
||||
podcastPath := path.Join(p.PodcastBasePath, podcastEpisode.Path)
|
||||
podcastTags, err := tags.New(podcastPath)
|
||||
podcastPath := path.Join(p.baseDir, podcastEpisode.Path)
|
||||
podcastTags, err := p.tagger.Read(podcastPath)
|
||||
if err != nil {
|
||||
log.Printf("error parsing podcast audio: %e", err)
|
||||
podcastEpisode.Status = db.PodcastEpisodeStatusError
|
||||
p.DB.Save(podcastEpisode)
|
||||
p.db.Save(podcastEpisode)
|
||||
return nil
|
||||
}
|
||||
podcastEpisode.Bitrate = podcastTags.Bitrate()
|
||||
podcastEpisode.Status = db.PodcastEpisodeStatusCompleted
|
||||
podcastEpisode.Length = podcastTags.Length()
|
||||
podcastEpisode.Size = int(stat.Size())
|
||||
return p.DB.Save(podcastEpisode).Error
|
||||
return p.db.Save(podcastEpisode).Error
|
||||
}
|
||||
|
||||
func (p *Podcasts) DeletePodcast(userID, podcastID int) error {
|
||||
podcast := db.Podcast{}
|
||||
err := p.DB.
|
||||
err := p.db.
|
||||
Where("id=? AND user_id=?", podcastID, userID).
|
||||
First(&podcast).
|
||||
Error
|
||||
@@ -479,17 +488,17 @@ func (p *Podcasts) DeletePodcast(userID, podcastID int) error {
|
||||
return err
|
||||
}
|
||||
var userCount int
|
||||
p.DB.
|
||||
p.db.
|
||||
Model(&db.Podcast{}).
|
||||
Where("title=?", podcast.Title).
|
||||
Count(&userCount)
|
||||
if userCount == 1 {
|
||||
// only delete the folder if there are not multiple listeners
|
||||
if err = os.RemoveAll(podcast.Fullpath(p.PodcastBasePath)); err != nil {
|
||||
if err = os.RemoveAll(podcast.Fullpath(p.baseDir)); err != nil {
|
||||
return fmt.Errorf("delete podcast directory: %w", err)
|
||||
}
|
||||
}
|
||||
err = p.DB.
|
||||
err = p.db.
|
||||
Where("id=? AND user_id=?", podcastID, userID).
|
||||
Delete(db.Podcast{}).
|
||||
Error
|
||||
@@ -501,13 +510,13 @@ func (p *Podcasts) DeletePodcast(userID, podcastID int) error {
|
||||
|
||||
func (p *Podcasts) DeletePodcastEpisode(podcastEpisodeID int) error {
|
||||
episode := db.PodcastEpisode{}
|
||||
err := p.DB.First(&episode, podcastEpisodeID).Error
|
||||
err := p.db.First(&episode, podcastEpisodeID).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
episode.Status = db.PodcastEpisodeStatusDeleted
|
||||
p.DB.Save(&episode)
|
||||
if err := os.Remove(filepath.Join(p.PodcastBasePath, episode.Path)); err != nil {
|
||||
p.db.Save(&episode)
|
||||
if err := os.Remove(filepath.Join(p.baseDir, episode.Path)); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user