示例#1
0
        public ParatextService(IWebHostEnvironment env, IOptions <ParatextOptions> paratextOptions,
                               IRepository <UserSecret> userSecretRepository, IRealtimeService realtimeService,
                               IExceptionHandler exceptionHandler, IOptions <SiteOptions> siteOptions, IFileSystemService fileSystemService,
                               ILogger <ParatextService> logger, IJwtTokenHelper jwtTokenHelper, IParatextDataHelper paratextDataHelper,
                               IInternetSharedRepositorySourceProvider internetSharedRepositorySourceProvider,
                               ISFRestClientFactory restClientFactory)
        {
            _paratextOptions      = paratextOptions;
            _userSecretRepository = userSecretRepository;
            _realtimeService      = realtimeService;
            _exceptionHandler     = exceptionHandler;
            _siteOptions          = siteOptions;
            _fileSystemService    = fileSystemService;
            _logger             = logger;
            _jwtTokenHelper     = jwtTokenHelper;
            _paratextDataHelper = paratextDataHelper;
            _internetSharedRepositorySourceProvider = internetSharedRepositorySourceProvider;
            _restClientFactory = restClientFactory;

            _httpClientHandler = new HttpClientHandler();
            _registryClient    = new HttpClient(_httpClientHandler);
            if (env.IsDevelopment() || env.IsEnvironment("DevelopmentBeta") || env.IsEnvironment("Testing") || env.IsEnvironment("TestingBeta"))
            {
                _httpClientHandler.ServerCertificateCustomValidationCallback
                    = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
                // This should be paratext-qa.thedigitalbiblelibrary.org, but it's broken as of 2021-04 and
                // qa.thedigitalbiblelibrary.org should be just as good, at least for the time being.
                _dblServerUri               = "https://qa.thedigitalbiblelibrary.org/";
                _registryServerUri          = "https://registry-dev.paratext.org";
                _registryClient.BaseAddress = new Uri(_registryServerUri);
                _sendReceiveServerUri       = InternetAccess.uriDevelopment;
            }
            else
            {
                _registryClient.BaseAddress = new Uri(_registryServerUri);
            }
            _registryClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            ScrTextCollection = new LazyScrTextCollection();
            HgWrapper         = new HgWrapper();

            SharingLogicWrapper = new SharingLogicWrapper();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        }
示例#2
0
        /// <summary>
        /// Synchronizes the text and notes data on the SF server with the data on the Paratext server.
        /// </summary>
        public async Task SendReceiveAsync(UserSecret userSecret, string ptTargetId,
                                           IProgress <ProgressState> progress = null)
        {
            if (userSecret == null || ptTargetId == null)
            {
                throw new ArgumentNullException();
            }

            IInternetSharedRepositorySource source = await GetInternetSharedRepositorySource(userSecret.Id);

            IEnumerable <SharedRepository>       repositories        = source.GetRepositories();
            IEnumerable <ProjectMetadata>        projectsMetadata    = source.GetProjectsMetaData();
            IEnumerable <string>                 projectGuids        = projectsMetadata.Select(pmd => pmd.ProjectGuid.Id);
            Dictionary <string, ParatextProject> ptProjectsAvailable =
                GetProjects(userSecret, repositories, projectsMetadata).ToDictionary(ptProject => ptProject.ParatextId);

            if (!projectGuids.Contains(ptTargetId))
            {
                // See if this is a resource
                IReadOnlyList <ParatextResource> resources = await this.GetResourcesInternalAsync(userSecret.Id, true);

                ParatextResource resource = resources.SingleOrDefault(r => r.ParatextId == ptTargetId);
                if (resource != null)
                {
                    ptProjectsAvailable.Add(resource.ParatextId, resource);
                }
                else
                {
                    _logger.LogWarning($"The target project did not have a full name available {ptTargetId}");
                }
            }
            if (!ptProjectsAvailable.TryGetValue(ptTargetId, out ParatextProject targetPtProject))
            {
                throw new ArgumentException(
                          $"PT projects with the following PT ids were requested but without access or they don't exist: {ptTargetId}");
            }

            EnsureProjectReposExists(userSecret, targetPtProject, source);
            StartProgressReporting(progress);
            if (!(targetPtProject is ParatextResource))
            {
                SharedProject targetSharedProj = SharingLogicWrapper.CreateSharedProject(ptTargetId,
                                                                                         targetPtProject.ShortName, source.AsInternetSharedRepositorySource(), repositories);
                string username = GetParatextUsername(userSecret);
                // Specifically set the ScrText property of the SharedProject to indicate the project is available locally
                targetSharedProj.ScrText     = ScrTextCollection.FindById(username, ptTargetId);
                targetSharedProj.Permissions = targetSharedProj.ScrText.Permissions;
                List <SharedProject> sharedPtProjectsToSr = new List <SharedProject> {
                    targetSharedProj
                };

                // TODO report results
                List <SendReceiveResult> results = Enumerable.Empty <SendReceiveResult>().ToList();
                bool success  = false;
                bool noErrors = SharingLogicWrapper.HandleErrors(() => success = SharingLogicWrapper
                                                                                 .ShareChanges(sharedPtProjectsToSr, source.AsInternetSharedRepositorySource(),
                                                                                               out results, sharedPtProjectsToSr));
                if (!noErrors || !success)
                {
                    throw new InvalidOperationException(
                              "Failed: Errors occurred while performing the sync with the Paratext Server.");
                }
            }
        }