private void GivenMatchingRemoteLibraries()
        {
            _remoteLibraryContainer = new PlexMediaContainerBuilder().Build();

            for (var i = 0; i < _plexServer.PlexLibraries.Count; i++)
            {
                _remoteLibraryContainer.MediaContainer.Directory[i].Key = _plexServer.PlexLibraries.ElementAt(i).LibraryKey;
            }

            _plexApi.GetLibraries(Arg.Any <string>(), Arg.Any <string>()).Returns(_remoteLibraryContainer);
        }
        private void GivenRemoteLibraries(bool hasRemoteLibraries)
        {
            _remoteLibraryContainer = new PlexMediaContainerBuilder().WithMetadata().WithDirectories().Build();

            if (!hasRemoteLibraries)
            {
                _remoteLibraryContainer.MediaContainer.Directory = new List <Directory>();
            }

            _plexApi.GetLibraries(Arg.Any <string>(), Arg.Any <string>()).Returns(_remoteLibraryContainer);
        }
Beispiel #3
0
        public async Task Synchronise(bool fullRefresh)
        {
            var refreshType = fullRefresh ? "full" : "partial";

            _logger.LogInformation($"Starting a {refreshType} sync with Plex.");

            var plexServer = await _plexService.GetServer();

            if (plexServer == null)
            {
                _logger.LogInformation("Unable to sync plex content as no admin server was found");
            }

            var librariesToSync = plexServer?.PlexLibraries.Where(x => x.IsEnabled).ToList();

            if (librariesToSync == null || !librariesToSync.Any())
            {
                _logger.LogDebug("No Plex libraries have been enabled for synchronisation");
                return;
            }

            if (fullRefresh)
            {
                //TODO Why are we doing this? It should be a soft delete
                //What about issues/requests that use this as a FK?
                _plexService.DeleteAllMediaItems();
            }

            var plexUrl = plexServer.GetPlexUri(_plexSettings.ConnectLocally);

            var plexLibraryContainer = await _plexApi.GetLibraries(plexServer.AccessToken, plexUrl);

            foreach (var libraryToSync in librariesToSync)
            {
                var existsAsRemoteLibrary =
                    plexLibraryContainer.MediaContainer.Directory.Any(x => x.Key == libraryToSync.LibraryKey);

                if (!existsAsRemoteLibrary)
                {
                    _logger.LogInformation($"Attempted to sync the local library '{libraryToSync.Type}|{libraryToSync.LibraryKey}' but it no longer exists remotely");
                    continue;
                }

                var syncProcessor = _processorProvider.GetProcessor(libraryToSync.Type);

                if (syncProcessor == null)
                {
                    _logger.LogInformation($"Attempted to sync the local library '{libraryToSync.Type}|{libraryToSync.LibraryKey}' but the type is not supported");
                    return;
                }

                await SynchroniseLibrary(fullRefresh, libraryToSync, plexServer, plexUrl, syncProcessor);
            }
        }
        private async Task CreateAdminServer(Server adminServer, User plexUser)
        {
            _logger.LogInformation("Found a PlexServer owned by the Admin account");
            var plexServer = new PlexServerRow
            {
                Identifier        = Guid.NewGuid(),
                AccessToken       = adminServer.AccessToken,
                Name              = adminServer.Name,
                MachineIdentifier = adminServer.MachineIdentifier,
                LocalIp           = adminServer.LocalAddresses.Split(",").FirstOrDefault(),
                LocalPort         = _plexSettings.DefaultLocalPort,
                ExternalIp        = adminServer.Address,
                ExternalPort      = Convert.ToInt32(adminServer.Port),
                Scheme            = adminServer.Scheme,
                PlexLibraries     = new List <PlexLibraryRow>()
            };

            _logger.LogInformation("Getting available libraries on PlexServer");

            var libraryContainer = await _plexApi.GetLibraries(plexUser.AuthToken,
                                                               plexServer.GetPlexUri(_plexSettings.ConnectLocally));

            var directories = libraryContainer?.MediaContainer.Directory;

            if (directories != null)
            {
                _logger.LogInformation(
                    $"Identified '{directories.Count}' libraries on the PlexServer");

                plexServer.PlexLibraries = directories.Select(x =>
                                                              new PlexLibraryRow
                {
                    LibraryKey = x.Key,
                    Title      = x.Title,
                    Type       = x.Type
                }).ToList();
            }
            else
            {
                _logger.LogInformation("No Plex Libraries were found for the server");
            }

            await _plexService.AddServer(plexServer);
        }
Beispiel #5
0
        public async Task <ValidationContext> Handle(SyncLibrariesCommand request, CancellationToken cancellationToken)
        {
            var result = new ValidationContext();

            var server = await _plexService.GetServer();

            if (server == null)
            {
                result.AddError("No admin server found", "Cannot sync libraries as no admin server has been found");
                return(result);
            }

            var libraryContainer = await _plexApi.GetLibraries(server.AccessToken, server.GetPlexUri(_plexSettings.ConnectLocally));

            CheckForDeletedLibraries(server, libraryContainer);

            SetNewLibraries(libraryContainer, server);

            await _unitOfWork.CommitAsync();

            return(result);
        }
Beispiel #6
0
        private void GivenServerLibraries()
        {
            _plexLibraryContainer = new PlexMediaContainerBuilder().WithMetadata().Build();

            _plexApi.GetLibraries(Arg.Any <string>(), Arg.Any <string>()).Returns(_plexLibraryContainer);
        }