Beispiel #1
0
        /// <summary>
        /// Загрузка файла на сервер
        /// </summary>
        /// <param name="uploadModels">Модель формы загрузки</param>
        /// <returns>Возвращаем модель для отображения</returns>
        public ProductDto UploadProducts(UploadProductModel uploadModels, long creatorId)
        {
            var data = Mapper.Map <Product>(uploadModels);
            var uploadProductInByte = Convert.FromBase64String(uploadModels.UploadProduct);

            data.ProductType = uploadProductInByte.GetMimeFromByteArray();
            if (this._accountRepository.Get(creatorId) == null)
            {
                throw new ArgumentException(Resources.UserNotFound);
            }

            data.CreatorId = creatorId;

            if (string.IsNullOrEmpty(uploadModels.UploadProduct))
            {
                throw new ArgumentNullException(Resources.NullOrEmptyContent);
            }
            else
            {
                data.OriginalProduct   = new OriginalProduct();
                data.CompressedProduct = new CompressedProduct();
                data.ProtectedProduct  = new ProtectedProduct();

                switch (data.ProductType)
                {
                case ProductType.Image:
                    data.OriginalProduct.Content   = uploadProductInByte;
                    data.CompressedProduct.Content = uploadProductInByte.GetCompressedImage();
                    data.ProtectedProduct.Content  = uploadProductInByte.GetProtectedImage();
                    break;

                case ProductType.Music:
                    data.OriginalProduct.Content   = uploadProductInByte;
                    data.CompressedProduct.Content = Convert.FromBase64String(Resources.CompressedAudio);
                    data.ProtectedProduct.Content  = uploadProductInByte.GetProtectedMusic();
                    break;

                case ProductType.Video:
                    data.OriginalProduct.Content   = uploadProductInByte;
                    data.ProtectedProduct.Content  = uploadProductInByte.GetProtectedVideoAsync(HttpContext.Current);
                    data.CompressedProduct.Content = uploadProductInByte.GetCompresedVideoFrameAsync(HttpContext.Current);
                    break;

                case ProductType.unknow:
                    throw new ArgumentException(Resources.UnknowProductType);
                }
            }

            var result = this._repository.Add(data);

            return(result is null ? throw new InvalidOperationException(Resources.UploadProductError) : Mapper.Map <ProductDto>(result));
        }
 public ActionResult UploadProduct(UploadProductModel model, HttpPostedFileBase image = null)
 {
     if (ModelState.IsValid && model != null && image != null) //get uploaded image
     {
         model.ImageName = image.FileName;
         model.Content   = new byte[image.ContentLength];
         image.InputStream.Read(model.Content, 0, image.ContentLength);
         _service.UploadProduct(model);
         return(RedirectToAction("Index", "Home"));
     }
     else
     {
         return(View(model));
     }
 }