public IActionResult UploadVideo(FileDataDto fileDataDto) { var file = Request.Form.Files["FileData"]; var id = string.Empty; string[] mimeTypes = new[] { "video/mp4", "video/webm" }; if (mimeTypes.Contains(file.ContentType)) { var fileData = new FileData() { ContentType = file.ContentType, Author = User.Identities.First().Name, TagsArray = fileDataDto.Tags?.Split(DELIMITER), Hero = fileDataDto.Hero }; using (var ms = new MemoryStream()) { file.CopyTo(ms); var fileBytes = ms.ToArray(); var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); fileData.FileName = parsedContentDisposition.FileName.ToString().Trim('"'); fileData.FileContents = fileBytes; } id = _videoService.AddAsync(fileData).Result; } return(Ok(id)); }
public async Task <IHttpActionResult> Post(string courseId, [FromBody, CustomizeValidator(RuleSet = "AddVideo")] VideoView video) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var videoInfo = _mapper.Map <VideoView, VideoMaterialInfo>(video); var result = await _videoService.AddAsync(videoInfo, courseId); return(result.IsError ? BadRequest(result.Message) : (IHttpActionResult)Ok(result.Data)); }
public async Task <IActionResult> OnPostAsync(ICollection <IFormFile> file) { string yyyymm = DateTime.Now.ToyyyyMM(); var config = await _configService.GetAsync(); string oppositePath = $"videos/{User.GetUserIdOrZero()}/{yyyymm}/"; var folder = _env.WebRootPath + "/upload/" + oppositePath; if (config != null && !string.IsNullOrEmpty(config.PictureSavePath)) { folder = Path.Combine(config.PictureSavePath, oppositePath); } if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } //long[] groupIds = groups.SplitToInt64(); //检查素材分组与用户关系 //foreach (var id in groupIds) //{ // var group = await _groupService.GetByIdAsync(id); // if (group == null || group.Type != Domain.Enum.MaterialCenter.EmUserMaterialGroupType.Picture // || group.UserId != User.GetUserIdOrZero()) // { // throw new KuException("选择的素材分组不正确!"); // } //} foreach (var item in file) { VideoDto model = new VideoDto(); model.Id = ID.NewID(); string suffix = item.FileName.Split('.').Last().ToLower(); var saveName = model.Id + "." + suffix; var path = Path.Combine(folder, saveName); using (var fileStream = new FileStream(path, FileMode.Create)) { await item.CopyToAsync(fileStream); await fileStream.FlushAsync(); } //try //{ // model.Md5Code = VideoHelper.GetVideoDuration(path); //} //catch (Exception ex) //{ // _logger.LogError(ex.Message); //} //try //{ // model.ThumbPath = VideoHelper.GetPreviewImage(path); //} //catch (Exception ex) //{ // _logger.LogError(ex.Message); //} model.Title = item.FileName; model.FileName = saveName; model.Folder = oppositePath; model.FilePath = oppositePath + saveName; model.FileSize = item.Length; model.FileType = suffix; model.UploadUserId = User.GetUserIdOrZero(); await _service.AddAsync(model); } return(Json(true)); }