public async Task <Image> Add(ImageAddModel imageAddModel) { string zipName = $"{Guid.NewGuid().ToString("N")}.zip"; string imageContentZipPath = _environment.BuildPath(_pathOptions.ImageZipFileDirectory, zipName); using (var file = File.Create(imageContentZipPath)) { var stream = imageAddModel.ImageContents.First().OpenReadStream(); stream.Seek(0, SeekOrigin.Begin); await stream.CopyToAsync(file); } var image = new Image { Name = imageAddModel.DisplayName, ImageContentZipPath = zipName, Type = imageAddModel.Type, SupportsMods = !string.IsNullOrEmpty(imageAddModel.ModDir), ModDirectory = imageAddModel.ModDir ?? "", BuildStatus = null }; await _context.Images.AddAsync(image); await _context.SaveChangesAsync(); return(image); }
public static ImageAddCommand ToCommand(this ImageAddModel model) { if (model == null) { return(null); } return(new ImageAddCommand(SystemDefine.DefaultVersion) { Extentsion = model.Extension, FileName = model.FileName, FilePath = model.FilePath, CreatedUid = model.CreatedUid, }); }
public async Task <IActionResult> Add(ImageAddModel inputModel) { if (inputModel == null || !ModelState.IsValid || inputModel.ImageContents.Count != 1) { return(BadRequest(ModelState)); } _logger.LogInformation($"Adding new image to database. Name: '{inputModel.DisplayName}'"); var image = await _imageRepository.Add(inputModel); _logger.LogInformation($"Image was added. Name: '{inputModel.DisplayName}'"); return(Ok(image)); }
public async Task <ImageAddResponse> ImageAdd(ImageAddModel model) { ImageAddResponse response = new ImageAddResponse(); try { var command = model.ToCommand(); await _fileService.ImageAdd(command); response.SetSucess(); } catch (Exception e) { response.SetFail(e); _logger.LogError(e, e.Message, model); } response.SetSucess(); return(response); }
public async Task<IActionResult> Add(ImageAddModel model) { _logger.LogInformation("Validating "); if (ModelState.IsValid) { _logger.LogInformation("Adding image"); var result = await _imageService.AddImageAsync(model); if (result.IsSuccessful) { _logger.LogInformation("Image added"); return RedirectToAction("Index", "Images"); } _logger.LogInformation("Error adding image"); ModelState.AddModelError("Image", String.Join(", ", result.ErrorMessages.ToArray())); } return View(model); }
/// <summary> /// Add an image /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <(ImageInfoModel Data, Boolean IsSuccessful, IList <String> ErrorMessages)> AddImageAsync(ImageAddModel model) { try { if (!model.Validate <ImageAddModelValidator, ImageAddModel>().IsValid) { var validations = model.Validate <ImageAddModelValidator, ImageAddModel>(); return(null, false, validations.ToList()); } var image = _mapper.Map <ImageAddModel, Image>(model); image.Content = model.Image.ToByteArray(); image.FileExtension = Path.GetExtension(model.Image.FileName); image.FileName = Path.GetFileNameWithoutExtension(model.Image.FileName); image.FileSize = model.Image.Length; image.ContentType = model.Image.ContentType; await _unitOfWork.ImageRepository.AddImageAsync(image); await _unitOfWork.CompleteAsync(); var entityMapped = _mapper.Map <Image, ImageInfoModel>(image); return(entityMapped, true, null); } catch (Exception ex) { return(null, false, new List <String>() { ex.Message }); } }