Exemple #1
0
        private async Task SynchronizeServer(List <PlexMediaSource> allExisting, PlexMediaSource server)
        {
            Option <PlexMediaSource> maybeExisting =
                allExisting.Find(s => s.ClientIdentifier == server.ClientIdentifier);
            await maybeExisting.Match(
                existing =>
            {
                existing.ProductVersion = server.ProductVersion;
                existing.ServerName     = server.ServerName;
                var toAdd = server.Connections
                            .Filter(connection => existing.Connections.All(c => c.Uri != connection.Uri)).ToList();
                var toRemove = existing.Connections
                               .Filter(connection => server.Connections.All(c => c.Uri != connection.Uri)).ToList();
                return(_mediaSourceRepository.Update(existing, toAdd, toRemove));
            },
                async() =>
            {
                if (server.Connections.Any())
                {
                    server.Connections.Head().IsActive = true;
                }

                await _mediaSourceRepository.Add(server);
            });
        }
    private async Task SynchronizeServer(List <PlexMediaSource> allExisting, PlexMediaSource server)
    {
        if (server.Connections.All(c => c.Uri != LocalhostUri))
        {
            var localhost = new PlexConnection
            {
                PlexMediaSource   = server,
                PlexMediaSourceId = server.Id,
                Uri = LocalhostUri
            };

            server.Connections.Add(localhost);
        }

        Option <PlexMediaSource> maybeExisting =
            allExisting.Find(s => s.ClientIdentifier == server.ClientIdentifier);

        foreach (PlexMediaSource existing in maybeExisting)
        {
            existing.Platform        = server.Platform;
            existing.PlatformVersion = server.PlatformVersion;
            existing.ProductVersion  = server.ProductVersion;
            existing.ServerName      = server.ServerName;
            var toAdd = server.Connections
                        .Filter(connection => existing.Connections.All(c => c.Uri != connection.Uri)).ToList();
            var toRemove = existing.Connections
                           .Filter(connection => server.Connections.All(c => c.Uri != connection.Uri)).ToList();
            await _mediaSourceRepository.Update(existing, toAdd, toRemove);
            await FindConnectionToActivate(existing);
        }

        if (maybeExisting.IsNone)
        {
            await _mediaSourceRepository.Add(server);
            await FindConnectionToActivate(server);
        }
    }
        private async Task <Unit> Synchronize(RequestParameters parameters)
        {
            var lastScan = new DateTimeOffset(parameters.Library.LastScan ?? DateTime.MinValue, TimeSpan.Zero);

            if (parameters.ForceScan || lastScan < DateTimeOffset.Now - TimeSpan.FromHours(6))
            {
                switch (parameters.Library.MediaKind)
                {
                case LibraryMediaKind.Movies:
                    await _plexMovieLibraryScanner.ScanLibrary(
                        parameters.ConnectionParameters.ActiveConnection,
                        parameters.ConnectionParameters.PlexServerAuthToken,
                        parameters.Library);

                    break;

                case LibraryMediaKind.Shows:
                    await _plexTelevisionLibraryScanner.ScanLibrary(
                        parameters.ConnectionParameters.ActiveConnection,
                        parameters.ConnectionParameters.PlexServerAuthToken,
                        parameters.Library);

                    break;
                }

                parameters.Library.LastScan = DateTime.UtcNow;
                await _mediaSourceRepository.Update(parameters.Library);
            }
            else
            {
                _logger.LogDebug(
                    "Skipping unforced scan of plex media library {Name}",
                    parameters.Library.Name);
            }

            _entityLocker.UnlockLibrary(parameters.Library.Id);
            return(Unit.Default);
        }