Beispiel #1
0
        private async Task GetNewMedia(IApiClient apiClient,
                                       ServerInfo server,
                                       IProgress <double> progress,
                                       CancellationToken cancellationToken)
        {
            var jobItems = await apiClient.GetReadySyncItems(apiClient.DeviceId).ConfigureAwait(false);

            var    numComplete     = 0;
            double startingPercent = 0;
            double percentPerItem  = 1;

            if (jobItems.Count > 0)
            {
                percentPerItem /= jobItems.Count;
            }

            foreach (var jobItem in jobItems)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var currentPercent = startingPercent;
                var innerProgress  = new DoubleProgress();
                innerProgress.RegisterAction(pct =>
                {
                    var totalProgress = pct * percentPerItem;
                    totalProgress    += currentPercent;
                    progress.Report(totalProgress);
                });

                try
                {
                    await GetItem(apiClient, server, jobItem, innerProgress, cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error syncing new media", ex);
                }

                numComplete++;
                startingPercent  = numComplete;
                startingPercent /= jobItems.Count;
                startingPercent *= 100;
                progress.Report(startingPercent);
            }
        }
Beispiel #2
0
        private async Task SyncInternal(ServerInfo server, IApiClient apiClient, bool enableCameraUpload, IProgress <double> progress, CancellationToken cancellationToken)
        {
            const double cameraUploadTotalPercentage = .25;

            var uploadProgress = new DoubleProgress();

            uploadProgress.RegisterAction(p => progress.Report(p * cameraUploadTotalPercentage));

            if (enableCameraUpload)
            {
                await new ContentUploader(apiClient, _logger)
                .UploadImages(uploadProgress, cancellationToken).ConfigureAwait(false);
            }

            var syncProgress = new DoubleProgress();

            syncProgress.RegisterAction(p => progress.Report((cameraUploadTotalPercentage * 100) + (p * (1 - cameraUploadTotalPercentage))));

            await new MediaSync(_localAssetManager, _logger, _fileTransferManager)
            .Sync(apiClient, server, uploadProgress, cancellationToken).ConfigureAwait(false);
        }
Beispiel #3
0
        private async Task GetItem(IApiClient apiClient,
                                   ServerInfo server,
                                   SyncedItem jobItem,
                                   IProgress <double> progress,
                                   CancellationToken cancellationToken)
        {
            var libraryItem = jobItem.Item;

            var localItem = _localAssetManager.CreateLocalItem(libraryItem, server, jobItem.SyncJobItemId, jobItem.OriginalFileName);

            // Create db record
            await _localAssetManager.AddOrUpdate(localItem).ConfigureAwait(false);

            var fileTransferProgress = new DoubleProgress();

            fileTransferProgress.RegisterAction(pct => progress.Report(pct * .92));

            // Download item file
            await _fileTransferManager.GetItemFileAsync(apiClient, server, localItem, jobItem.SyncJobItemId, fileTransferProgress, cancellationToken).ConfigureAwait(false);

            progress.Report(92);

            // Download images
            await GetItemImages(apiClient, localItem, cancellationToken).ConfigureAwait(false);

            progress.Report(95);

            // Download subtitles
            await GetItemSubtitles(apiClient, jobItem, localItem, cancellationToken).ConfigureAwait(false);

            progress.Report(99);

            // Let the server know it was successfully downloaded
            await apiClient.ReportSyncJobItemTransferred(jobItem.SyncJobItemId).ConfigureAwait(false);

            progress.Report(100);
        }
        private async Task Sync(List <ServerInfo> servers, List <string> cameraUploadServers, bool syncOnlyOnLocalNetwork, IProgress <double> progress, CancellationToken cancellationToken = default(CancellationToken))
        {
            var    numComplete      = 0;
            double startingPercent  = 0;
            double percentPerServer = 1;

            if (servers.Count > 0)
            {
                percentPerServer /= servers.Count;
            }

            _logger.Debug("Beginning MultiServerSync with {0} servers", servers.Count);

            foreach (var server in servers)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var currentPercent = startingPercent;
                var serverProgress = new DoubleProgress();
                serverProgress.RegisterAction(pct =>
                {
                    var totalProgress = pct * percentPerServer;
                    totalProgress    += currentPercent;
                    progress.Report(totalProgress);
                });

                // Grab the latest info from the connection manager about that server
                var serverInfo = await _connectionManager.GetServerInfo(server.Id).ConfigureAwait(false);

                if (serverInfo == null)
                {
                    serverInfo = server;
                }

                if (syncOnlyOnLocalNetwork)
                {
                    var result = await _connectionManager.Connect(server, new ConnectionOptions
                    {
                        EnableWebSocket        = false,
                        ReportCapabilities     = false,
                        UpdateDateLastAccessed = false
                    }, cancellationToken).ConfigureAwait(false);

                    var apiClient = result.ApiClient;

                    var endpointInfo = await apiClient.GetEndPointInfo(cancellationToken).ConfigureAwait(false);

                    _logger.Debug("Server: {0}, Id: {1}, IsInNetwork:{2}", server.Name, server.Id, endpointInfo.IsInNetwork);

                    if (!endpointInfo.IsInNetwork)
                    {
                        continue;
                    }
                }

                var enableCameraUpload = cameraUploadServers.Contains(serverInfo.Id, StringComparer.OrdinalIgnoreCase);

                await new ServerSync(_connectionManager, _logger, _localAssetManager, _fileTransferManager, _connectionManager.ClientCapabilities)
                .Sync(serverInfo, enableCameraUpload, serverProgress, cancellationToken).ConfigureAwait(false);

                numComplete++;
                startingPercent  = numComplete;
                startingPercent /= servers.Count;
                startingPercent *= 100;
                progress.Report(startingPercent);
            }

            progress.Report(100);
        }