public void Setup() { this.badImageViewModel = new ImageViewModel() { Id = 1, ImagePath = null, ImageDescription = "Description", }; this.goodImageViewModel = new ImageViewModel() { Id = 1, ImagePath = new MemoryFile(), ImageDescription = "Description", }; this.controller = new ImageController(this.ImageServiceMock.Object, this.ArticleServiceMock.Object); }
public ActionResult Add(ImageViewModel model) { if (model == null || !this.ModelState.IsValid || model.ImagePath.ContentLength <= 0) { this.TempData.Add(ModelConstants.Error, ModelConstants.ModelError); return this.RedirectToAction("Index", "Image"); } string serverPath = this.HttpContext.Server.MapPath("~"); string currentFolder = DateTime.Now.Year + "_" + DateTime.Now.Month; string hddPath = serverPath + WebConstants.DirectoryUpload + "\\" + currentFolder; if (!Directory.Exists(hddPath)) { Directory.CreateDirectory(hddPath); } string fileName = Path.GetFileName(model.ImagePath.FileName); if (fileName == null) { this.TempData.Add(ModelConstants.Error, ModelConstants.FileNotFound); return this.RedirectToAction("Index", "Image"); } string fileNameSaveAs = Math.Abs(fileName.GetHashCode() ^ fileName.Length) + "-" + fileName; model.ImagePath.SaveAs(Path.Combine(hddPath, fileNameSaveAs)); string imageDbPath = "/" + WebConstants.DirectoryUpload + "/" + currentFolder + "/" + fileNameSaveAs; var image = new Image() { ImagePath = imageDbPath, ImageDescription = model.ImageDescription }; this.imageService.Add(image); return this.RedirectToAction("Index", "Image"); }