2013-03-01 07:03:41 +00:00
|
|
|
using System.Collections.Generic;
|
2019-10-01 20:33:28 +00:00
|
|
|
using System.IO;
|
2019-06-08 19:13:58 +00:00
|
|
|
using System.Linq;
|
2019-02-16 14:49:24 +00:00
|
|
|
using Marr.Data.QGen;
|
2013-03-01 07:03:41 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2019-02-16 14:49:24 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2013-03-01 07:03:41 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
2017-05-11 18:43:05 +00:00
|
|
|
public interface IMediaFileRepository : IBasicRepository<TrackFile>
|
2013-03-01 07:03:41 +00:00
|
|
|
{
|
2017-06-20 00:57:07 +00:00
|
|
|
List<TrackFile> GetFilesByArtist(int artistId);
|
2017-08-26 02:00:14 +00:00
|
|
|
List<TrackFile> GetFilesByAlbum(int albumId);
|
2019-03-15 12:10:45 +00:00
|
|
|
List<TrackFile> GetFilesByRelease(int releaseId);
|
2019-08-25 15:49:30 +00:00
|
|
|
List<TrackFile> GetUnmappedFiles();
|
2019-06-08 19:13:58 +00:00
|
|
|
List<TrackFile> GetFilesWithBasePath(string path);
|
|
|
|
TrackFile GetFileWithPath(string path);
|
2019-06-26 20:57:31 +00:00
|
|
|
void DeleteFilesByAlbum(int albumId);
|
2013-03-01 07:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-11 18:43:05 +00:00
|
|
|
public class MediaFileRepository : BasicRepository<TrackFile>, IMediaFileRepository
|
2013-03-01 07:03:41 +00:00
|
|
|
{
|
2015-05-03 19:46:21 +00:00
|
|
|
public MediaFileRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
2013-09-14 06:36:07 +00:00
|
|
|
: base(database, eventAggregator)
|
2013-03-01 07:03:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
// always join with all the other good stuff
|
|
|
|
// needed more often than not so better to load it all now
|
|
|
|
protected override QueryBuilder<TrackFile> Query =>
|
|
|
|
DataMapper.Query<TrackFile>()
|
2019-03-15 12:10:45 +00:00
|
|
|
.Join<TrackFile, Track>(JoinType.Left, t => t.Tracks, (t, x) => t.Id == x.TrackFileId)
|
|
|
|
.Join<TrackFile, Album>(JoinType.Left, t => t.Album, (t, a) => t.AlbumId == a.Id)
|
|
|
|
.Join<TrackFile, Artist>(JoinType.Left, t => t.Artist, (t, a) => t.Album.Value.ArtistMetadataId == a.ArtistMetadataId)
|
|
|
|
.Join<Artist, ArtistMetadata>(JoinType.Left, a => a.Metadata, (a, m) => a.ArtistMetadataId == m.Id);
|
2014-04-10 17:58:50 +00:00
|
|
|
|
2017-06-20 00:57:07 +00:00
|
|
|
public List<TrackFile> GetFilesByArtist(int artistId)
|
2014-04-10 17:58:50 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
return Query
|
2019-03-23 08:37:34 +00:00
|
|
|
.Join<Track, AlbumRelease>(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id)
|
2019-02-16 14:49:24 +00:00
|
|
|
.Where<AlbumRelease>(r => r.Monitored == true)
|
|
|
|
.AndWhere(t => t.Artist.Value.Id == artistId)
|
|
|
|
.ToList();
|
2014-04-10 17:58:50 +00:00
|
|
|
}
|
2017-08-26 02:00:14 +00:00
|
|
|
|
|
|
|
public List<TrackFile> GetFilesByAlbum(int albumId)
|
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
return Query
|
2019-03-27 22:05:33 +00:00
|
|
|
.Join<Track, AlbumRelease>(JoinType.Inner, t => t.AlbumRelease, (t, r) => t.AlbumReleaseId == r.Id)
|
|
|
|
.Where<AlbumRelease>(r => r.Monitored == true)
|
|
|
|
.AndWhere(f => f.AlbumId == albumId)
|
2019-02-16 14:49:24 +00:00
|
|
|
.ToList();
|
2017-08-26 02:00:14 +00:00
|
|
|
}
|
2019-03-15 12:10:45 +00:00
|
|
|
|
2019-08-25 15:49:30 +00:00
|
|
|
public List<TrackFile> GetUnmappedFiles()
|
|
|
|
{
|
|
|
|
var query = "SELECT TrackFiles.* " +
|
|
|
|
"FROM TrackFiles " +
|
|
|
|
"LEFT JOIN Tracks ON Tracks.TrackFileId = TrackFiles.Id " +
|
|
|
|
"WHERE Tracks.Id IS NULL ";
|
|
|
|
|
|
|
|
return DataMapper.Query<TrackFile>().QueryText(query).ToList();
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:57:31 +00:00
|
|
|
public void DeleteFilesByAlbum(int albumId)
|
|
|
|
{
|
|
|
|
var ids = DataMapper.Query<TrackFile>().Where(x => x.AlbumId == albumId);
|
|
|
|
DeleteMany(ids);
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:10:45 +00:00
|
|
|
public List<TrackFile> GetFilesByRelease(int releaseId)
|
|
|
|
{
|
|
|
|
return Query
|
|
|
|
.Where<Track>(x => x.AlbumReleaseId == releaseId)
|
|
|
|
.ToList();
|
|
|
|
}
|
2018-01-26 03:01:53 +00:00
|
|
|
|
2019-06-08 19:13:58 +00:00
|
|
|
public List<TrackFile> GetFilesWithBasePath(string path)
|
2018-01-26 03:01:53 +00:00
|
|
|
{
|
2019-10-01 20:33:28 +00:00
|
|
|
// ensure path ends with a single trailing path separator to avoid matching partial paths
|
|
|
|
var safePath = path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
|
2019-02-16 14:49:24 +00:00
|
|
|
return Query
|
2019-10-01 20:33:28 +00:00
|
|
|
.Where(x => x.Path.StartsWith(safePath))
|
2019-02-16 14:49:24 +00:00
|
|
|
.ToList();
|
2018-01-26 03:01:53 +00:00
|
|
|
}
|
2019-06-08 19:13:58 +00:00
|
|
|
|
|
|
|
public TrackFile GetFileWithPath(string path)
|
|
|
|
{
|
|
|
|
return Query.Where(x => x.Path == path).SingleOrDefault();
|
|
|
|
}
|
2013-03-01 07:03:41 +00:00
|
|
|
}
|
2018-01-26 03:01:53 +00:00
|
|
|
}
|