node build fixed
This commit is contained in:
2
seanime-2.9.10/internal/database/db_bridge/README.md
Normal file
2
seanime-2.9.10/internal/database/db_bridge/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
The database may store some structs defined outside as `[]byte` inside `models`.
|
||||
To avoid circular dependencies, we define methods that directly convert `[]byte` to the corresponding struct using the database to store/retrieve them.
|
||||
@@ -0,0 +1,109 @@
|
||||
package db_bridge
|
||||
|
||||
import (
|
||||
"github.com/goccy/go-json"
|
||||
"seanime/internal/database/db"
|
||||
"seanime/internal/database/models"
|
||||
"seanime/internal/library/anime"
|
||||
)
|
||||
|
||||
var CurrAutoDownloaderRules []*anime.AutoDownloaderRule
|
||||
|
||||
func GetAutoDownloaderRules(db *db.Database) ([]*anime.AutoDownloaderRule, error) {
|
||||
|
||||
//if CurrAutoDownloaderRules != nil {
|
||||
// return CurrAutoDownloaderRules, nil
|
||||
//}
|
||||
|
||||
var res []*models.AutoDownloaderRule
|
||||
err := db.Gorm().Find(&res).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshal the data
|
||||
var rules []*anime.AutoDownloaderRule
|
||||
for _, r := range res {
|
||||
smBytes := r.Value
|
||||
var sm anime.AutoDownloaderRule
|
||||
if err := json.Unmarshal(smBytes, &sm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sm.DbID = r.ID
|
||||
rules = append(rules, &sm)
|
||||
}
|
||||
|
||||
//CurrAutoDownloaderRules = rules
|
||||
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
func GetAutoDownloaderRule(db *db.Database, id uint) (*anime.AutoDownloaderRule, error) {
|
||||
var res models.AutoDownloaderRule
|
||||
err := db.Gorm().First(&res, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshal the data
|
||||
smBytes := res.Value
|
||||
var sm anime.AutoDownloaderRule
|
||||
if err := json.Unmarshal(smBytes, &sm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sm.DbID = res.ID
|
||||
|
||||
return &sm, nil
|
||||
}
|
||||
|
||||
func GetAutoDownloaderRulesByMediaId(db *db.Database, mediaId int) (ret []*anime.AutoDownloaderRule) {
|
||||
rules, err := GetAutoDownloaderRules(db)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, rule := range rules {
|
||||
if rule.MediaId == mediaId {
|
||||
ret = append(ret, rule)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func InsertAutoDownloaderRule(db *db.Database, sm *anime.AutoDownloaderRule) error {
|
||||
|
||||
CurrAutoDownloaderRules = nil
|
||||
|
||||
// Marshal the data
|
||||
bytes, err := json.Marshal(sm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Save the data
|
||||
return db.Gorm().Create(&models.AutoDownloaderRule{
|
||||
Value: bytes,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func DeleteAutoDownloaderRule(db *db.Database, id uint) error {
|
||||
|
||||
CurrAutoDownloaderRules = nil
|
||||
|
||||
return db.Gorm().Delete(&models.AutoDownloaderRule{}, id).Error
|
||||
}
|
||||
|
||||
func UpdateAutoDownloaderRule(db *db.Database, id uint, sm *anime.AutoDownloaderRule) error {
|
||||
|
||||
CurrAutoDownloaderRules = nil
|
||||
|
||||
// Marshal the data
|
||||
bytes, err := json.Marshal(sm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Save the data
|
||||
return db.Gorm().Model(&models.AutoDownloaderRule{}).Where("id = ?", id).Update("value", bytes).Error
|
||||
}
|
||||
97
seanime-2.9.10/internal/database/db_bridge/localfiles.go
Normal file
97
seanime-2.9.10/internal/database/db_bridge/localfiles.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package db_bridge
|
||||
|
||||
import (
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/samber/mo"
|
||||
"seanime/internal/database/db"
|
||||
"seanime/internal/database/models"
|
||||
"seanime/internal/library/anime"
|
||||
)
|
||||
|
||||
var CurrLocalFilesDbId uint
|
||||
var CurrLocalFiles mo.Option[[]*anime.LocalFile]
|
||||
|
||||
// GetLocalFiles will return the latest local files and the id of the entry.
|
||||
func GetLocalFiles(db *db.Database) ([]*anime.LocalFile, uint, error) {
|
||||
|
||||
if CurrLocalFiles.IsPresent() {
|
||||
return CurrLocalFiles.MustGet(), CurrLocalFilesDbId, nil
|
||||
}
|
||||
|
||||
// Get the latest entry
|
||||
var res models.LocalFiles
|
||||
err := db.Gorm().Last(&res).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Unmarshal the local files
|
||||
lfsBytes := res.Value
|
||||
var lfs []*anime.LocalFile
|
||||
if err := json.Unmarshal(lfsBytes, &lfs); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
db.Logger.Debug().Msg("db: Local files retrieved")
|
||||
|
||||
CurrLocalFiles = mo.Some(lfs)
|
||||
CurrLocalFilesDbId = res.ID
|
||||
|
||||
return lfs, res.ID, nil
|
||||
}
|
||||
|
||||
// SaveLocalFiles will save the local files in the database at the given id.
|
||||
func SaveLocalFiles(db *db.Database, lfsId uint, lfs []*anime.LocalFile) ([]*anime.LocalFile, error) {
|
||||
// Marshal the local files
|
||||
marshaledLfs, err := json.Marshal(lfs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Save the local files
|
||||
ret, err := db.UpsertLocalFiles(&models.LocalFiles{
|
||||
BaseModel: models.BaseModel{
|
||||
ID: lfsId,
|
||||
},
|
||||
Value: marshaledLfs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshal the saved local files
|
||||
var retLfs []*anime.LocalFile
|
||||
if err := json.Unmarshal(ret.Value, &retLfs); err != nil {
|
||||
return lfs, nil
|
||||
}
|
||||
|
||||
CurrLocalFiles = mo.Some(retLfs)
|
||||
CurrLocalFilesDbId = ret.ID
|
||||
|
||||
return retLfs, nil
|
||||
}
|
||||
|
||||
// InsertLocalFiles will insert the local files in the database at a new entry.
|
||||
func InsertLocalFiles(db *db.Database, lfs []*anime.LocalFile) ([]*anime.LocalFile, error) {
|
||||
|
||||
// Marshal the local files
|
||||
bytes, err := json.Marshal(lfs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Save the local files to the database
|
||||
ret, err := db.InsertLocalFiles(&models.LocalFiles{
|
||||
Value: bytes,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
CurrLocalFiles = mo.Some(lfs)
|
||||
CurrLocalFilesDbId = ret.ID
|
||||
|
||||
return lfs, nil
|
||||
|
||||
}
|
||||
82
seanime-2.9.10/internal/database/db_bridge/playlist.go
Normal file
82
seanime-2.9.10/internal/database/db_bridge/playlist.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package db_bridge
|
||||
|
||||
import (
|
||||
"github.com/goccy/go-json"
|
||||
"seanime/internal/database/db"
|
||||
"seanime/internal/database/models"
|
||||
"seanime/internal/library/anime"
|
||||
)
|
||||
|
||||
func GetPlaylists(db *db.Database) ([]*anime.Playlist, error) {
|
||||
var res []*models.PlaylistEntry
|
||||
err := db.Gorm().Find(&res).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
playlists := make([]*anime.Playlist, 0)
|
||||
for _, p := range res {
|
||||
var localFiles []*anime.LocalFile
|
||||
if err := json.Unmarshal(p.Value, &localFiles); err == nil {
|
||||
playlist := anime.NewPlaylist(p.Name)
|
||||
playlist.SetLocalFiles(localFiles)
|
||||
playlist.DbId = p.ID
|
||||
playlists = append(playlists, playlist)
|
||||
}
|
||||
}
|
||||
return playlists, nil
|
||||
}
|
||||
|
||||
func SavePlaylist(db *db.Database, playlist *anime.Playlist) error {
|
||||
data, err := json.Marshal(playlist.LocalFiles)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
playlistEntry := &models.PlaylistEntry{
|
||||
Name: playlist.Name,
|
||||
Value: data,
|
||||
}
|
||||
|
||||
return db.Gorm().Save(playlistEntry).Error
|
||||
}
|
||||
|
||||
func DeletePlaylist(db *db.Database, id uint) error {
|
||||
return db.Gorm().Where("id = ?", id).Delete(&models.PlaylistEntry{}).Error
|
||||
}
|
||||
|
||||
func UpdatePlaylist(db *db.Database, playlist *anime.Playlist) error {
|
||||
data, err := json.Marshal(playlist.LocalFiles)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the playlist entry
|
||||
playlistEntry := &models.PlaylistEntry{}
|
||||
if err := db.Gorm().Where("id = ?", playlist.DbId).First(playlistEntry).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update the playlist entry
|
||||
playlistEntry.Name = playlist.Name
|
||||
playlistEntry.Value = data
|
||||
|
||||
return db.Gorm().Save(playlistEntry).Error
|
||||
}
|
||||
|
||||
func GetPlaylist(db *db.Database, id uint) (*anime.Playlist, error) {
|
||||
playlistEntry := &models.PlaylistEntry{}
|
||||
if err := db.Gorm().Where("id = ?", id).First(playlistEntry).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var localFiles []*anime.LocalFile
|
||||
if err := json.Unmarshal(playlistEntry.Value, &localFiles); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
playlist := anime.NewPlaylist(playlistEntry.Name)
|
||||
playlist.SetLocalFiles(localFiles)
|
||||
playlist.DbId = playlistEntry.ID
|
||||
|
||||
return playlist, nil
|
||||
}
|
||||
50
seanime-2.9.10/internal/database/db_bridge/scan_summary.go
Normal file
50
seanime-2.9.10/internal/database/db_bridge/scan_summary.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package db_bridge
|
||||
|
||||
import (
|
||||
"seanime/internal/database/db"
|
||||
"seanime/internal/database/models"
|
||||
"seanime/internal/library/summary"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
func GetScanSummaries(database *db.Database) ([]*summary.ScanSummaryItem, error) {
|
||||
var res []*models.ScanSummary
|
||||
err := database.Gorm().Find(&res).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshal the data
|
||||
var items []*summary.ScanSummaryItem
|
||||
for _, r := range res {
|
||||
smBytes := r.Value
|
||||
var sm summary.ScanSummary
|
||||
if err := json.Unmarshal(smBytes, &sm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, &summary.ScanSummaryItem{
|
||||
CreatedAt: r.CreatedAt,
|
||||
ScanSummary: &sm,
|
||||
})
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func InsertScanSummary(db *db.Database, sm *summary.ScanSummary) error {
|
||||
if sm == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshal the data
|
||||
bytes, err := json.Marshal(sm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Save the data
|
||||
return db.Gorm().Create(&models.ScanSummary{
|
||||
Value: bytes,
|
||||
}).Error
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package db_bridge
|
||||
|
||||
import (
|
||||
"github.com/goccy/go-json"
|
||||
"seanime/internal/database/db"
|
||||
"seanime/internal/database/models"
|
||||
hibiketorrent "seanime/internal/extension/hibike/torrent"
|
||||
)
|
||||
|
||||
func GetTorrentstreamHistory(db *db.Database, mId int) (*hibiketorrent.AnimeTorrent, error) {
|
||||
var history models.TorrentstreamHistory
|
||||
if err := db.Gorm().Where("media_id = ?", mId).First(&history).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var torrent hibiketorrent.AnimeTorrent
|
||||
if err := json.Unmarshal(history.Torrent, &torrent); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &torrent, nil
|
||||
}
|
||||
|
||||
func InsertTorrentstreamHistory(db *db.Database, mId int, torrent *hibiketorrent.AnimeTorrent) error {
|
||||
if torrent == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshal the data
|
||||
bytes, err := json.Marshal(torrent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get current history
|
||||
var history models.TorrentstreamHistory
|
||||
if err := db.Gorm().Where("media_id = ?", mId).First(&history).Error; err == nil {
|
||||
// Update the history
|
||||
history.Torrent = bytes
|
||||
return db.Gorm().Save(&history).Error
|
||||
}
|
||||
|
||||
return db.Gorm().Create(&models.TorrentstreamHistory{
|
||||
MediaId: mId,
|
||||
Torrent: bytes,
|
||||
}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user