Пример #1
0
        //[ValidateAntiForgeryToken]
        public ActionResult Upload(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode)
        {
            string url;
            string message;
            string output;

            if (upload == null)
            {
                return(null);
            }

            const string extensions =
                ".7z|.aiff|.asf|.avi|.bmp|.csv|.doc|.docx|.fla|.flv|.gif|.gz|.gzip|.jpeg|.jpg|.mid|.mov|.mp3|.mp4|.mpc|.mpeg|.mpg|.ods|.odt|.pdf|.png|.ppt|.pxd|.qt|.ram|.rar|.rm|.rmi|.rmvb|.rtf|.sdc|.sitd|.swf|.sxc|.sxw|.tar|.tgz|.tif|.tiff|.txt|.vsd|.wav|.wma|.wmv|.xls|.xml|.zip";

            if (upload.ContentLength == 0 || upload.ContentLength > 1000000 ||
                extensions.Split('|').All(e => e != Path.GetExtension(upload.FileName)) ||
                !UploadUtilities.IsValidImageBinary(upload.InputStream))
            {
                message = ValidationResources.SelectedFileIsInvalid;

                output = BuildOutput(CKEditorFuncNum, null, message);

                return(Content(output));
            }

            var file = new File
            {
                Uploader    = _webHelper.GetCurrentUser(HttpContext),
                AccessMode  = AccessMode.Any,
                CreateDate  = DateTime.UtcNow,
                Name        = upload.FileName,
                ContentType = upload.ContentType,
                Size        = upload.ContentLength,
                IsPublished = true
            };

            _fileService.SaveFile(file);

            bool isSaved;

            try
            {
                isSaved = _unitOfWork.SaveChanges() > 0;
            }
            catch
            {
                isSaved = false;
            }

            if (isSaved)
            {
                Logger.SaveLog(new CreateFileProvider(file));

                string targetPath = Server.MapPath(Constants.UploadsUrl);

                UploadUtilities.Save(upload, targetPath, file.Guid.ToString());

                url = Url.RouteUrl("Download", new { file.Guid, fn = file.Name });

                message = ValidationResources.UploadFileSuccess;

                output = BuildOutput(CKEditorFuncNum, url, message);
                return(Content(output));
            }

            message = ValidationResources.UploadFileFailure;

            output = BuildOutput(CKEditorFuncNum, null, message);
            return(Content(output));
        }
Пример #2
0
        public ActionResult Create(Carousel carousel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("",
                                         ValidationResources.InvalidState);

                return(ViewOrPartialView(carousel));
            }

            if (carousel.Slide == null)
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageRequired);

                return(ViewOrPartialView(carousel));
            }

            if (carousel.Slide.ContentLength > 1000000)
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageLength);

                return(ViewOrPartialView(carousel));
            }

            if (!UploadUtilities.IsValidImageBinary(carousel.Slide.InputStream))
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageInvalidBinary);

                return(ViewOrPartialView(carousel));
            }

            _carouselService.SaveCarousel(carousel);

            bool isSaved;

            try
            {
                isSaved = _unitOfWork.SaveChanges() > 0;
            }
            catch
            {
                isSaved = false;
            }

            if (isSaved)
            {
                Logger.SaveLog(new CreateCarouselProvider(carousel));
            }
            else
            {
                ModelState.AddModelError("", ValidationResources.CreationFailure);

                return(ViewOrPartialView(carousel));
            }

            UploadUtilities.TryToSaveImage(carousel.Slide, Constants.CarouselsUrl, carousel.Id.ToString());

            if (IsReferrerValid())
            {
                return(Redirect(Request.UrlReferrer.AbsolutePath));
            }

            return(RedirectToAction("List", new { page = 1 }));
        }