/// <summary> /// Update Execise Video To Fitcom sprout server /// </summary> /// <param name="sPath"></param> /// <param name="exerciseId"></param> /// <param name="sproudId"></param> private void UpdateExeciseVideoToFitcom(string sPath, int exerciseId, string sproudId) { try { GetSingleExeciseVideo(); using (WebClient httpWebClient = new WebClient()) { httpWebClient.Headers[ConstantHelper.constSproutVideoApiKey] = ConstantHelper.constSproutVideoApiKeyValue; httpWebClient.Headers[HttpRequestHeader.Host] = ConstantHelper.constSproutHost; string replacedExerciseWepApi = string.Format(ConstantHelper.constSproutReplacedUploadApi, sproudId); Uri replacedUrl = new Uri(replacedExerciseWepApi); var uploadedExecisedata = httpWebClient.UploadFile(replacedUrl, "POST", sPath); if (uploadedExecisedata != null) { if (System.IO.File.Exists(sPath)) { System.IO.File.Delete(sPath); } var uploadesfile = System.Text.Encoding.ASCII.GetString(uploadedExecisedata); if (uploadesfile != null) { ExerciseVideoResponse response = JsonConvert.DeserializeObject <ExerciseVideoResponse>(uploadesfile); if (response != null) { UpdateExerciseSproutLink objUpdateExerciseSproutLink = new UpdateExerciseSproutLink { SecuryId = response.security_token, VideoId = response.id, V1080pUrl = response.assets.videos._1080p, V240pUrl = response.assets.videos._240p, V360pUrl = response.assets.videos._360p, V480pUrl = response.assets.videos._480p, V720pUrl = response.assets.videos._720p, ThumnailUrl = response.assets.thumbnails[0], ExerciseId = exerciseId }; // Get all details of ExerciseBL.UpdateExerciseSproutData(objUpdateExerciseSproutLink); } } } System.IO.File.Delete(sPath); } } catch (WebException ex) { LogManager.LogManagerInstance.WriteErrorLog(ex); throw; } catch (Exception ex) { LogManager.LogManagerInstance.WriteErrorLog(ex); throw; } }
private void UploadExecelToFitcom(string sPath) { try { using (WebClient httpWebClient = new WebClient()) { httpWebClient.Headers[ConstantHelper.constSproutVideoApiKey] = ConstantHelper.constSproutVideoApiKeyValue; httpWebClient.Headers[HttpRequestHeader.Host] = ConstantHelper.constSproutHost; Uri uploadExerciseUri = new Uri(ConstantHelper.constSproutUploadApi); var uploadedExecisedata = httpWebClient.UploadFile(uploadExerciseUri, "POST", sPath); if (uploadedExecisedata != null) { var uploadesfile = System.Text.Encoding.ASCII.GetString(uploadedExecisedata); if (uploadesfile != null) { ExerciseVideoResponse response = JsonConvert.DeserializeObject <ExerciseVideoResponse>(uploadesfile); if (response != null) { UpdateExerciseSproutLink objUpdateExerciseSproutLink = new UpdateExerciseSproutLink { SecuryId = response.security_token, VideoId = response.id }; // Get all details of ExerciseBL.UpdateExerciseSproutData(objUpdateExerciseSproutLink); } } } if (System.IO.File.Exists(sPath)) { System.IO.File.Delete(sPath); } } } catch (WebException ex) { LogManager.LogManagerInstance.WriteErrorLog(ex); throw; } catch (Exception ex) { LogManager.LogManagerInstance.WriteErrorLog(ex); throw; } }
/// <summary> /// Add Exercise SproutData /// </summary> /// <param name="updateExecise"></param> public static void AddExerciseSproutData(UpdateExerciseSproutLink updateExecise) { StringBuilder traceLog = new StringBuilder(); using (LinksMediaContext dataContext = new LinksMediaContext()) { try { traceLog.AppendLine("Start: AddExerciseSproutData---- " + DateTime.Now.ToLongDateString()); if (updateExecise != null) { tblExercise uploadExecise = new tblExercise(); if (uploadExecise != null) { string vidurlformat = "https://api-files.sproutvideo.com/file/{0}/{1}/{2}.mp4"; string v1 = updateExecise.V1080pUrl; string v5 = updateExecise.V240pUrl; string v4 = updateExecise.V360pUrl; string v3 = updateExecise.V480pUrl; string v2 = updateExecise.V720pUrl; if (string.IsNullOrEmpty(Convert.ToString(v2))) { if (string.IsNullOrEmpty(v3)) { if (string.IsNullOrEmpty(v4)) { uploadExecise.V720pUrl = v5; } else { uploadExecise.V720pUrl = v4; } } else { uploadExecise.V720pUrl = v3; } } else { uploadExecise.V720pUrl = v2; } if (string.IsNullOrEmpty(v1)) { uploadExecise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "1080"); } if (string.IsNullOrEmpty(v2) && string.IsNullOrEmpty(updateExecise.V720pUrl)) { uploadExecise.V720pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "240"); } if (string.IsNullOrEmpty(v3)) { uploadExecise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "480"); } if (string.IsNullOrEmpty(v4)) { uploadExecise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "360"); } if (string.IsNullOrEmpty(v5)) { uploadExecise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "240"); } uploadExecise.ThumnailUrl = updateExecise.ThumnailUrl; uploadExecise.SecuryId = updateExecise.SecuryId; uploadExecise.VideoId = updateExecise.VideoId; uploadExecise.IsUpdated = true; uploadExecise.VideoLink = updateExecise.VideoLink; uploadExecise.VideoId = updateExecise.VideoId; uploadExecise.ExerciseName = updateExecise.ExerciseName; uploadExecise.Index = updateExecise.Index; uploadExecise.IsActive = updateExecise.IsActive; uploadExecise.Description = updateExecise.Description; uploadExecise.VideoLink = updateExecise.VideoLink; uploadExecise.TrainerID = updateExecise.TrainerID; uploadExecise.TeamID = updateExecise.TeamID; uploadExecise.ExerciseStatus = 1; dataContext.Exercise.Add(uploadExecise); dataContext.SaveChanges(); } } } catch { throw; } finally { traceLog.AppendLine("End AddExerciseSproutData : --- " + DateTime.Now.ToLongDateString()); LogManager.LogManagerInstance.WriteTraceLog(traceLog); } } }
public ActionResult UploadExerciseVideosAndExcel() { StringBuilder traceLog = null; if (Login.IsSessionExpire(Convert.ToString(HttpContext.Session[ConstantHelper.constLoginUserName])) || Convert.ToString(HttpContext.Session[ConstantHelper.constUserType]) != ConstantHelper.constLoginadmin) { return(RedirectToAction(ConstantHelper.constLogin, ConstantHelper.constLogin)); } DataTable dt = null; try { traceLog = new StringBuilder(); traceLog.AppendLine("Start: UploadExerciseVideos() in Execise Controller"); var httpRequest = System.Web.HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { Dictionary <string, string> uploadedExeciseVideos = new Dictionary <string, string>(); List <UpdateExerciseSproutLink> FailToUploadedExeciseVideos = new List <UpdateExerciseSproutLink>(); for (int i = 0; i < httpRequest.Files.Count; i++) { var fileDoc = httpRequest.Files[i]; IExcelDataReader reader = null; string sPath = ""; sPath = Server.MapPath("~/Temp/"); string newVideoId = Path.GetFileName(fileDoc.FileName); string execiseName = Path.GetFileNameWithoutExtension(fileDoc.FileName); if (fileDoc.FileName.EndsWith(".xls", StringComparison.OrdinalIgnoreCase)) { using (Stream stream = fileDoc.InputStream) { reader = ExcelReaderFactory.CreateBinaryReader(stream); reader.IsFirstRowAsColumnNames = true; DataSet result = reader.AsDataSet(); reader.Close(); dt.Clear(); dt = result.Tables[0]; } } else if (fileDoc.FileName.EndsWith(".csv", StringComparison.OrdinalIgnoreCase)) { dt = new DataTable(); dt.Locale = CultureInfo.InvariantCulture; dt.Clear(); dt.Columns.Add("Name"); dt.Columns.Add("Index"); dt.Columns.Add("Team ID"); dt.Columns.Add("Trainer ID"); if (!System.IO.File.Exists(sPath + newVideoId)) { fileDoc.SaveAs(sPath + newVideoId); } using (var fileReader = System.IO.File.OpenText(sPath + newVideoId)) using (var csvReader = new CsvHelper.CsvReader(fileReader)) { csvReader.Configuration.HasHeaderRecord = true; csvReader.Configuration.IgnoreHeaderWhiteSpace = false; csvReader.Configuration.IsHeaderCaseSensitive = true; csvReader.Configuration.RegisterClassMap <CSVExerciseMap>(); while (csvReader.Read()) { var record = csvReader.GetRecord <CSVExerciseVM>(); if (record != null) { DataRow rowdata = dt.NewRow(); rowdata["Name"] = record.Name; rowdata["Index"] = record.Index; rowdata["Team ID"] = record.TeamID; rowdata["Trainer ID"] = record.TrainerID; dt.Rows.Add(rowdata); } } } } else if (fileDoc.FileName.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase)) { using (Stream stream = fileDoc.InputStream) { reader = ExcelReaderFactory.CreateOpenXmlReader(stream); reader.IsFirstRowAsColumnNames = true; DataSet result = reader.AsDataSet(); reader.Close(); dt = result.Tables[0]; } } else if (fileDoc.FileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)) { uploadedExeciseVideos.Add(execiseName, sPath + newVideoId); } else { var FailToUpload = new UpdateExerciseSproutLink { ExerciseName = execiseName, Index = string.Empty, TeamID = string.Empty, TrainerID = string.Empty, FailedVideoName = fileDoc.FileName, }; FailToUploadedExeciseVideos.Add(FailToUpload); } // Save the files in local directory if (!System.IO.File.Exists(sPath + newVideoId)) { fileDoc.SaveAs(sPath + newVideoId); } } // varify execel execisename and upload on sprout server foreach (DataRow row in dt.Rows) { string execiseName = string.Empty; string index = string.Empty; string teamID = string.Empty; string trainerID = string.Empty; foreach (DataColumn col in dt.Columns) { switch (col.ColumnName) { case "Name": execiseName = Convert.ToString(row[col.ColumnName]); break; case "Index": index = Convert.ToString(row[col.ColumnName]); break; case "Team ID": teamID = Convert.ToString(row[col.ColumnName]); break; case "Trainer ID": trainerID = Convert.ToString(row[col.ColumnName]); break; } } // uploaded Execise Videos on sprout server if (uploadedExeciseVideos.ContainsKey(execiseName)) { // find out local stored file path from dictionary string localUploadPath = string.Empty; if (uploadedExeciseVideos.TryGetValue(execiseName, out localUploadPath)) { if (!string.IsNullOrEmpty(localUploadPath)) { // Upload on Execise Video on sprout server, get response and add to Fitcom database; string uploadesfile = UploadExeciseVideoToFitcom(localUploadPath); if (!string.IsNullOrEmpty(uploadesfile)) { ExerciseVideoResponse response = JsonConvert.DeserializeObject <ExerciseVideoResponse>(uploadesfile); if (response != null) { UpdateExerciseSproutLink objUpdateExerciseSproutLink = new UpdateExerciseSproutLink { SecuryId = response.security_token, VideoId = response.id, V1080pUrl = response.assets.videos._1080p, V240pUrl = response.assets.videos._240p, V360pUrl = response.assets.videos._360p, V480pUrl = response.assets.videos._480p, V720pUrl = response.assets.videos._720p, ThumnailUrl = response.assets.thumbnails[0], ExerciseName = execiseName, IsActive = true, Index = index, Description = string.Empty, IsUpdated = true, TeamID = teamID, TrainerID = trainerID, VideoLink = execiseName + ConstantHelper.constMP4ExesionWithdot, SourceUrl = string.Empty }; // Add Execise Details ExerciseBL.AddExerciseSproutData(objUpdateExerciseSproutLink); } } else { var FailToUpload = new UpdateExerciseSproutLink { ExerciseName = execiseName, Index = index, TeamID = teamID, TrainerID = trainerID, FailedVideoName = execiseName + ConstantHelper.constMP4ExesionWithdot, }; FailToUploadedExeciseVideos.Add(FailToUpload); } } // Delete Uploaded Execise if (System.IO.File.Exists(localUploadPath)) { System.IO.File.Delete(localUploadPath); } localUploadPath = string.Empty; } } else if (!string.IsNullOrEmpty(execiseName)) { var FailToUpload = new UpdateExerciseSproutLink { ExerciseName = execiseName, Index = index, TeamID = teamID, TrainerID = trainerID, FailedVideoName = string.Empty, }; FailToUploadedExeciseVideos.Add(FailToUpload); } } // Save To Failed Upload data in database ExerciseBL.SaveFailedSproutServer(FailToUploadedExeciseVideos); } else { return(Json(new { Message = "No files selected.", StatusCode = 100 }, JsonRequestBehavior.AllowGet)); } return(Json(new { Message = "File Uploaded Successfully!", StatusCode = 101 }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { LogManager.LogManagerInstance.WriteErrorLog(ex); return(Json(new { Message = "Error occurred. Error details: " + ex.Message, StatusCode = 100 }, JsonRequestBehavior.AllowGet)); } finally { traceLog.AppendLine("UploadExerciseVideos end() : --- " + DateTime.Now.ToLongDateString()); LogManager.LogManagerInstance.WriteTraceLog(traceLog); if (dt != null) { dt.Dispose(); } } }
/// <summary> /// Update Exercise SproutData /// </summary> /// <param name="updateExecise"></param> public static void UpdateExerciseSproutData(UpdateExerciseSproutLink updateExecise) { StringBuilder traceLog = new StringBuilder(); using (LinksMediaContext dataContext = new LinksMediaContext()) { try { traceLog.AppendLine("Start: UpdateExerciseSproutData---- " + DateTime.Now.ToLongDateString()); if (updateExecise != null && updateExecise.ExerciseId > 0) { traceLog.AppendLine("Updating Execise Id- " + updateExecise.ExerciseId); var execise = dataContext.Exercise.Where(ex => ex.ExerciseId == updateExecise.ExerciseId).FirstOrDefault(); if (execise != null) { string vidurlformat = "https://api-files.sproutvideo.com/file/{0}/{1}/{2}.mp4"; string v1 = updateExecise.V1080pUrl; string v5 = updateExecise.V240pUrl; string v4 = updateExecise.V360pUrl; string v3 = updateExecise.V480pUrl; string v2 = updateExecise.V720pUrl; if (string.IsNullOrEmpty(Convert.ToString(v2))) { if (string.IsNullOrEmpty(v3)) { if (string.IsNullOrEmpty(v4)) { execise.V720pUrl = v5; } else { execise.V720pUrl = v4; } } else { execise.V720pUrl = v3; } } else { execise.V720pUrl = v2; } if (string.IsNullOrEmpty(v1)) { execise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "1080"); } if (string.IsNullOrEmpty(v2) && string.IsNullOrEmpty(updateExecise.V720pUrl)) { execise.V720pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "240"); } if (string.IsNullOrEmpty(v3)) { execise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "480"); } if (string.IsNullOrEmpty(v4)) { execise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "360"); } if (string.IsNullOrEmpty(v5)) { execise.V240pUrl = string.Format(vidurlformat, updateExecise.VideoId, updateExecise.SecuryId, "240"); } execise.ThumnailUrl = updateExecise.ThumnailUrl; execise.SecuryId = updateExecise.SecuryId; execise.VideoId = updateExecise.VideoId; execise.IsUpdated = true; execise.VideoLink = updateExecise.VideoLink; execise.VideoId = updateExecise.VideoId; dataContext.SaveChanges(); } } } catch { throw; } finally { traceLog.AppendLine("End UpdateExerciseSproutData : --- " + DateTime.Now.ToLongDateString()); LogManager.LogManagerInstance.WriteTraceLog(traceLog); } } }