public async Task Update( PlexMediaSource plexMediaSource, List <PlexConnection> toAdd, List <PlexConnection> toDelete) { await _dbConnection.ExecuteAsync( @"UPDATE PlexMediaSource SET ProductVersion = @ProductVersion, ServerName = @ServerName WHERE Id = @Id", new { plexMediaSource.ProductVersion, plexMediaSource.ServerName, plexMediaSource.Id }); await using TvContext dbContext = _dbContextFactory.CreateDbContext(); foreach (PlexConnection add in toAdd) { add.PlexMediaSourceId = plexMediaSource.Id; dbContext.Entry(add).State = EntityState.Added; } foreach (PlexConnection delete in toDelete) { dbContext.Entry(delete).State = EntityState.Deleted; } await dbContext.SaveChangesAsync(); PlexMediaSource pms = await dbContext.PlexMediaSources.FindAsync(plexMediaSource.Id); await dbContext.Entry(pms).Collection(x => x.Connections).LoadAsync(); if (plexMediaSource.Connections.Any() && plexMediaSource.Connections.All(c => !c.IsActive)) { plexMediaSource.Connections.Head().IsActive = true; await dbContext.SaveChangesAsync(); } }
public async Task PlexWindows_To_EtvLinux_UncPath() { var replacements = new List <PlexPathReplacement> { new() { Id = 1, PlexPath = @"\\192.168.1.100\Something\Some Shared Folder", LocalPath = @"/mnt/something else/Some Shared Folder", PlexMediaSource = new PlexMediaSource { Platform = "Windows" } } }; var repo = new Mock <IMediaSourceRepository>(); repo.Setup(x => x.GetPlexPathReplacementsByLibraryId(It.IsAny <int>())).Returns(replacements.AsTask()); var runtime = new Mock <IRuntimeInfo>(); runtime.Setup(x => x.IsOSPlatform(OSPlatform.Windows)).Returns(false); var service = new PlexPathReplacementService( repo.Object, runtime.Object, new Mock <ILogger <PlexPathReplacementService> >().Object); string result = await service.GetReplacementPlexPath( 0, @"\\192.168.1.100\Something\Some Shared Folder\Some Movie\Some Movie.mkv"); result.Should().Be(@"/mnt/something else/Some Shared Folder/Some Movie/Some Movie.mkv"); }
private Validation <BaseError, ConnectionParameters> MediaSourceMustHaveActiveConnection( PlexMediaSource plexMediaSource) { Option <PlexConnection> maybeConnection = plexMediaSource.Connections.SingleOrDefault(c => c.IsActive); return(maybeConnection.Map(connection => new ConnectionParameters(plexMediaSource, connection)) .ToValidation <BaseError>("Plex media source requires an active connection")); }
public async Task <PlexMediaSource> Add(PlexMediaSource plexMediaSource) { await using TvContext context = _dbContextFactory.CreateDbContext(); await context.PlexMediaSources.AddAsync(plexMediaSource); await context.SaveChangesAsync(); return(plexMediaSource); }
private static bool IsWindows(PlexMediaSource plexMediaSource) => plexMediaSource.Platform.ToLowerInvariant() == "windows";
public async Task <Either <BaseError, List <PlexMediaSource> > > GetServers() { try { var result = new List <PlexMediaSource>(); string clientIdentifier = await _plexSecretStore.GetClientIdentifier(); foreach (PlexUserAuthToken token in await _plexSecretStore.GetUserAuthTokens()) { List <PlexResource> httpResources = await _plexTvApi.GetResources( 0, clientIdentifier, token.AuthToken); List <PlexResource> httpsResources = await _plexTvApi.GetResources( 1, clientIdentifier, token.AuthToken); var allResources = httpResources.Filter(resource => resource.HttpsRequired == false) .Append(httpsResources.Filter(resource => resource.HttpsRequired)) .ToList(); IEnumerable <PlexResource> ownedResources = allResources .Filter(r => r.Provides.Split(",").Any(p => p == "server")) .Filter(r => r.Owned); // TODO: maybe support non-owned servers in the future foreach (PlexResource resource in ownedResources) { var serverAuthToken = new PlexServerAuthToken( resource.ClientIdentifier, resource.AccessToken); await _plexSecretStore.UpsertServerAuthToken(serverAuthToken); List <PlexResourceConnection> sortedConnections = resource.HttpsRequired ? resource.Connections : resource.Connections.OrderBy(c => c.Local ? 0 : 1).ToList(); var source = new PlexMediaSource { ServerName = resource.Name, ProductVersion = resource.ProductVersion, Platform = resource.Platform, PlatformVersion = resource.PlatformVersion, ClientIdentifier = resource.ClientIdentifier, Connections = sortedConnections .Map(c => new PlexConnection { Uri = c.Uri }).ToList() }; result.Add(source); } } return(result); } catch (ApiException apiException) { if (apiException.ReasonPhrase == "Unauthorized") { await _plexSecretStore.DeleteAll(); } return(BaseError.New(apiException.Message)); } catch (Exception ex) { _logger.LogError(ex, "Error getting plex servers"); return(BaseError.New(ex.Message)); } }
internal static PlexMediaSourceViewModel ProjectToViewModel(PlexMediaSource plexMediaSource) =>