public async Task <IResult> Insert(CategoryViewModel categoryView, IFormFile img) { var result = new Result { Operation = Operation.Create, Status = Status.Success }; try { var categoryCheck = _categoryRepository.CheckCategory(categoryView.CategoryName, categoryView.CategoryId); if (!categoryCheck) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; result.Message = "This category already exists."; return(result); } CategoryModel category = new CategoryModel(); category.MapFromViewModel(categoryView); category.CreatedBy = _specificClaim.GetSpecificClaim("Id"); category.CreatedDate = DateTime.Now; if (!category.ParentCategory) { category.ChildCategory = categoryView.ChildCategory; } if (img != null) { Images image = new Images(); ImageExtention imageExtention = new ImageExtention(); image.ImageName = img.FileName; image.ImageContent = imageExtention.Image(img); image.ImageExtenstion = Path.GetExtension(img.FileName); category.Images = image; } var insertCategory = await _categoryRepository.InsertCategory(category); return(insertCategory); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public async Task <IResult> Update(UserViewModel viewModel, IFormFile img) { var result = new Result() { Operation = Operation.Update, Status = Status.Success }; try { UserModel userModel = new UserModel(); userModel.MapFromViewModel(viewModel); if (img != null) { ImageExtention imageExtention = new ImageExtention(); userModel.ImageContent = imageExtention.Image(img); } var updateUser = await _userRepository.Update(userModel); if (updateUser.Status != Status.Success) { return(updateUser); } userModel = updateUser.Body; UserViewModel userView = new UserViewModel(); userView.MapFromModel(userModel); result.Body = userView; result.Status = Status.Success; result.StatusCode = HttpStatusCode.OK; return(result); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public async Task <IResult> AddProductImages(int id, List <IFormFile> img) { var result = new Result { Operation = Operation.Create, Status = Status.Success }; try { var productObj = _productRepository.GetProductDetail(id); ProductModel productModel = new ProductModel(); productModel.MapFromViewModel(productObj); productModel.ModifiedBy = _specificClaim.GetSpecificClaim("Id"); productModel.ModifiedDate = DateTime.Now; ImageExtention imageExtention = new ImageExtention(); var images = new List <ProductImage>(); foreach (IFormFile i in img) { ProductImage productimage = new ProductImage(); productimage.ImageName = i.FileName; productimage.ImageContent = imageExtention.Image(i); productimage.ImageExtenstion = Path.GetExtension(i.FileName); productimage.ProductId = productModel.ProductId; images.Add(productimage); } var addImages = await _productRepository.AddProductImages(productModel, images); return(addImages); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public ActionResult RemoveBackground(HttpPostedFileBase file) { var client = new Baidu.Aip.BodyAnalysis.Body(apiKey, secretKey); var options = new Dictionary <string, object> { { "type", "foreground" } }; var result = client.BodySeg(file.InputStream.ToBytes(), options); var imageBase64 = result["foreground"].ToString(); byte[] imageBytes = imageBase64.Base64StrToBuffer(); //剪切背景图 MemoryStream imageBkStream = new MemoryStream(); imageBkStream.Write(imageBytes, 0, imageBytes.Length); Bitmap img = new Bitmap(imageBkStream); Dictionary <int, List <int> > imgs = new Dictionary <int, List <int> >(); for (var x = 0; x < img.Width; x++) { for (var y = 0; y < img.Height; y++) { var p = img.GetPixel(x, y); if (p.A > 0) { if (imgs.ContainsKey(x)) { imgs[x].Add(y); } else { imgs.Add(x, new List <int>()); } } } } int minX = img.Width, minY = img.Height; int maxX = 0, maxY = 0; foreach (var kv in imgs) { if (kv.Key < minX) { minX = kv.Key; } if (kv.Key > maxX) { maxX = kv.Key; } if (kv.Value.Min() < minY) { minY = kv.Value.Min(); } if (kv.Value.Max() > maxY) { maxY = kv.Value.Max(); } } int realWidth = maxX - minX; int realHeight = maxY - minY; Stream newImageBkStream = ImageExtention.GenerateThumbnail(imageBkStream, ImageModelEnum.cut, minX, minY, ref realWidth, ref realHeight); //保存磁盘 string newpath = AppDomain.CurrentDomain.BaseDirectory + "images\\process_photo\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; string fileName = ObjectId.GenerateNewId().ToString() + ".png"; if (!Directory.Exists(newpath)) { Directory.CreateDirectory(newpath); } using (FileStream fileStream = new FileStream(newpath + fileName, FileMode.Create, FileAccess.Write)) { newImageBkStream.CopyTo(fileStream); } return(Json(new { path = "process_photo-" + DateTime.Now.ToString("yyyyMMdd") + "-" + fileName, width = realWidth, height = realHeight })); }
public async Task <IResult> CreateUser(UserViewModel viewModel, IFormFile img, bool fromAdmin) { var result = new Result { Operation = Operation.Create, Status = Status.Success }; try { if (viewModel == null) { result.Message = "noUser"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } if (img == null) { result.Message = "noUserImage"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } var user = new UserModel(); ImageExtention imageExtention = new ImageExtention(); user.MapFromViewModel(viewModel); user.ImageContent = imageExtention.Image(img); if (user.Password == null) { user.Password = Guid.NewGuid().ToString().Replace("-", ""); } var userRoles = new List <UserRolesModel>(); foreach (var role in viewModel.Roles) { UserRolesModel rolesModel = new UserRolesModel(); rolesModel.RoleId = role; userRoles.Add(rolesModel); } user.Roles = userRoles; var newUser = await _userRepository.CreateUser(user); if (newUser.Status == Status.Success) { if (fromAdmin) { var mailLink = await _loginService.MailPasswordLink(newUser.Body); return(mailLink); } result.Status = Status.Success; result.StatusCode = HttpStatusCode.OK; result.Body = "success"; return(result); } return(newUser); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public async Task <IResult> Update(CategoryViewModel categoryView, IFormFile img) { var result = new Result { Operation = Operation.Update, Status = Status.Success }; try { var categoryCheck = Detail(categoryView.CategoryId); if (categoryCheck.Status != Status.Success) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; result.Message = "This category already exists."; return(result); } CategoryViewModel viewModel = new CategoryViewModel(); viewModel = categoryCheck.Body; var categoryDetail = _categoryRepository.GetCategory(categoryView.CategoryId); categoryDetail.MapFromViewModel(categoryView); categoryDetail.CreatedBy = viewModel.CreatedBy; categoryDetail.CreatedDate = viewModel.CreatedDate; categoryDetail.ModifiedBy = _specificClaim.GetSpecificClaim("Id"); categoryDetail.ModifiedDate = DateTime.Now; if (categoryView.ImageId == null) { if (categoryDetail.ImageId != null) { var deleteImage = await _categoryRepository.EditCategoryImage(categoryDetail.CategoryId, categoryDetail.ImageId, Operation.Delete); categoryDetail.Images = null; categoryDetail.ImageId = null; } } if (!categoryView.ParentCategory) { categoryDetail.ChildCategory = categoryView.ChildCategory; } else { categoryDetail.ChildCategory = null; } if (img != null) { ImageExtention imageExtention = new ImageExtention(); if (categoryDetail == null) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; result.Message = "Category does not exist."; return(result); } if (categoryDetail.Images == null) { Images image = new Images(); image.ImageName = img.FileName; image.ImageContent = imageExtention.Image(img); image.ImageExtenstion = Path.GetExtension(img.FileName); categoryDetail.Images = image; } } var updateCategory = await _categoryRepository.UpdateCategory(categoryDetail); return(updateCategory); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }