Skip to content

Commit

Permalink
feat: use album create time for home ui and album listings
Browse files Browse the repository at this point in the history
closes #182
closes #135

Co-authored-by: b-levin <windrider417@gmail.com>
  • Loading branch information
sentriz and b-levin committed Jan 7, 2022
1 parent 9c1f978 commit 14a2668
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/ctrladmin/handlers.go
Expand Up @@ -47,7 +47,7 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
// recent folders box
c.DB.
Where("tag_artist_id IS NOT NULL").
Order("modified_at DESC").
Order("created_at DESC").
Limit(8).
Find(&data.RecentFolders)
data.IsScanning = c.Scanner.IsScanning()
Expand Down
2 changes: 1 addition & 1 deletion server/ctrlsubsonic/handlers_by_folder.go
Expand Up @@ -132,7 +132,7 @@ func (c *Controller) ServeGetAlbumList(r *http.Request) *spec.Response {
user.ID)
q = q.Order("plays.count DESC")
case "newest":
q = q.Order("modified_at DESC")
q = q.Order("created_at DESC")
case "random":
q = q.Order(gorm.Expr("random()"))
case "recent":
Expand Down
22 changes: 19 additions & 3 deletions server/scanner/scanner.go
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"

"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -243,8 +244,8 @@ func (s *Scanner) populateTrackAndAlbumArtists(tx *db.DB, c *ctx, i int, album *
return nil
}

if err := populateAlbum(tx, album, albumArtist, trags, stat.ModTime()); err != nil {
return fmt.Errorf("propulate album: %w", err)
if err := populateAlbum(tx, album, albumArtist, trags, stat.ModTime(), statCreateTime(stat)); err != nil {
return fmt.Errorf("populate album: %w", err)
}

if err := populateAlbumGenres(tx, album, genreIDs); err != nil {
Expand All @@ -254,14 +255,18 @@ func (s *Scanner) populateTrackAndAlbumArtists(tx *db.DB, c *ctx, i int, album *
return nil
}

func populateAlbum(tx *db.DB, album *db.Album, albumArtist *db.Artist, trags tags.Parser, modTime time.Time) error {
func populateAlbum(tx *db.DB, album *db.Album, albumArtist *db.Artist, trags tags.Parser, modTime, createTime time.Time) error {
albumName := trags.SomeAlbum()
album.TagTitle = albumName
album.TagTitleUDec = decoded(albumName)
album.TagBrainzID = trags.AlbumBrainzID()
album.TagYear = trags.Year()
album.TagArtistID = albumArtist.ID

album.ModifiedAt = modTime
if !createTime.IsZero() {
album.CreatedAt = createTime
}

if err := tx.Save(&album).Error; err != nil {
return fmt.Errorf("saving album: %w", err)
Expand Down Expand Up @@ -503,3 +508,14 @@ type ctx struct {
seenAlbums map[int]struct{}
seenTracksNew int
}

func statCreateTime(info fs.FileInfo) time.Time {
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return time.Time{}
}
if stat.Ctim.Sec == 0 {
return time.Time{}
}
return time.Unix(stat.Ctim.Sec, stat.Ctim.Nsec)
}

0 comments on commit 14a2668

Please sign in to comment.