Пример #1
0
        [HttpGet("SaveSvg/{doSave:bool}")]          // GET /api/Editor/SaveSvg/false
        public IActionResult SaveSvg(bool doSave)
        {
            // traverse through all circles and set the layer whenever the radius is the same
            var drawModel = HttpContext.Session.GetObjectFromJson <DrawModel>("DrawModel");

            if (drawModel != null)
            {
                // convert to a real svg document
                var svg = DrawModel.ToSvgDocument(drawModel);

                // build new filename
                string fileName    = drawModel.FileName;
                var    newFileName = Path.GetFileNameWithoutExtension(fileName);

                // always use the svg extension since thats what we are saving
                var newFileExtension = ".svg";
                var newFullFileName  = newFileName + newFileExtension;

                if (doSave)
                {
                    var  basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
                    bool basePathExists = System.IO.Directory.Exists(basePath);
                    if (!basePathExists)
                    {
                        Directory.CreateDirectory(basePath);
                    }

                    var filePath = Path.Combine(basePath, newFullFileName);
                    svg.Write(filePath);
                }
                else
                {
                    // download converted file to user
                    var memoryStream = new MemoryStream();
                    svg.Write(memoryStream);

                    // At this point, the Offset is at the end of the MemoryStream
                    // Either do this to seek to the beginning
                    memoryStream.Position = 0;

                    // have to fix the xml / svg document since
                    // the library used creates tags
                    // that TinkerCad doesn't support
                    var fixedMemStream = FixSvgDocument(memoryStream);

                    return(File(fixedMemStream, "APPLICATION/octet-stream", newFullFileName));
                }
            }
            else
            {
                _logger.LogError("Saving Svg unsuccessfull!");
                return(BadRequest());
            }

            return(Ok());
        }