/// <summary> /// Method for adding images into Property (house,hotel..) /// Directly into its navigation property /// </summary> /// <param name="imagesToAdd"></param> /// <param name="isForSlider">determines the image type to resize to</param> public IList <PropertyImages> CreatePropertyImagesSet(IEnumerable <HttpPostedFileBase> imagesToAdd, string userName, bool isForSlider = false) { var propertyImages = imagesToAdd as HttpPostedFileBase[] ?? imagesToAdd.ToArray(); var imagesForFileSystem = propertyImages .Select(i => new ImageFileSystemDTO { ImageRelPath = PathManager.CreateUserPropertyImagePath(userName, i.FileName, isForSlider: isForSlider), ImageFile = i }) .ToList(); SaveToFileSystem(imagesForFileSystem, isForSlider); var images = propertyImages .Select(p => new PropertyImages { ImagePath = PathManager.CreateUserPropertyImagePath(userName, p.FileName), ImageType = p.ContentType, ImageRatio = isForSlider ? (float)2200 / 800 : (float)870 / 580, }) .ToList(); return(images); }
/// <summary> /// Factory for creating Images /// </summary> /// <param name="imagesToAdd">images model to be added</param> /// <param name="imageType">PropertyImages/SightImages/UserImages/CityImages</param> public ISet <Images> CreateImagesFactory(ImageCreateViewModel imagesToAdd, string imageType, string userName) { string callerName; switch (imageType) // Caller Determining { case "PropertyImages": int propertyId = int.Parse(imagesToAdd.ForeignKey); callerName = unitOfWork.PropertiesRepository .Where(p => p.Id == propertyId).Select(p => p.Owner.UserName) .FirstOrDefault(); //If property not found if (string.IsNullOrEmpty(callerName)) { throw new ArgumentException("No such Property"); } break; case "SightImages": int sightId = int.Parse(imagesToAdd.ForeignKey); callerName = unitOfWork.SightsRepository .Where(p => p.SightId == sightId).Select(p => p.SightName) .FirstOrDefault(); //If property not found if (string.IsNullOrEmpty(callerName)) { throw new ArgumentException("No such Sight"); } break; case "UserImages": callerName = userName; //If property not found if (string.IsNullOrEmpty(callerName)) { throw new ArgumentException("No such user"); } break; case "CityImages": int cityId = int.Parse(imagesToAdd.ForeignKey); callerName = unitOfWork.CitiesRepository .Where(p => p.CityId == cityId).Select(p => p.CityName) .FirstOrDefault(); //If property not found if (string.IsNullOrEmpty(callerName)) { throw new ArgumentException("No such city"); } break; default: throw new ArgumentException("No such image type"); } ISet <Images> images = new HashSet <Images>(); foreach (HttpPostedFileBase imageToAdd in imagesToAdd.ImageFiles) { Images image = null; switch (imageType) { case "PropertyImages": image = new PropertyImages { ImagePath = PathManager.CreateUserPropertyImagePath(callerName, imageToAdd.FileName), PropertyId = int.Parse(imagesToAdd.ForeignKey) }; break; case "SightImages": image = new SightImages { ImagePath = PathManager.CreateSightImagePath(callerName, imageToAdd.FileName), SightId = int.Parse(imagesToAdd.ForeignKey) }; break; case "UserImages": image = new UserImages { ImagePath = PathManager.CreateUserProfileImagePath(callerName, imageToAdd.FileName), UserId = (string)imagesToAdd.ForeignKey }; break; case "CityImages": image = new CityImages { ImagePath = PathManager.CreateCityImagePath(callerName, imageToAdd.FileName), CityId = int.Parse(imagesToAdd.ForeignKey) }; break; default: throw new ArgumentException("No such image type"); } image.ImageType = imageToAdd.ContentType; var physicalPath = Path.Combine(HttpRuntime.AppDomainAppPath.TrimEnd('\\'), image.ImagePath.TrimStart('\\')); var physicalDirPath = Path.GetDirectoryName(physicalPath); if (!Directory.Exists(physicalDirPath)) { DirectoryHelpers.CreateDirectory(physicalDirPath); } imageToAdd.SaveAs(physicalPath); //Add Image to FileSystem Image img = Image.FromStream(imageToAdd.InputStream); image.ImageRatio = (float)img.Width / img.Height; //Adding image to DataBase unitOfWork.ImagesRepository.Add(image); unitOfWork.Save(); //Add Image to returned set images.Add(image); } return(images); }