コード例 #1
0
        public IActionResult Index(
            [NotNull] [ItemNotNull] IEnumerable<IFormFile> files,
            [CanBeNull] string format,
            [CanBeNull] string title,
            [CanBeNull] string publisher,
            [CanBeNull] string website,
            [CanBeNull] string stylesheetUrl,
            [CanBeNull] IFormFile stylesheet)
        {
            if (files is null)
                throw new ArgumentNullException(nameof(files));

            IFormFile[] uploadedFiles = files.ToArray();

            if (uploadedFiles.Length == 0)
                return BadRequest("No files uploaded.");

            if (uploadedFiles.Any(x => x.Length <= 0))
                return BadRequest("Invalid file length.");

            if (uploadedFiles.Any(x => x.ContentType != MicrosoftWordDocument.ToString() &&
                                       x.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase) &&
                                       x.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase)))
                return BadRequest("Invalid file format.");

            if (stylesheet is IFormFile s && !s.FileName.EndsWith(".css"))
                return BadRequest($"Invalid stylesheet:{stylesheet.FileName}.");

            Queue<Package> packagesQueue = new Queue<Package>(uploadedFiles.Length);

            foreach (IFormFile file in uploadedFiles)
            {
                if (file.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase))
                {
                    packagesQueue.Enqueue(Package.Open(file.OpenReadStream()));
                }
                else if (file.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
                {
                    MDocument mDocument = new MDocument();
                    using (StreamReader reader = new StreamReader(file.OpenReadStream()))
                    {
                        MarkdownVisitor visitor = new MarkdownVisitor();
                        ReadOnlySpan<char> span;
                        while ((span = reader.ReadLine()) != null)
                        {
                            mDocument.Append(visitor.Visit(in span));
                        }
                    }

                    Package package = DocxFilePath.Create().ToPackage(FileAccess.ReadWrite);

                    mDocument.ToOpenXml().WriteTo(package.GetPart(Document.PartUri));

                    packagesQueue.Enqueue(package);
                }
            }

            Package output =
                Process(
                    packagesQueue,
                    title ?? "[REPORT TITLE]",
                    publisher ?? "[PUBLISHER]",
                    website ?? "[PUBLISHER WEBSITE]");

            foreach (Package package in packagesQueue)
            {
                package.Close();
            }

            switch (format)
            {
                case "docx":
                {
                    return File(output.ToStream(), MicrosoftWordDocument, $"{title ?? "result"}.docx");
                }
                case "html":
                {
                    string styles = stylesheet is null ? null : new StreamReader(stylesheet.OpenReadStream()).ReadToEnd();
                    OpenXmlPackageVisitor ooxml = new OpenXmlPackageVisitor(output);
                    HtmlVisitor html = new HtmlVisitor(ooxml.Document.ChartReferences, ooxml.Document.ImageReferences);
                    XObject htmlResult = html.Visit(ooxml.Document.Content, ooxml.Footnotes.Content, title, stylesheetUrl, styles);
                    return File(Encoding.UTF8.GetBytes(htmlResult.ToString()), "text/html", $"{title ?? "result"}.html");
                }
                case "xml":
                {
                    OpenXmlPackageVisitor xml = new OpenXmlPackageVisitor(output);
                    XElement xmlResult = xml.Document.Content;
                    return File(Encoding.UTF8.GetBytes(xmlResult.ToString()), "application/xml", $"{title ?? "result"}.xml");
                }
                default:
                {
                    return BadRequest(ModelState);
                }
            }
        }