Пример #1
0
        /// <inheritdoc/>
        public async Task <Result <bool> > RefreshLibrariesAsync(PlexAccount plexAccount, PlexServer plexServer)
        {
            if (plexServer == null)
            {
                string msg = "plexServer was null";
                Log.Warning(msg);
                return(Result.Fail(msg));
            }

            Log.Debug($"Refreshing PlexLibraries for plexServer: {plexServer.Name}");

            var authToken = await _plexAuthenticationService.GetPlexServerTokenAsync(plexServer.Id, plexAccount.Id);

            if (authToken.IsFailed)
            {
                return(Result.Fail(new Error("Failed to retrieve the server auth token")));
            }

            var libraries = await _plexServiceApi.GetLibrarySectionsAsync(authToken.Value, plexServer.ServerUrl);

            if (!libraries.Any())
            {
                string msg = $"plexLibraries returned for server {plexServer.Name} - {plexServer.ServerUrl} was empty";
                Log.Warning(msg);
                return(Result.Fail(msg));
            }

            return(await _mediator.Send(new AddOrUpdatePlexLibrariesCommand(plexAccount, plexServer, libraries)));
        }
Пример #2
0
        private async Task <Result <DownloadTask> > AuthenticateDownloadTask(DownloadTask downloadTask)
        {
            var tokenResult = await _plexAuthenticationService.GetPlexServerTokenAsync(downloadTask.PlexServerId);

            if (tokenResult.IsFailed)
            {
                return(tokenResult.ToResult());
            }

            downloadTask.ServerToken = tokenResult.Value;
            downloadTask.ServerToken = tokenResult.Value;

            return(Result.Ok(downloadTask));
        }
Пример #3
0
        private async Task <Result <bool> > FinalizeDownloadTasks(List <DownloadTask> downloadTasks, int plexAccountId = 0)
        {
            if (!downloadTasks.Any())
            {
                return(ResultExtensions.IsEmpty(nameof(downloadTasks)).LogWarning());
            }

            // Get the download folder
            var downloadFolder = await _folderPathService.GetDownloadFolderAsync();

            if (downloadFolder.IsFailed)
            {
                return(downloadFolder.ToResult());
            }

            // Get the destination folder
            var destinationFolder = await _folderPathService.GetDestinationFolderByMediaType(downloadTasks.First().MediaType);

            if (destinationFolder.IsFailed)
            {
                return(destinationFolder.ToResult());
            }

            // Get plex server access token
            var serverToken = await _plexAuthenticationService.GetPlexServerTokenAsync(downloadTasks.First().PlexServerId, plexAccountId);

            if (serverToken.IsFailed)
            {
                return(serverToken.ToResult());
            }

            foreach (var downloadTask in downloadTasks)
            {
                downloadTask.DownloadFolderId    = downloadFolder.Value.Id;
                downloadTask.DownloadFolder      = downloadFolder.Value;
                downloadTask.DestinationFolderId = destinationFolder.Value.Id;
                downloadTask.DestinationFolder   = destinationFolder.Value;
                downloadTask.ServerToken         = serverToken.Value;
            }

            return(await _downloadManager.AddToDownloadQueueAsync(downloadTasks));
        }
Пример #4
0
        public async Task <Result <PlexServerStatus> > CheckPlexServerStatusAsync(PlexServer plexServer, int plexAccountId = 0, bool trimEntries = true)
        {
            // Get plexServer authToken
            var authToken = await _plexAuthenticationService.GetPlexServerTokenAsync(plexServer.Id, plexAccountId);

            if (authToken.IsFailed)
            {
                return(authToken.ToResult());
            }

            // Request status
            var serverStatus = await _plexServiceApi.GetPlexServerStatusAsync(authToken.Value, plexServer.ServerUrl);

            serverStatus.PlexServer   = plexServer;
            serverStatus.PlexServerId = plexServer.Id;

            // Add plexServer status to DB, the PlexServerStatus table functions as a server log.
            var result = await _mediator.Send(new CreatePlexServerStatusCommand(serverStatus));

            if (result.IsFailed)
            {
                return(result.ToResult());
            }

            if (trimEntries)
            {
                // Ensure that there are not too many PlexServerStatuses stored.
                var trimResult = await _mediator.Send(new TrimPlexServerStatusCommand(plexServer.Id));

                if (trimResult.IsFailed)
                {
                    return(trimResult.ToResult());
                }
            }

            return(await _mediator.Send(new GetPlexServerStatusByIdQuery(result.Value)));
        }