Exemplo n.º 1
0
        public async Task<IHttpActionResult> AddVideo()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return BadRequest("Unsupported media type.");
            }

            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(root);
            await Request.Content.ReadAsMultipartAsync(provider);

            if (!provider.FileData.Any())
            {
                return BadRequest("Image file needs to be uploaded");
            }
            var file = provider.FileData.First();

            try
            {
                var model = JsonConvert.DeserializeObject<AddVideoViewModel>(provider.FormData["model"]);

                var category = await _videoManager.GetVideoCategoryById(model.VideoCategoryId);

                if (category == null)
                {
                    return BadRequest();
                }
                
                var uniqueFilename = file.Headers.ContentDisposition.FileName.ToGfsFilename();
                var sanitizedFilename = file.Headers.ContentDisposition.FileName.SanitizeWebApiContentDispositionFilename();

                using (var fs = new FileStream(file.LocalFileName, FileMode.Open)) {
                    await _gridFsHelper.UploadFromStreamAsync(uniqueFilename, fs, sanitizedFilename, MediaTypeEnum.Images);

                    var video = new VideoFile
                    {
                        CategoryId = model.VideoCategoryId,
                        CategoryName = category.Name,
                        Name = model.VideoFilename,
                        NameUrl = model.VideoFilename.Slugify(),
                        YearReleased = model.YearReleased,
                        Quality = model.Quality,
                        ImageGfsFilename = uniqueFilename,
                        ImageFilename = sanitizedFilename
                    };

                    await _videoManager.AddVideoFile(video);
                }

                File.Delete(file.LocalFileName);

                return Ok();
            }
            catch (Exception ex)
            {
                File.Delete(file.LocalFileName);
                Log.Error(ex);
                return InternalServerError(ex);
            }
        }
Exemplo n.º 2
0
        public async Task<VideoFile> AddVideoFile(VideoFile video) {
            await _videoRepos.AddAsync(video);

            return video;
        }