private string ValidateInputs(SnipModel sm, decimal startSecondsTotal, decimal endSecondsTotal) { try { // no filename error if (sm.Url.Replace("http://", "") == "" && sm.File.FileName.Length < 4) { return("url or filename is required!"); } // bad time range error if (endSecondsTotal < startSecondsTotal || endSecondsTotal == startSecondsTotal) { return("bad time range!"); } return(""); } catch { throw; } }
private PhysicalFileResult ProcessStream(SnipModel sm) { try { // get all the values in seconds var sH = sm.StartHours * 60 * 60; var sM = sm.StartMinutes * 60; var sS = sm.StartSeconds; var eH = sm.EndHours * 60 * 60; var eM = sm.EndMinutes * 60; var eS = sm.EndSeconds; var startSecondsTotal = sH + sM + sS; var endSecondsTotal = eH + eM + eS; var validationResult = ValidateInputs(sm, startSecondsTotal, endSecondsTotal); if (validationResult != "") { ModelState.Remove("StatusMessage"); sm.StatusMessage = validationResult; return(null); } // get the name of the mp3 out of the URL // 1. split on "?" // 2. split that result on "/" // 3. take last element of that as the mp3 name string[] split1 = sm.Url.Trim().Split('?'); string[] split2 = split1[0].Split('/'); string[] split3 = split2[split2.Length - 1].Split('.'); string mp3Name = split3[0]; var splitLength = endSecondsTotal - startSecondsTotal; HttpWebRequest dynamicRequest = (HttpWebRequest)WebRequest.Create(sm.Url.Trim()); var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory + "app_data\\uploads", mp3Name + ".mp3"); FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); // write original mp3 file to a location (var path) on disk using (Stream stream = dynamicRequest.GetResponse().GetResponseStream()) { byte[] buffer = new byte[32768]; int read; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, read); } fs.Close(); } // split this text into hours, minutes, seconds for the filename string[] totalTimePieces = sm.SnipLength.ToString().Split(':'); var outputFilename = String.Format("{7}[{1}{2}m{3}s-{4}{5}m{6}s, {8}{9}m{10}s total] {0}", mp3Name, EvalHourTextForOutputFilename(sm.StartHours), sm.StartMinutes.ToString(), sm.StartSeconds.ToString(), EvalHourTextForOutputFilename(sm.EndHours), sm.EndMinutes.ToString(), sm.EndSeconds.ToString(), EvalOptionalTag(sm.Tag), EvalHourTextForOutputFilename(Convert.ToInt32(totalTimePieces[0])), totalTimePieces[1], totalTimePieces[2]); return(Process(path, outputFilename, startSecondsTotal, splitLength)); // TODO: <><><>< I SHOULD be able to delete the temp file, right??? ><><><> } catch { throw; } }