/// <summary> /// Forward/Remove cookies used by the current HTTPContext to the capturing web browser /// </summary> /// <param name="job">Screenshot job</param> /// <param name="state">if true current context will me forwarded</param> /// <returns></returns> public static ScreenshotJob SetTransfertRequestCookies(this ScreenshotJob job, bool state) { if (!state) { if (job.Cookies.Any()) { job.Cookies = new ReadOnlyCollection <Cookie>(Enumerable.Empty <Cookie>().ToList()); } } else { if (job?.Context?.Cookies == null) { throw new InvalidOperationException("No HttpContext was found for current thread"); } var list = new List <Cookie>(); for (int i = 0; i < job.Context.Cookies.Count; i++) { HttpCookie cookie = job.Context.Cookies[i]; list.Add(CookieHelper.CreateFrom(cookie, HttpContext.Current.Request.Url.Host)); } job.Cookies = new ReadOnlyCollection <Cookie>(list); } return(job); }
/// <summary> /// The map to job. /// </summary> /// <param name="screenshotJob"> /// The screenshot job. /// </param> /// <returns> /// The <see cref="Job"/>. /// </returns> private static Job MapToJob(ScreenshotJob screenshotJob) { return(new Job() { Id = string.IsNullOrEmpty(screenshotJob.job_id) ? screenshotJob.id : screenshotJob.job_id, State = !string.IsNullOrEmpty(screenshotJob.state) ? (Job.States)Enum.Parse(typeof(Job.States), screenshotJob.state.Replace("-", string.Empty), true) : Job.States.Pending, Info = new Job.JobInfo() { CallbackUrl = screenshotJob.callback_url, Orientation = !string.IsNullOrEmpty(screenshotJob.orientation) ? (Job.Orientations?)Enum.Parse(typeof(Job.Orientations), screenshotJob.orientation, true) : null, Quality = !string.IsNullOrEmpty(screenshotJob.quality) ? (Job.Qualities?)Enum.Parse(typeof(Job.Qualities), screenshotJob.quality, true) : null, WaitTime = screenshotJob.wait_time, OsxResolution = !string.IsNullOrEmpty(screenshotJob.mac_res) ? (Job.OSXResolutions?)Enum.Parse(typeof(Job.OSXResolutions), "r_" + screenshotJob.mac_res, true) : null, WinResolution = !string.IsNullOrEmpty(screenshotJob.win_res) ? (Job.WinResolutions?)Enum.Parse(typeof(Job.WinResolutions), "r_" + screenshotJob.win_res, true) : null, }, Screenshots = screenshotJob.Screenshots.Select( x => new API.Screenshots.Screenshot() { Id = x.id, State = !string.IsNullOrEmpty(x.state) ? (Screenshot.States)Enum.Parse(typeof(Screenshot.States), x.state.Replace("-", string.Empty), true) : Screenshot.States.Pending, Url = x.url, Browser = MapInfoToBrowser(x), ThumbnailUrl = x.thumb_url, ImageUrl = x.image_url, CreatedAt = !string.IsNullOrEmpty(x.created_at) ? (DateTimeOffset?)new DateTimeOffset(DateTime.Parse(x.created_at.Replace(" UTC", string.Empty))) : null, }), }); }
/// <summary> /// Forward "Accept-Languages" header used by the current HTTPContext to Freezer capturing browser /// </summary> /// <param name="job"></param> /// <returns></returns> public static ScreenshotJob UseRequestLanguages(this ScreenshotJob job) { if (job.Context == null) { throw new InvalidOperationException("No HttpContext was found for current thread"); } job.SetAcceptLanguages(job.Context.AcceptLanguages); return(job); }
internal ScreenshotMvcResult(ScreenshotJob task, string downloadFileName) : base("image/png") { _lazyData = task.Freeze(); if (downloadFileName != null) { FileDownloadName = downloadFileName; } }
public byte [] ProduceImage(ScreenshotJob task) { using (var workerEngine = _engineProvider.CreateEngine(task)) { foreach (var cookie in task.Cookies) { workerEngine.Cookies.Add(cookie); } workerEngine.AcceptLanguages = task.AcceptLanguages; workerEngine.UserAgent = task.UserAgent; return(workerEngine.CaptureRawUrl(task.FinalUri, task.Timeout)); } }
/// <summary> /// Convert the screenshot job to an file download ActionResult /// </summary> /// <param name="job">Screenshot job</param> /// <param name="downloadFileName">filename</param> /// <returns></returns> public static ActionResult FreezeActionResult(this ScreenshotJob job, string downloadFileName) { return(new ScreenshotMvcResult(job, downloadFileName)); }
internal ScreenshotMvcResult(ScreenshotJob task) : this(task, null) { }