[HttpPost]
public async Task<IActionResult> AddVideo(Videos model, IFormFile FileName)
{
string msg = "";
try
{
if(model.IsStreaming == false)
{
if (FileName != null && FileName.Length > 0)
{
var uploads = Path.Combine(_environment.WebRootPath.ToString(), "uploads/video/");
if (FileName.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(FileName.ContentDisposition).FileName.Trim('"');
string UniqueFileName = string.Format(@"{0}", Guid.NewGuid()) + fileName.ToString();
using (var fileStream = new FileStream(Path.Combine(uploads, UniqueFileName), FileMode.Create))
{
await FileName.CopyToAsync(fileStream);
}
model.FileName = UniqueFileName.ToString();
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
ffMpeg.GetVideoThumbnail(Path.Combine(uploads, UniqueFileName), uploads + UniqueFileName + ".jpg", 10);
model.Thumnail = UniqueFileName + ".jpg";
}
}
}else
{
var youtubeIds = model.Url.Split('=');
var uploads = Path.Combine(_environment.WebRootPath.ToString(), "uploads/video/");
string UniqueFileName = string.Format(@"{0}", Guid.NewGuid()+ ".jpg");
using (WebClient client = new WebClient())
{
client.DownloadFileAsync(new Uri("https://img.youtube.com/vi/"+ youtubeIds[1] + "/0.jpg"), Path.Combine(uploads, UniqueFileName));
}
model.Url = "https://www.youtube.com/embed/" + youtubeIds[1];
model.Thumnail = UniqueFileName;
}
DB.Videos.Add(model);
await DB.SaveChangesAsync();
msg = "บันทึกข้อมูลสำเร็จ";
}
catch (Exception e)
{
msg = "Error is :" + e.InnerException.Message;
return Json(new { valid = false, message = msg, type = model.VideoCategoryId });
}
return Json(new { valid = true, message = msg, type = model.VideoCategoryId });
}