public async Task <IActionResult> AddOrderEditsAsync(string id, [FromForm] OrderEditFormDTO item) { id = _idHelper.CutFirstTwoNumbers(id); item.Images = Request.Form.Files; if (!_orderSrv.IsOrderExistById(id)) { return(NotFound("Order not found")); } var result = await _orderSrv.EditOrder(id, item); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public async Task <OperationResult> EditOrder(string id, OrderEditFormDTO item) { var dir = GetOrderDirectory(id); var infoData = ParseInfoTxt(dir); var valuesDict = RefactorInfoData(infoData); try { var filePath = ""; var zipPath = ""; var comments = item.Comments ?? ""; if (item.Images.Count() > 0) { var targetFilename = valuesDict["maket"] .TrimEnd(GetModelFileExtension(valuesDict["maket"]).ToCharArray()); zipPath = dir + $"\\c_{targetFilename}zip"; using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create)) { } foreach (var image in item.Images) { var fileExtension = GetModelFileExtension(image.FileName); filePath = dir + $"\\c_{targetFilename}{fileExtension}"; using (var stream = new FileStream(filePath, FileMode.Create)) { await image.CopyToAsync(stream); } using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) { archive.CreateEntryFromFile(filePath, image.FileName); } } if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } } var dirs = System.IO.Directory.GetDirectories(_fileHost); var catalogWithMarks = _cfg.GetValue <string>("CatalogWithMarks"); var dirCatalogWithMarks = dirs.FirstOrDefault(x => catalogWithMarks.Equals(new String(x.Skip(_fileHost.Length + 1).ToArray()))); if (dirCatalogWithMarks == null) { dirCatalogWithMarks = $"{_fileHost}\\{catalogWithMarks}"; System.IO.Directory.CreateDirectory(dirCatalogWithMarks); } var dateTimeStamp = DateTime.UtcNow.ToString("yyyyMMdd_HH_mm_ss"); using (TextWriter fileTW = File.CreateText($"{dirCatalogWithMarks}\\{dateTimeStamp}.sca")) { fileTW.NewLine = "\n"; fileTW.WriteLine(valuesDict["name"]); } var content = new[] { "Ответ = " + Responses.GetResponse(item.Status), "Файл = " + zipPath, "Комментарий = " + comments.Replace("\n", "; ") }; using (TextWriter fileTW = new StreamWriter($"{dir}\\{dateTimeStamp}.sca")) { fileTW.NewLine = "\n"; foreach (string s in content) { fileTW.WriteLine(s); } } using (TextWriter fileTW = new StreamWriter($"{dir}\\ok.txt")) { fileTW.NewLine = "\n"; fileTW.WriteLine(); } } catch (Exception ex) { return(new OperationResult() { Messages = new[] { $"ERROR: {ex.Message}" } }); } return(new OperationResult() { Data = GetInfoDTO(valuesDict, dir), Success = true, Messages = new[] { "Edits was sended successfully" } }); }