Updating the database scheme

This commit is contained in:
Zoe Roux 2020-05-08 02:21:59 +02:00
parent 602661db40
commit 90a7d6654e
9 changed files with 188 additions and 121 deletions

View File

@ -12,7 +12,12 @@ namespace Kyoo.Models
public string Poster { get; set; } public string Poster { get; set; }
public string Overview { get; set; } public string Overview { get; set; }
[JsonIgnore] public string ImgPrimary { get; set; } [JsonIgnore] public string ImgPrimary { get; set; }
public virtual IEnumerable<Show> Shows { get; set; } [JsonIgnore] public virtual IEnumerable<CollectionLink> Links { get; set; }
public virtual IEnumerable<Show> Shows
{
get => Links.Select(x => x.Show);
set => Links = value.Select(x => new CollectionLink(this, x));
}
public Collection() { } public Collection() { }

View File

@ -7,5 +7,15 @@ namespace Kyoo.Models
public virtual Collection Collection { get; set; } public virtual Collection Collection { get; set; }
public long ShowID { get; set; } public long ShowID { get; set; }
public virtual Show Show { get; set; } public virtual Show Show { get; set; }
public CollectionLink() { }
public CollectionLink(Collection collection, Show show)
{
Collection = collection;
CollectionID = collection.ID;
Show = show;
ShowID = show.ID;
}
} }
} }

View File

@ -430,25 +430,14 @@ namespace Kyoo.Controllers
return x; return x;
}).ToList(); }).ToList();
show.People = show.People?.Select(x => show.People = show.People?.GroupBy(x => x.Slug)
.Select(x => x.First())
.Select(x =>
{ {
x.People = Validate(x.People); x.People = Validate(x.People);
x.PeopleID = x.People.ID; x.PeopleID = x.People.ID;
return x; return x;
}).ToList(); }).ToList();
show.Seasons = show.Seasons?.Select(x =>
{
return _database.Seasons.FirstOrDefault(y => y.ShowID == x.ShowID
&& y.SeasonNumber == x.SeasonNumber) ?? Validate(x);
}).ToList();
show.Episodes = show.Episodes?.Select(x =>
{
return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID
&& y.SeasonNumber == x.SeasonNumber
&& y.EpisodeNumber == x.EpisodeNumber) ?? Validate(x);
}).ToList();
show.ExternalIDs = Validate(show.ExternalIDs); show.ExternalIDs = Validate(show.ExternalIDs);
return show; return show;
} }
@ -458,12 +447,7 @@ namespace Kyoo.Controllers
if (season == null) if (season == null)
return null; return null;
season.Episodes = season.Episodes?.Select(x => season.Show = Validate(season.Show);
{
return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID
&& y.SeasonNumber == x.SeasonNumber
&& y.EpisodeNumber == x.EpisodeNumber) ?? Validate(x);
}).ToList();
season.ExternalIDs = Validate(season.ExternalIDs); season.ExternalIDs = Validate(season.ExternalIDs);
return season; return season;
} }
@ -473,12 +457,8 @@ namespace Kyoo.Controllers
if (episode == null) if (episode == null)
return null; return null;
Season old = _database.Seasons.FirstOrDefault(x => x.ShowID == episode.ShowID episode.Show = GetShow(episode.Show.Slug) ?? Validate(episode.Show);
&& x.SeasonNumber == episode.SeasonNumber); episode.Season = GetSeason(episode.Show.Slug, episode.SeasonNumber) ?? Validate(episode.Season);
if (old != null)
episode.Season = old;
else
episode.Season.ExternalIDs = Validate(episode.Season.ExternalIDs);
episode.ExternalIDs = Validate(episode.ExternalIDs); episode.ExternalIDs = Validate(episode.ExternalIDs);
return episode; return episode;
} }

View File

@ -118,16 +118,16 @@ namespace Kyoo
modelBuilder.Entity<Show>() modelBuilder.Entity<Show>()
.Ignore(x => x.Genres); .Ignore(x => x.Genres);
modelBuilder.Entity<Collection>()
.Ignore(x => x.Shows);
modelBuilder.Entity<PeopleLink>() modelBuilder.Entity<PeopleLink>()
.Ignore(x => x.Slug); .Ignore(x => x.Slug)
modelBuilder.Entity<PeopleLink>() .Ignore(x => x.Name)
.Ignore(x => x.Name);
modelBuilder.Entity<PeopleLink>()
.Ignore(x => x.ExternalIDs); .Ignore(x => x.ExternalIDs);
modelBuilder.Entity<ProviderLink>() modelBuilder.Entity<ProviderLink>()
.Ignore(x => x.Name); .Ignore(x => x.Name)
modelBuilder.Entity<ProviderLink>()
.Ignore(x => x.Logo); .Ignore(x => x.Logo);

View File

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kyoo.Models.DatabaseMigrations.Internal namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
[DbContext(typeof(DatabaseContext))] [DbContext(typeof(DatabaseContext))]
[Migration("20200426223831_Initial")] [Migration("20200507185533_Initial")]
partial class Initial partial class Initial
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -27,6 +27,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<string>("ImgPrimary") b.Property<string>("ImgPrimary")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Name") b.Property<string>("Name")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
@ -41,6 +44,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID"); b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug") b.HasIndex("Slug")
.IsUnique(); .IsUnique();
@ -271,10 +276,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<string>("PeopleID") b.Property<long>("PeopleID")
.HasColumnType("TEXT");
b.Property<long?>("PeopleID1")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<string>("Role") b.Property<string>("Role")
@ -288,7 +290,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID"); b.HasKey("ID");
b.HasIndex("PeopleID1"); b.HasIndex("PeopleID");
b.HasIndex("ShowID"); b.HasIndex("ShowID");
@ -385,6 +387,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<bool>("IsMovie") b.Property<bool>("IsMovie")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Logo") b.Property<string>("Logo")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
@ -417,6 +422,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID"); b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug") b.HasIndex("Slug")
.IsUnique(); .IsUnique();
@ -485,10 +492,17 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.ToTable("Tracks"); b.ToTable("Tracks");
}); });
modelBuilder.Entity("Kyoo.Models.Collection", b =>
{
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Collections")
.HasForeignKey("LibraryID");
});
modelBuilder.Entity("Kyoo.Models.CollectionLink", b => modelBuilder.Entity("Kyoo.Models.CollectionLink", b =>
{ {
b.HasOne("Kyoo.Models.Collection", "Collection") b.HasOne("Kyoo.Models.Collection", "Collection")
.WithMany() .WithMany("Links")
.HasForeignKey("CollectionID"); .HasForeignKey("CollectionID");
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
@ -572,7 +586,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
b.HasOne("Kyoo.Models.People", "People") b.HasOne("Kyoo.Models.People", "People")
.WithMany("Roles") .WithMany("Roles")
.HasForeignKey("PeopleID1"); .HasForeignKey("PeopleID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("People") .WithMany("People")
@ -605,6 +621,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
modelBuilder.Entity("Kyoo.Models.Show", b => modelBuilder.Entity("Kyoo.Models.Show", b =>
{ {
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Shows")
.HasForeignKey("LibraryID");
b.HasOne("Kyoo.Models.Studio", "Studio") b.HasOne("Kyoo.Models.Studio", "Studio")
.WithMany() .WithMany()
.HasForeignKey("StudioID"); .HasForeignKey("StudioID");

View File

@ -7,23 +7,6 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.CreateTable(
name: "Collections",
columns: table => new
{
ID = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Slug = table.Column<string>(nullable: true),
Name = table.Column<string>(nullable: true),
Poster = table.Column<string>(nullable: true),
Overview = table.Column<string>(nullable: true),
ImgPrimary = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Collections", x => x.ID);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Genres", name: "Genres",
columns: table => new columns: table => new
@ -96,6 +79,30 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
table.PrimaryKey("PK_Studios", x => x.ID); table.PrimaryKey("PK_Studios", x => x.ID);
}); });
migrationBuilder.CreateTable(
name: "Collections",
columns: table => new
{
ID = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Slug = table.Column<string>(nullable: true),
Name = table.Column<string>(nullable: true),
Poster = table.Column<string>(nullable: true),
Overview = table.Column<string>(nullable: true),
ImgPrimary = table.Column<string>(nullable: true),
LibraryID = table.Column<long>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Collections", x => x.ID);
table.ForeignKey(
name: "FK_Collections_Libraries_LibraryID",
column: x => x.LibraryID,
principalTable: "Libraries",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "ProviderLinks", name: "ProviderLinks",
columns: table => new columns: table => new
@ -141,11 +148,18 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
Logo = table.Column<string>(nullable: true), Logo = table.Column<string>(nullable: true),
Backdrop = table.Column<string>(nullable: true), Backdrop = table.Column<string>(nullable: true),
IsMovie = table.Column<bool>(nullable: false), IsMovie = table.Column<bool>(nullable: false),
StudioID = table.Column<long>(nullable: true) StudioID = table.Column<long>(nullable: true),
LibraryID = table.Column<long>(nullable: true)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Shows", x => x.ID); table.PrimaryKey("PK_Shows", x => x.ID);
table.ForeignKey(
name: "FK_Shows_Libraries_LibraryID",
column: x => x.LibraryID,
principalTable: "Libraries",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
table.ForeignKey( table.ForeignKey(
name: "FK_Shows_Studios_StudioID", name: "FK_Shows_Studios_StudioID",
column: x => x.StudioID, column: x => x.StudioID,
@ -243,8 +257,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
ID = table.Column<long>(nullable: false) ID = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true), .Annotation("Sqlite:Autoincrement", true),
PeopleID = table.Column<string>(nullable: true), PeopleID = table.Column<long>(nullable: false),
PeopleID1 = table.Column<long>(nullable: true),
ShowID = table.Column<long>(nullable: false), ShowID = table.Column<long>(nullable: false),
Role = table.Column<string>(nullable: true), Role = table.Column<string>(nullable: true),
Type = table.Column<string>(nullable: true) Type = table.Column<string>(nullable: true)
@ -253,11 +266,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
table.PrimaryKey("PK_PeopleLinks", x => x.ID); table.PrimaryKey("PK_PeopleLinks", x => x.ID);
table.ForeignKey( table.ForeignKey(
name: "FK_PeopleLinks_Peoples_PeopleID1", name: "FK_PeopleLinks_Peoples_PeopleID",
column: x => x.PeopleID1, column: x => x.PeopleID,
principalTable: "Peoples", principalTable: "Peoples",
principalColumn: "ID", principalColumn: "ID",
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Cascade);
table.ForeignKey( table.ForeignKey(
name: "FK_PeopleLinks_Shows_ShowID", name: "FK_PeopleLinks_Shows_ShowID",
column: x => x.ShowID, column: x => x.ShowID,
@ -411,6 +424,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
table: "CollectionLinks", table: "CollectionLinks",
column: "ShowID"); column: "ShowID");
migrationBuilder.CreateIndex(
name: "IX_Collections_LibraryID",
table: "Collections",
column: "LibraryID");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Collections_Slug", name: "IX_Collections_Slug",
table: "Collections", table: "Collections",
@ -485,9 +503,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
column: "ShowID"); column: "ShowID");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_PeopleLinks_PeopleID1", name: "IX_PeopleLinks_PeopleID",
table: "PeopleLinks", table: "PeopleLinks",
column: "PeopleID1"); column: "PeopleID");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_PeopleLinks_ShowID", name: "IX_PeopleLinks_ShowID",
@ -521,6 +539,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
table: "Seasons", table: "Seasons",
column: "ShowID"); column: "ShowID");
migrationBuilder.CreateIndex(
name: "IX_Shows_LibraryID",
table: "Shows",
column: "LibraryID");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Shows_Slug", name: "IX_Shows_Slug",
table: "Shows", table: "Shows",
@ -576,9 +599,6 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Peoples"); name: "Peoples");
migrationBuilder.DropTable(
name: "Libraries");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Providers"); name: "Providers");
@ -591,6 +611,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Shows"); name: "Shows");
migrationBuilder.DropTable(
name: "Libraries");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Studios"); name: "Studios");
} }

View File

@ -25,6 +25,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<string>("ImgPrimary") b.Property<string>("ImgPrimary")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Name") b.Property<string>("Name")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
@ -39,6 +42,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID"); b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug") b.HasIndex("Slug")
.IsUnique(); .IsUnique();
@ -269,10 +274,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<string>("PeopleID") b.Property<long>("PeopleID")
.HasColumnType("TEXT");
b.Property<long?>("PeopleID1")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<string>("Role") b.Property<string>("Role")
@ -286,7 +288,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID"); b.HasKey("ID");
b.HasIndex("PeopleID1"); b.HasIndex("PeopleID");
b.HasIndex("ShowID"); b.HasIndex("ShowID");
@ -383,6 +385,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<bool>("IsMovie") b.Property<bool>("IsMovie")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Logo") b.Property<string>("Logo")
.HasColumnType("TEXT"); .HasColumnType("TEXT");
@ -415,6 +420,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID"); b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug") b.HasIndex("Slug")
.IsUnique(); .IsUnique();
@ -483,10 +490,17 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.ToTable("Tracks"); b.ToTable("Tracks");
}); });
modelBuilder.Entity("Kyoo.Models.Collection", b =>
{
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Collections")
.HasForeignKey("LibraryID");
});
modelBuilder.Entity("Kyoo.Models.CollectionLink", b => modelBuilder.Entity("Kyoo.Models.CollectionLink", b =>
{ {
b.HasOne("Kyoo.Models.Collection", "Collection") b.HasOne("Kyoo.Models.Collection", "Collection")
.WithMany() .WithMany("Links")
.HasForeignKey("CollectionID"); .HasForeignKey("CollectionID");
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
@ -570,7 +584,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{ {
b.HasOne("Kyoo.Models.People", "People") b.HasOne("Kyoo.Models.People", "People")
.WithMany("Roles") .WithMany("Roles")
.HasForeignKey("PeopleID1"); .HasForeignKey("PeopleID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("People") .WithMany("People")
@ -603,6 +619,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
modelBuilder.Entity("Kyoo.Models.Show", b => modelBuilder.Entity("Kyoo.Models.Show", b =>
{ {
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Shows")
.HasForeignKey("LibraryID");
b.HasOne("Kyoo.Models.Studio", "Studio") b.HasOne("Kyoo.Models.Studio", "Studio")
.WithMany() .WithMany()
.HasForeignKey("StudioID"); .HasForeignKey("StudioID");

View File

@ -8,6 +8,7 @@ using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Models.Watch; using Kyoo.Models.Watch;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Controllers namespace Kyoo.Controllers
@ -21,7 +22,7 @@ namespace Kyoo.Controllers
public bool RunOnStartup => true; public bool RunOnStartup => true;
public int Priority => 0; public int Priority => 0;
private ILibraryManager _libraryManager; private IServiceProvider _serviceProvider;
private IThumbnailsManager _thumbnailsManager; private IThumbnailsManager _thumbnailsManager;
private IProviderManager _metadataProvider; private IProviderManager _metadataProvider;
private ITranscoder _transcoder; private ITranscoder _transcoder;
@ -29,7 +30,9 @@ namespace Kyoo.Controllers
public IEnumerable<string> GetPossibleParameters() public IEnumerable<string> GetPossibleParameters()
{ {
return _libraryManager.GetLibraries().Select(x => x.Slug); using IServiceScope serviceScope = _serviceProvider.CreateScope();
ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
return libraryManager.GetLibraries().Select(x => x.Slug);
} }
public int? Progress() public int? Progress()
@ -40,30 +43,30 @@ namespace Kyoo.Controllers
public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null) public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null)
{ {
// TODO Should use more scopes of the library manager (one per episodes to register). _serviceProvider = serviceProvider;
using IServiceScope serviceScope = serviceProvider.CreateScope(); _thumbnailsManager = serviceProvider.GetService<IThumbnailsManager>();
_libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>(); _metadataProvider = serviceProvider.GetService<IProviderManager>();
_thumbnailsManager = serviceScope.ServiceProvider.GetService<IThumbnailsManager>(); _transcoder = serviceProvider.GetService<ITranscoder>();
_metadataProvider = serviceScope.ServiceProvider.GetService<IProviderManager>(); _config = serviceProvider.GetService<IConfiguration>();
_transcoder = serviceScope.ServiceProvider.GetService<ITranscoder>();
_config = serviceScope.ServiceProvider.GetService<IConfiguration>();
try try
{ {
IEnumerable<Episode> episodes = _libraryManager.GetEpisodes(); using IServiceScope serviceScope = _serviceProvider.CreateScope();
ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
IEnumerable<Episode> episodes = libraryManager.GetEpisodes();
IEnumerable<Library> libraries = argument == null IEnumerable<Library> libraries = argument == null
? _libraryManager.GetLibraries() ? libraryManager.GetLibraries()
: new [] {_libraryManager.GetLibrary(argument)}; : new [] {libraryManager.GetLibrary(argument)};
foreach (Episode episode in episodes) foreach (Episode episode in episodes)
{ {
if (!File.Exists(episode.Path)) if (!File.Exists(episode.Path))
_libraryManager.RemoveEpisode(episode); libraryManager.RemoveEpisode(episode);
} }
await _libraryManager.SaveChanges(); await libraryManager.SaveChanges();
foreach (Library library in libraries) foreach (Library library in libraries)
await Scan(library, cancellationToken); await Scan(library, libraryManager, cancellationToken);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -72,7 +75,7 @@ namespace Kyoo.Controllers
Console.WriteLine("Scan finished!"); Console.WriteLine("Scan finished!");
} }
private async Task Scan(Library library, CancellationToken cancellationToken) private async Task Scan(Library library, ILibraryManager libraryManager, CancellationToken cancellationToken)
{ {
Console.WriteLine($"Scanning library {library.Name} at {string.Join(", ", library.Paths)}."); Console.WriteLine($"Scanning library {library.Name} at {string.Join(", ", library.Paths)}.");
foreach (string path in library.Paths) foreach (string path in library.Paths)
@ -106,11 +109,11 @@ namespace Kyoo.Controllers
} }
await Task.WhenAll(files.Select(file => await Task.WhenAll(files.Select(file =>
{ {
if (!IsVideo(file) || _libraryManager.GetEpisodes().Any(x => x.Path == file)) if (!IsVideo(file) || libraryManager.GetEpisodes().Any(x => x.Path == file))
return null; return Task.CompletedTask;
string relativePath = file.Substring(path.Length); string relativePath = file.Substring(path.Length);
return RegisterFile(file, relativePath, library, cancellationToken); return RegisterFile(file, relativePath, library, cancellationToken);
}).Where(x => x != null)); }));
} }
} }
@ -119,6 +122,10 @@ namespace Kyoo.Controllers
if (token.IsCancellationRequested) if (token.IsCancellationRequested)
return; return;
using IServiceScope serviceScope = _serviceProvider.CreateScope();
ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
((DbSet<Library>)libraryManager.GetLibraries()).Attach(library);
Console.WriteLine($"Registering episode at: {path}"); Console.WriteLine($"Registering episode at: {path}");
string patern = _config.GetValue<string>("regex"); string patern = _config.GetValue<string>("regex");
Regex regex = new Regex(patern, RegexOptions.IgnoreCase); Regex regex = new Regex(patern, RegexOptions.IgnoreCase);
@ -131,33 +138,36 @@ namespace Kyoo.Controllers
long episodeNumber = long.TryParse(match.Groups["Episode"].Value, out tmp) ? tmp : -1; long episodeNumber = long.TryParse(match.Groups["Episode"].Value, out tmp) ? tmp : -1;
long absoluteNumber = long.TryParse(match.Groups["Absolute"].Value, out tmp) ? tmp : -1; long absoluteNumber = long.TryParse(match.Groups["Absolute"].Value, out tmp) ? tmp : -1;
Collection collection = await GetCollection(collectionName, library); Collection collection = await GetCollection(libraryManager, collectionName, library);
bool isMovie = seasonNumber == -1 && episodeNumber == -1 && absoluteNumber == -1; bool isMovie = seasonNumber == -1 && episodeNumber == -1 && absoluteNumber == -1;
Show show = await GetShow(showName, showPath, isMovie, library); Show show = await GetShow(libraryManager, showName, showPath, isMovie, library);
if (isMovie) if (isMovie)
_libraryManager.Register(await GetMovie(show, path)); libraryManager.Register(await GetMovie(show, path));
else else
{ {
Season season = await GetSeason(show, seasonNumber, library); Season season = await GetSeason(libraryManager, show, seasonNumber, library);
Episode episode = await GetEpisode(show, season, episodeNumber, absoluteNumber, path, library); Episode episode = await GetEpisode(libraryManager, show, season, episodeNumber, absoluteNumber, path, library);
_libraryManager.Register(episode); libraryManager.Register(episode);
} }
if (collection != null) if (collection != null)
_libraryManager.Register(collection); libraryManager.Register(collection);
_libraryManager.RegisterShowLinks(library, collection, show); libraryManager.RegisterShowLinks(library, collection, show);
await _libraryManager.SaveChanges(); await libraryManager.SaveChanges();
} }
private async Task<Collection> GetCollection(string collectionName, Library library) private async Task<Collection> GetCollection(ILibraryManager libraryManager, string collectionName, Library library)
{ {
if (string.IsNullOrEmpty(collectionName)) if (string.IsNullOrEmpty(collectionName))
return await Task.FromResult<Collection>(null); return await Task.FromResult<Collection>(null);
return _libraryManager.GetCollection(Utility.ToSlug(collectionName)) ?? await _metadataProvider.GetCollectionFromName(collectionName, library); Collection name = libraryManager.GetCollection(Utility.ToSlug(collectionName));
if (name != null)
return name;
return await _metadataProvider.GetCollectionFromName(collectionName, library);
} }
private async Task<Show> GetShow(string showTitle, string showPath, bool isMovie, Library library) private async Task<Show> GetShow(ILibraryManager libraryManager, string showTitle, string showPath, bool isMovie, Library library)
{ {
Show show = _libraryManager.GetShowByPath(showPath); Show show = libraryManager.GetShowByPath(showPath);
if (show != null) if (show != null)
return show; return show;
show = await _metadataProvider.SearchShow(showTitle, isMovie, library); show = await _metadataProvider.SearchShow(showTitle, isMovie, library);
@ -170,11 +180,11 @@ namespace Kyoo.Controllers
return show; return show;
} }
private async Task<Season> GetSeason(Show show, long seasonNumber, Library library) private async Task<Season> GetSeason(ILibraryManager libraryManager, Show show, long seasonNumber, Library library)
{ {
if (seasonNumber == -1) if (seasonNumber == -1)
return default; return default;
Season season = _libraryManager.GetSeason(show.Slug, seasonNumber); Season season = libraryManager.GetSeason(show.Slug, seasonNumber);
if (season == null) if (season == null)
{ {
season = await _metadataProvider.GetSeason(show, seasonNumber, library); season = await _metadataProvider.GetSeason(show, seasonNumber, library);
@ -184,11 +194,11 @@ namespace Kyoo.Controllers
return season; return season;
} }
private async Task<Episode> GetEpisode(Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library) private async Task<Episode> GetEpisode(ILibraryManager libraryManager, Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library)
{ {
Episode episode = await _metadataProvider.GetEpisode(show, episodePath, season?.SeasonNumber ?? -1, episodeNumber, absoluteNumber, library); Episode episode = await _metadataProvider.GetEpisode(show, episodePath, season?.SeasonNumber ?? -1, episodeNumber, absoluteNumber, library);
if (season == null) if (season == null)
season = await GetSeason(show, episode.SeasonNumber, library); season = await GetSeason(libraryManager, show, episode.SeasonNumber, library);
episode.Season = season; episode.Season = season;
if (season == null) if (season == null)
{ {

View File

@ -25,12 +25,11 @@ namespace Kyoo.Api
if (people == null) if (people == null)
return NotFound(); return NotFound();
Collection collection = new Collection(people.Slug, people.Name, null, null) return new Collection(people.Slug, people.Name, null, null)
{ {
Shows = people.Roles.Select(x => x.Show), Shows = people.Roles.Select(x => x.Show),
Poster = "peopleimg/" + people.Slug Poster = "peopleimg/" + people.Slug
}; };
return collection;
} }
} }
} }