Esempio n. 1
0
        public async Task <Dictionary <string, List <string> > > GetAvailableParametersAsync(string document)
        {
            try
            {
                if (!document.Contains(JsonOpener) || !document.Contains(DocsParam))
                {
                    return(new Dictionary <string, List <string> >());
                }

                var(jsonBeginningIndex, jsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document);

                if (jsonBeginningIndex < 0 || jsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection))
                {
                    return(new Dictionary <string, List <string> >());
                }

                var pureJson = insideJsonSection.Replace(DocsParam, "").Trim();

                if (!DocsJsonSerializerHelper.TryDeserialize <Dictionary <string, List <string> > >(pureJson, out var availableParameters))
                {
                    throw new UserFriendlyException("ERROR-20200327: Cannot validate JSON content for `AvailableParameters`!");
                }

                return(await Task.FromResult(availableParameters));
            }
            catch (Exception)
            {
                Logger.LogWarning("Unable to parse parameters of document.");
                return(new Dictionary <string, List <string> >());
            }
        }
Esempio n. 2
0
        public async Task <DocumentParametersDto> GetParametersAsync(GetParametersDocumentInput input)
        {
            var project = await _projectRepository.GetAsync(input.ProjectId);

            input.Version = GetProjectVersionPrefixIfExist(project) + input.Version;

            try
            {
                if (string.IsNullOrWhiteSpace(project.ParametersDocumentName))
                {
                    return(await Task.FromResult <DocumentParametersDto>(null));
                }

                var document = await GetDocumentWithDetailsDtoAsync(
                    project,
                    project.ParametersDocumentName,
                    input.LanguageCode,
                    input.Version
                    );

                if (!DocsJsonSerializerHelper.TryDeserialize <DocumentParametersDto>(document.Content, out var documentParameters))
                {
                    throw new UserFriendlyException(
                              $"Cannot validate document parameters file '{project.ParametersDocumentName}' for the project {project.Name}.");
                }

                return(documentParameters);
            }
            catch (DocumentNotFoundException)
            {
                Logger.LogWarning($"Parameter file ({project.ParametersDocumentName}) not found!");
                return(new DocumentParametersDto());
            }
        }
Esempio n. 3
0
        public async Task PullAllAsync(PullAllDocumentInput input)
        {
            var project = await _projectRepository.GetAsync(input.ProjectId);

            var navigationDocument = await GetDocumentAsync(
                project,
                project.NavigationDocumentName,
                input.LanguageCode,
                input.Version
                );

            if (!DocsJsonSerializerHelper.TryDeserialize <NavigationNode>(navigationDocument.Content, out var navigation))
            {
                throw new UserFriendlyException($"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
            }

            var leafs = navigation.Items.GetAllNodes(x => x.Items)
                        .Where(x => x.IsLeaf && !x.Path.IsNullOrWhiteSpace())
                        .ToList();

            var source = _documentStoreFactory.Create(project.DocumentStoreType);

            var documents = new List <Document>();

            foreach (var leaf in leafs)
            {
                if (leaf.Path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
                    leaf.Path.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ||
                    (leaf.Path.StartsWith("{{") && leaf.Path.EndsWith("}}")))
                {
                    continue;
                }

                try
                {
                    var sourceDocument = await source.GetDocumentAsync(project, leaf.Path, input.LanguageCode, input.Version);

                    documents.Add(sourceDocument);
                }
                catch (Exception e)
                {
                    Logger.LogException(e);
                }
            }

            foreach (var document in documents)
            {
                await _documentRepository.DeleteAsync(document.ProjectId, document.Name,
                                                      document.LanguageCode,
                                                      document.Version);

                await _documentRepository.InsertAsync(document, true);
                await UpdateDocumentUpdateInfoCache(document);
            }
        }
Esempio n. 4
0
        public async Task <LanguageConfig> GetLanguageListAsync(Project project, string version)
        {
            var path = Path.Combine(project.GetFileSystemPath(), DocsDomainConsts.LanguageConfigFileName);
            var configJsonContent = await FileHelper.ReadAllTextAsync(path);

            if (!DocsJsonSerializerHelper.TryDeserialize <LanguageConfig>(configJsonContent, out var languageConfig))
            {
                throw new UserFriendlyException($"Cannot validate language config file '{DocsDomainConsts.LanguageConfigFileName}' for the project {project.Name}.");
            }

            return(languageConfig);
        }
Esempio n. 5
0
        public virtual async Task <NavigationNode> GetNavigationAsync(GetNavigationDocumentInput input)
        {
            var project = await _projectRepository.GetAsync(input.ProjectId);

            input.Version = GetProjectVersionPrefixIfExist(project) + input.Version;

            var navigationDocument = await GetDocumentWithDetailsDtoAsync(
                project,
                project.NavigationDocumentName,
                input.LanguageCode,
                input.Version
                );

            if (!DocsJsonSerializerHelper.TryDeserialize <NavigationNode>(navigationDocument.Content,
                                                                          out var navigationNode))
            {
                throw new UserFriendlyException(
                          $"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
            }

            var leafs = navigationNode.Items.GetAllNodes(x => x.Items)
                        .Where(x => !x.Path.IsNullOrWhiteSpace())
                        .ToList();

            foreach (var leaf in leafs)
            {
                var cacheKey =
                    CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, leaf.Path, input.LanguageCode,
                                                                         input.Version);
                var documentUpdateInfo = await DocumentUpdateCache.GetAsync(cacheKey);

                if (documentUpdateInfo != null)
                {
                    leaf.CreationTime              = documentUpdateInfo.CreationTime;
                    leaf.LastUpdatedTime           = documentUpdateInfo.LastUpdatedTime;
                    leaf.LastSignificantUpdateTime = documentUpdateInfo.LastSignificantUpdateTime;
                }
            }

            await NavigationTreePostProcessor.ProcessAsync(
                new NavigationTreePostProcessorContext(
                    navigationDocument,
                    navigationNode
                    )
                );

            return(navigationNode);
        }
Esempio n. 6
0
        public async Task <LanguageConfig> GetLanguageListAsync(Project project, string version)
        {
            var token     = project.GetGitHubAccessTokenOrNull();
            var rootUrl   = project.GetGitHubUrl(version);
            var userAgent = project.GetGithubUserAgentOrNull();

            var url = CalculateRawRootUrl(rootUrl) + DocsDomainConsts.LanguageConfigFileName;

            var configAsJson = await DownloadWebContentAsStringAsync(url, token, userAgent);

            if (!DocsJsonSerializerHelper.TryDeserialize <LanguageConfig>(configAsJson, out var languageConfig))
            {
                throw new UserFriendlyException($"Cannot validate language config file '{DocsDomainConsts.LanguageConfigFileName}' for the project {project.Name} - v{version}.");
            }

            return(languageConfig);
        }
Esempio n. 7
0
        private async Task <List <NavigationNode> > GetNavigationNodesAsync(string prefix, Project project,
                                                                            Document document)
        {
            var version            = GetProjectVersionPrefixIfExist(project) + document.Version;
            var navigationDocument = await GetDocumentWithDetailsDtoAsync(
                project,
                project.NavigationDocumentName,
                document.LanguageCode,
                version
                );

            if (!DocsJsonSerializerHelper.TryDeserialize <NavigationNode>(navigationDocument.Content,
                                                                          out var navigationNode))
            {
                throw new UserFriendlyException(
                          $"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
            }

            return(navigationNode.Items);
        }
Esempio n. 8
0
        public async Task <List <string> > GetUrlsAsync(string prefix)
        {
            var documentUrls = new List <string>();
            var projects     = await _projectRepository.GetListAsync();

            foreach (var project in projects)
            {
                var documents = await _documentRepository.GetListByProjectId(project.Id);

                foreach (var document in documents)
                {
                    var version            = GetProjectVersionPrefixIfExist(project) + document.Version;
                    var navigationDocument = await GetDocumentWithDetailsDtoAsync(
                        project,
                        project.NavigationDocumentName,
                        document.LanguageCode,
                        version
                        );

                    if (!DocsJsonSerializerHelper.TryDeserialize <NavigationNode>(navigationDocument.Content,
                                                                                  out var navigationNode))
                    {
                        throw new UserFriendlyException(
                                  $"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
                    }

                    navigationNode.Items?.ForEach(node =>
                    {
                        documentUrls.AddIfNotContains(
                            GetDocumentLinks(node, documentUrls, prefix, project.ShortName, document)
                            );
                    });
                }
            }

            return(documentUrls);
        }