132 lines
2.8 KiB
Go
132 lines
2.8 KiB
Go
package old
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/skobkin/magnetico/pkg/persistence"
|
|
"go.uber.org/zap"
|
|
"net/url"
|
|
)
|
|
|
|
type TorrentData struct {
|
|
Torrent persistence.TorrentMetadata
|
|
Files []persistence.File
|
|
}
|
|
|
|
func OpenSqliteDb(url_ url.URL) (*sql.DB, error) {
|
|
url_.Scheme = ""
|
|
|
|
return sql.Open("sqlite3", url_.String())
|
|
}
|
|
|
|
func GetNumberOfTorrents(db *sql.DB) (uint, error) {
|
|
rows, err := db.Query("SELECT COUNT(id) FROM torrents;")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
if rows.Next() != true {
|
|
return 0, fmt.Errorf("No rows returned from `SELECT COUNT(id)`")
|
|
}
|
|
|
|
var n *uint
|
|
if err = rows.Scan(&n); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// If the database is empty (i.e. 0 entries in 'torrents') then the query will return nil.
|
|
if n == nil {
|
|
return 0, nil
|
|
} else {
|
|
return *n, nil
|
|
}
|
|
}
|
|
|
|
func GetLastTorrentId(db *sql.DB) (uint64, error) {
|
|
rows, err := db.Query("SELECT MAX(id) FROM torrents;")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
if rows.Next() != true {
|
|
return 0, fmt.Errorf("No rows returned from `SELECT MAX(id)`")
|
|
}
|
|
|
|
var n *uint64
|
|
if err = rows.Scan(&n); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// If the database is empty (i.e. 0 entries in 'torrents') then the query will return nil.
|
|
if n == nil {
|
|
return 0, nil
|
|
} else {
|
|
return *n, nil
|
|
}
|
|
}
|
|
|
|
func GetTorrentsChunk(db *sql.DB, fromId uint64, size uint64) ([]TorrentData, error) {
|
|
chunk := make([]TorrentData, 0, size)
|
|
|
|
// Selecting torrents
|
|
tRows, err := db.Query(`
|
|
SELECT id, info_hash, name, total_size, discovered_on
|
|
FROM torrents
|
|
WHERE id > ?
|
|
ORDER BY id ASC
|
|
LIMIT ?;
|
|
`, fromId, size)
|
|
if err != nil {
|
|
zap.L().Fatal("Error when querying torrents from old database.")
|
|
}
|
|
defer tRows.Close()
|
|
|
|
for tRows.Next() {
|
|
var torrent persistence.TorrentMetadata
|
|
|
|
err := tRows.Scan(&torrent.ID, &torrent.InfoHash, &torrent.Name, &torrent.Size, &torrent.DiscoveredOn)
|
|
if err != nil {
|
|
zap.L().Error("Error scanning torrent row.", zap.Error(err))
|
|
|
|
return chunk, err
|
|
}
|
|
|
|
files, err := GetFilesForTorrent(db, torrent)
|
|
if err != nil {
|
|
zap.L().Error("Error getting files for torrent.", zap.String("hash", hex.EncodeToString(torrent.InfoHash)), zap.Error(err))
|
|
|
|
return chunk, err
|
|
}
|
|
|
|
chunk = append(chunk, TorrentData{Torrent: torrent, Files: files})
|
|
}
|
|
|
|
return chunk, nil
|
|
}
|
|
|
|
func GetFilesForTorrent(pyDb *sql.DB, torrent persistence.TorrentMetadata) ([]persistence.File, error) {
|
|
files := make([]persistence.File, 0)
|
|
|
|
// Selecting files for torrent
|
|
fRows, err := pyDb.Query("SELECT f.path, f.size FROM files AS f WHERE f.torrent_id = ? ORDER BY f.id ASC;", torrent.ID)
|
|
if err != nil {
|
|
return files, nil
|
|
}
|
|
defer fRows.Close()
|
|
|
|
for fRows.Next() {
|
|
var file persistence.File
|
|
err := fRows.Scan(&file.Path, &file.Size)
|
|
if err != nil {
|
|
return files, err
|
|
}
|
|
|
|
files = append(files, file)
|
|
}
|
|
|
|
return files, nil
|
|
}
|