[HttpPost("Upload")] // POST /api/Editor/Upload public async Task <IActionResult> Upload(List <IFormFile> files, [FromForm] string description, [FromForm] bool useContours) { DrawModel drawModel = null; foreach (var file in files) { // Read file fully and save to file system // var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\"); // bool basePathExists = System.IO.Directory.Exists(basePath); // if (!basePathExists) Directory.CreateDirectory(basePath); // var fileName = Path.GetFileNameWithoutExtension(file.FileName); // var filePath = Path.Combine(basePath, file.FileName); // var extension = Path.GetExtension(file.FileName); // if (!System.IO.File.Exists(filePath)) // { // using (var stream = new FileStream(filePath, FileMode.Create)) // { // await file.CopyToAsync(stream); // } // } // Read file fully into memory and act var hasParsedSuccessfully = false; using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); _logger.LogInformation("Successfully uploaded file!"); // At this point, the Offset is at the end of the MemoryStream // Do this to seek to the beginning memoryStream.Seek(0, SeekOrigin.Begin); // try to parse as dxf if (!hasParsedSuccessfully) { try { _logger.LogInformation("Trying to parse file as DXF..."); var dxf = DxfDocument.Load(memoryStream); if (dxf != null) { _logger.LogInformation("DXF read successfully!"); // convert dxf to a model that can be serialized // since I am unable to get the default dxfnet model to be serialized drawModel = DrawModel.FromDxfDocument(dxf, file.FileName, useContours); _logger.LogInformation("Successfully parsed DXF to draw model!"); hasParsedSuccessfully = true; } } catch (System.Exception e) { _logger.LogError(e, "Failed parsing as DXF!"); // ignore } } // At this point, the Offset is at the end of the MemoryStream // Do this to seek to the beginning memoryStream.Seek(0, SeekOrigin.Begin); // try to parse as SVG if (!hasParsedSuccessfully) { try { _logger.LogInformation("Trying to parse file as SVG..."); var svg = SVGDocument.Load(memoryStream); if (svg != null) { _logger.LogInformation("SVG read successfully!"); drawModel = DrawModel.FromSVGDocument(svg, file.FileName, useContours); _logger.LogInformation("Successfully parsed SVG to draw model!"); hasParsedSuccessfully = true; } } catch (System.Exception e) { _logger.LogError(e, "Failed parsing as SVG!"); // ignore } } // At this point, the Offset is at the end of the MemoryStream // Do this to seek to the beginning memoryStream.Seek(0, SeekOrigin.Begin); // try to parse as GCODE if (!hasParsedSuccessfully) { try { _logger.LogInformation("Trying to parse file as GCODE..."); StreamReader reader = new StreamReader(memoryStream); string gCode = reader.ReadToEnd(); if (!"".Equals(gCode)) { _logger.LogInformation("GCODE read successfully!"); drawModel = DrawModel.FromGCode(gCode, file.FileName); _logger.LogInformation("Successfully parsed GCODE to draw model!"); hasParsedSuccessfully = true; } } catch (System.Exception e) { _logger.LogError(e, "Failed parsing as GCODE!"); // ignore } } if (drawModel != null) { HttpContext.Session.SetObjectAsJson("DrawModel", drawModel); } } } if (HttpContext.Session.Keys.Contains("DrawModel")) { _logger.LogInformation("File successfully uploaded!"); } else { _logger.LogError("File upload failed!"); return(BadRequest()); } return(Ok()); }