Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        public async Task <List <Images> > CreateUserImages(List <HttpPostedFileBase> images, string userId)
        {
            string callerName = await userManager.Users
                                .Where(u => u.Id == userId)
                                .Select(u => u.UserName)
                                .FirstOrDefaultAsync()
                                ?? throw new ArgumentException("Не е намерен потребителят.");


            float GetSizeRatio(HttpPostedFileBase imageFile)
            {
                using (Image img = Image.FromStream(imageFile.InputStream))
                {
                    return((float)img.Width / img.Height);
                }
            }

            var imagesToAdd = images
                              .Select(i => (Images) new UserImages
            {
                ImagePath  = PathManager.CreateUserProfileImagePath(callerName, i.FileName),
                UserId     = userId,
                ImageRatio = GetSizeRatio(i),
                ImageType  = i.ContentType
            })
                              .ToList();

            unitOfWork.ImagesRepository.AddRange(imagesToAdd);
            await unitOfWork.SaveAsync();

            //Add images to FileSystem
            foreach (HttpPostedFileBase imageToAdd in images)
            {
                var physicalPath    = Path.Combine(HttpRuntime.AppDomainAppPath.TrimEnd('\\'), PathManager.CreateUserProfileImagePath(callerName, imageToAdd.FileName).TrimStart('\\'));
                var physicalDirPath = Path.GetDirectoryName(physicalPath);
                if (!Directory.Exists(physicalDirPath))
                {
                    DirectoryHelpers.CreateDirectory(physicalDirPath);
                }
                imageToAdd.SaveAs(physicalPath);

                using (Image img = Image.FromStream(imageToAdd.InputStream))
                {
                    ImageHelpers.SaveAsWebP(img, physicalPath, img.Width, img.Height);
                }
            }

            return(imagesToAdd);
        }