private IEnumerable <IDocumentationDocument> CopyDocuments <TDocumentationDocument>(IEnumerable <RemoteDocumentationDocument> sourceDocumentationDocuments, IServiceProvider serviceProvider) where TDocumentationDocument : IDocumentationDocument, new()
        {
            List <IDocumentationDocument> finalDocumentationDocuments = new List <IDocumentationDocument>();

            foreach (var item in sourceDocumentationDocuments)
            {
                IDocumentationDocument doc = CreateRemoteDocument <TDocumentationDocument>(item, serviceProvider);
                finalDocumentationDocuments.Add(doc);
            }

            return(finalDocumentationDocuments);
        }
Esempio n. 2
0
        public async Task <IActionResult> DownloadFile(string path1, string path2, string path3, string path4, string path5, string path6, string path7, string path8, string ext)
        {
            var    paths = new[] { path1, path2, path3, path4, path5, path6, path7, path8 }.Where(w => !string.IsNullOrWhiteSpace(w)).ToArray();
            string fullFilename     = string.Join("/", paths) + "." + ext;
            var    isDefaultProject = await _DocumentationService.DocumentationIndex.GetProjectFor(paths, out IDocumentationProject currentProject, out string[] documentPath);

            IDocumentationDocument document = await currentProject.GetDocumentFor(documentPath, ext);

            string path;

            if (document != null)
            {
                path = document.Path;
            }
            else
            {
                path = Path.Combine(_Options.GetDocumentationFolderAsAbsolute(_HostEnvironment.ContentRootPath).FullName, fullFilename);
            }

            var found = System.IO.File.Exists(path);

            if (!found && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                found = TryFindFileCaseInsensitive();
            }

            if (!found)
            {
                return(NotFound($"Not found: {fullFilename}"));
            }

            var fileStream = new FileStream(path, FileMode.Open);

            var    provider = new FileExtensionContentTypeProvider();
            string contentType;
            bool   forceDownload = false;

            if (!provider.TryGetContentType(fullFilename, out contentType))
            {
                contentType   = "application/octet-stream";
                forceDownload = true;
            }

            var result = new FileStreamResult(fileStream, contentType);

            // If the name is specified, the browser will automatically try to download the file. Sometimes.
            if (forceDownload)
            {
                result.FileDownloadName = fullFilename;
            }
            return(result);

            bool TryFindFileCaseInsensitive()
            {
                _Logger.LogInformation($"Searching for file {string.Join("/", paths)}.{ext} with insensitive case.");

                var dirInfo        = new DirectoryInfo(_Options.GetDocumentationFolderAsAbsolute(_HostEnvironment.ContentRootPath).FullName);
                var dirEnumOptions = new EnumerationOptions {
                    MatchCasing = MatchCasing.CaseInsensitive
                };

                // Manually scan the path to the file due to case sensitivity.
                for (int i = 0; i < paths.Length - 1; i++)
                {
                    var subDirInfo = dirInfo.GetDirectories(paths[i], dirEnumOptions);
                    _Logger.LogDebug($"Found {subDirInfo.Length} matching directory for path {dirInfo.FullName}/{paths[i]}.");

                    if (subDirInfo.Length == 0)
                    {
                        return(false);
                    }

                    dirInfo = subDirInfo.First();
                }
                var fileInfos = dirInfo.GetFiles($"{paths[^1]}.{ext}", dirEnumOptions);