Ejemplo n.º 1
0
        public async Task <IActionResult> AddCategory(CategoryModel addCategoryModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(addCategoryModel));
            }

            var response = await GenericGetDataClass <CategoryModel> .AddData("api/addcategory", addCategoryModel);

            if (response)
            {
                TempData["SM"] = "Категория успешно добавлена!";
                return(RedirectToAction(nameof(ShowAllCategories)));
            }
            else
            {
                return(View(addCategoryModel));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddVideo(VideoVM addedVideo, IFormFile[] file)
        {
            if (file == null || file.Length == 0)
            {
                ModelState.AddModelError("", "Видеофайл не выбран, пожалуйста, добавьте видео и попробуйте снова");
                return(View(addedVideo));
            }

            #region Save video file
            if (file.Length == 1)
            {
                if (!ModelState.IsValid)
                {
                    return(View(addedVideo));
                }

                // Формируем пути
                var webRootPath   = _hostingEnvironment.WebRootPath;
                var extension     = Path.GetExtension(file[0].FileName);
                var uploads       = Path.Combine(webRootPath, SD.VideosFolder, addedVideo.VideoModel.Name + extension);
                var pathToVideo   = Path.Combine("\\" + SD.VideosFolder, addedVideo.VideoModel.Name + extension);
                var directoryPath = Path.Combine(webRootPath, SD.VideosFolder);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                addedVideo.VideoModel.Path = pathToVideo;

                var response = await GenericGetDataClass <VideoVM> .AddData("api/addvideo", addedVideo);

                if (response)
                {
                    var   stream = new FileStream(uploads, FileMode.Create);
                    await file[0].CopyToAsync(stream);
                    stream.Close();

                    TempData["SM"] = "Видео успешно добавлено!";
                    return(RedirectToAction(nameof(ShowAllVideo)));
                }
                else
                {
                    ModelState.AddModelError("", "Ошибка сохранения данных");
                    return(View(addedVideo));
                }

                #endregion
            }
            else
            {
                var webRootPath   = _hostingEnvironment.WebRootPath;
                var directoryPath = Path.Combine(webRootPath, SD.VideosFolder);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                foreach (var item in file)
                {
                    string extension = Path.GetExtension(item.FileName);
                    addedVideo.VideoModel.Name       = String.Format(System.Guid.NewGuid() + extension);
                    addedVideo.VideoModel.CategoryId = 1;
                    var uploads     = Path.Combine(webRootPath, SD.VideosFolder, addedVideo.VideoModel.Name + extension);
                    var pathToVideo = Path.Combine("\\" + SD.VideosFolder, addedVideo.VideoModel.Name + extension);
                    addedVideo.VideoModel.Path = pathToVideo;
                    var response = await GenericGetDataClass <VideoVM> .AddData("api/addvideo", addedVideo);

                    if (response)
                    {
                        var stream = new FileStream(uploads, FileMode.Create);
                        await item.CopyToAsync(stream);

                        stream.Close();
                    }
                    else
                    {
                        ModelState.AddModelError("", "Ошибка сохранения данных");
                        return(View(addedVideo));
                    }
                }
                TempData["SM"] = "Видео успешно добавлены!";
                return(RedirectToAction(nameof(ShowAllVideo)));
            }
        }