public IActionResult GenerateDocument([Bind("DocId, InputText, OutputType")] SvgDocumentForm svgDocument)
        {
            Dictionary <string, string[]> jsonInput;
            int documentId = svgDocument.DocId;

            try
            {
                jsonInput = DataExtractor.GetJsonData(svgDocument.InputText);
            }
            catch (Exception e)
            {
                var error = e.Message;
                return(Content(error));
            }

            var document = _context.SvgDocuments.Find(documentId);

            string path = _hostingEnvironment.WebRootPath + document.DocumentPath;

            int            max         = jsonInput.First().Value.Length;
            int            index       = 0;
            List <ZipItem> outputFiles = new List <ZipItem>();


            foreach (var item in jsonInput)
            {
                int currentLength = item.Value.Length;
                if (currentLength != max)
                {
                    var error = "Value array error in column: " + item.Key + ". Expected length: " + max
                                + ", current length: " + currentLength;
                    return(Content(error));
                }
            }

            for (int i = 0; i < max; i++)
            {
                SvgDocumentEditModel model = new SvgDocumentEditModel()
                {
                    TemplateDocumentPath    = path,
                    ElementName             = "text",
                    Attribute               = "id",
                    ValueIndex              = index,
                    ElementsForSubstitution = jsonInput
                };
                index++;

                MemoryStream readySvgDocument = SvgEditor.GenerateSvgDocument(model);
                //MemoryStream jpegDocument = SvgEditor.SvgToJpeg(readySvgDocument);
                ZipItem zipItem = new ZipItem("Document" + index + ".svg", readySvgDocument);
                outputFiles.Add(zipItem);
                //jpegDocument.Close();
                //readySvgDocument.Close();
            }

            var resultZip = Zipper.Zip(outputFiles);

            return(File(resultZip, "application/octet-stream", "Documents.zip"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("ID, DocumentName, Description")] SvgDocument svgDocument, IFormFile file)
        {
            if (file != null)
            {
                string extension = Path.GetExtension(file.FileName);
                if (extension.ToLower() != ".svg")
                {
                    throw new FormatException("Invalid file extension!");
                }
                string localPath  = "/documents/" + file.FileName;
                string globalPath = _hostingEnvironment.WebRootPath + localPath;
                svgDocument.DocumentPath = localPath;

                if (ModelState.IsValid)
                {
                    _context.Add(svgDocument);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    return(View(svgDocument));
                }

                using (FileStream fs = new FileStream(globalPath, FileMode.Create))
                {
                    await file.CopyToAsync(fs);
                }

                XmlDocument doc = new XmlDocument();
                doc.Load(globalPath);
                var editableElements = SvgEditor.FindEditableElements(doc, "text", "id");

                for (int i = 0; i < editableElements.Count; i++)
                {
                    SvgElement element = new SvgElement()
                    {
                        DocumentId         = svgDocument.ID,
                        AttributeName      = editableElements[i].Attributes["id"].Value,
                        AttributeInnerText = editableElements[i].InnerText,
                        IsActive           = true
                    };
                    _context.SvgElements.Add(element);
                }
                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(svgDocument));
        }