예제 #1
0
        public ActionResult Upload()
        {
            var categories = _wallpaperService.GetCategoryList();
            var resolutions = _wallpaperService.GetResolutionList();

            var model = new WallpaperViewModel
            {
                CategoryList = categories,
                ResolutionList = resolutions
            };
            return View(model);
        }
예제 #2
0
        public ActionResult UploadWallpaper(WallpaperViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = _wallpaperService.WallpaperInsert(model);
                if (result.Success)
                {
                    ViewBag.Message = "Wallpaper uploaded successfuly !";
                    return RedirectToAction("Upload");
                }
                foreach (var error in result.Errors)
                    ModelState.AddModelError("", error);
            }
            var categories = _wallpaperService.GetCategoryList();
            var resolutions = _wallpaperService.GetResolutionList();

            model = new WallpaperViewModel
            {
                CategoryList = categories,
                ResolutionList = resolutions
            };
            return View(model);
        }
예제 #3
0
        public DatabaseOperationResult WallpaperInsert(WallpaperViewModel model)
        {
            var result = new DatabaseOperationResult();
            if (model.file == null && model.file.ContentLength == 0)
            {
                result.AddError("File not found!");
                return result;
            }

            if (((model.file.ContentLength / 1024) / 1024) > 2)
            {
                result.AddError("File must be less than 2 MB");
                return result;
            }

            System.Drawing.Image resolution = System.Drawing.Image.FromStream(model.file.InputStream);
            if (resolution.Width < 1024 || resolution.Height < 768)
            {
                result.AddError("The file must be greater than 1024 pixels wide and greater than to 768 pixels tall at least.");
                return result;
            }

            var fileExtension = System.IO.Path.GetExtension(model.file.FileName);
            var allowedExtensions = new List<string> { ".jpg", ".jpeg", ".tiff", ".png" };
            if (!string.IsNullOrWhiteSpace(fileExtension))
                fileExtension = fileExtension.ToLowerInvariant();
            else
            {
                result.AddError("The file extension not provided. Please try again");
                return result;
            }

            if (!allowedExtensions.Contains(fileExtension))
            {
                result.AddError("The file extension not supported, allowed extension is \"*.jpg\", \"*.jpeg\", \"*.tiff\"");
                return result;
            }

            if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads")))
                System.IO.Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads"));

            var lastWallpaper = _unitOfWork.WallpaperRepository.Table().OrderByDescending(x => x.Id).FirstOrDefault();
            string fileName = lastWallpaper == null ? "walltage-1" : "walltage-" + (lastWallpaper.Id + 1);
            model.ImgPath = string.Format("{0}{1}", fileName, fileExtension);
            string filePath = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads"), model.ImgPath);

            var tagList = PrepareTagList(model.Tags);

            model.Name = System.IO.Path.GetFileNameWithoutExtension(model.file.FileName);
            _unitOfWork.WallpaperRepository.Insert(new Domain.Entities.Wallpaper
            {
                AddedBy = _sessionWrapper.UserName,
                AddedDate = DateTime.Now,
                CategoryId = model.CategoryId,
                ImgPath = model.ImgPath,
                Name = model.Name,
                ResolutionId = model.ResolutionId,
                Size = model.file.ContentLength,
                UploaderId = _sessionWrapper.UserId,
                TagList = tagList
            });
            _unitOfWork.Save(true);

            if (!System.IO.File.Exists(filePath))
            {
                model.file.SaveAs(filePath);

                if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads/Thumbs")))
                    System.IO.Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath("/Uploads/Thumbs"));

                System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
                image = _webHelper.CreateThumbnail(image, new System.Drawing.Size(256, 250), true);
                image.Save(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads/Thumbs/") + fileName);
                image.Dispose();
            }

            return result;
        }