public byte[] LogbookDataForBackup() { using (Page p = new FormlessPage()) { p.Controls.Add(new HtmlForm()); IDownloadableAsData ifr = (IDownloadableAsData)p.LoadControl("~/Controls/mfbDownload.ascx"); return(ifr.RawData(User.UserName)); } }
public void LogbookDataForBackup(Stream s) { if (s == null) { throw new ArgumentNullException(nameof(s)); } using (Page p = new FormlessPage()) { p.Controls.Add(new HtmlForm()); using (Control c = p.LoadControl("~/Controls/mfbDownload.ascx")) { ((IDownloadableAsData)c).ToStream(User.UserName, s); } } }
public static string RenderControlsToHTML(Action <FormlessPage> addControlsToPage, Action <HtmlTextWriter> renderToStream) { if (addControlsToPage == null) { throw new ArgumentNullException(nameof(addControlsToPage)); } if (renderToStream == null) { throw new ArgumentNullException(nameof(renderToStream)); } // We have no Page, so things like Page_Load don't get called. // We fix this by faking a page and calling Server.Execute on it. This sets up the form and - more importantly - causes Page_load to be called on loaded controls. using (FormlessPage p = new FormlessPage()) { p.Controls.Add(new System.Web.UI.HtmlControls.HtmlForm()); using (StringWriter sw1 = new StringWriter(CultureInfo.CurrentCulture)) HttpContext.Current.Server.Execute(p, sw1, false); // Add the controls to the page addControlsToPage(p); // Now, write it out. StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb, CultureInfo.CurrentCulture)) { using (HtmlTextWriter htmlTW = new HtmlTextWriter(sw)) { try { renderToStream(htmlTW); return(sb.ToString()); } catch (ArgumentException ex) when(ex is ArgumentOutOfRangeException) { } // don't write bogus or incomplete HTML } } } return(string.Empty); }
private static void WriteDigitalEndorsements(HtmlTextWriter tw, string szUser) { IEnumerable <Endorsement> rgEndorsements = Endorsement.EndorsementsForUser(szUser, null); if (rgEndorsements.Any()) { using (Page p = new FormlessPage()) { p.Controls.Add(new HtmlForm()); IEndorsementListUpdate el = (IEndorsementListUpdate)p.LoadControl("~/Controls/mfbEndorsement.ascx"); foreach (Endorsement en in rgEndorsements) { el.SetEndorsement(en); try { ((UserControl)el).RenderControl(tw); } catch (Exception ex) when(!(ex is OutOfMemoryException)) { } // don't write bogus or incomplete HTML } } } }
/// <summary> /// Creates/returns a memory stream containing a zip of a) an HTML file of images, and b) the thumbnails of the images, linked to Amazon. /// THE STREAM MUST BE CLOSED BY THE CALLER! /// </summary> /// <param name="activeBrand">The brand to use - null for current brand</param> /// <returns>A memory stream of flight images followed by any profile images</returns> public MemoryStream ZipOfImagesForUser(Brand activeBrand) { if (activeBrand == null) { activeBrand = Branding.CurrentBrand; } MemoryStream ms = new MemoryStream(); using (ZipFile zip = new ZipFile()) { System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter tw = new HtmlTextWriter(sw); tw.RenderBeginTag(HtmlTextWriterTag.Html); tw.RenderBeginTag(HtmlTextWriterTag.Head); tw.AddAttribute("href", Branding.ReBrand("http://%APP_URL%%APP_ROOT%/public/stylesheet.css", activeBrand)); tw.AddAttribute("rel", "stylesheet"); tw.AddAttribute("type", "text/css"); tw.RenderBeginTag(HtmlTextWriterTag.Link); tw.RenderEndTag(); // Link tw.RenderBeginTag(HtmlTextWriterTag.Title); tw.Write(HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.ImagesBackupTitle, User.UserFullName))); tw.RenderEndTag(); // Head tw.RenderBeginTag(HtmlTextWriterTag.Body); // Write out profile images tw.RenderBeginTag(HtmlTextWriterTag.H1); tw.Write(HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.ImagesBackupEndorsementsHeader, User.UserFullName))); tw.RenderEndTag(); // h1 ImageList il = new ImageList(MFBImageInfo.ImageClass.Endorsement, User.UserName); il.Refresh(true); foreach (MFBImageInfo mfbii in il.ImageArray) { zip.AddFile(System.Web.Hosting.HostingEnvironment.MapPath(mfbii.PathThumbnail), szThumbFolder); mfbii.ToHtml(tw); mfbii.UnCache(); } // Write out any digital endorsements too IEnumerable <Endorsement> rgEndorsements = Endorsement.EndorsementsForUser(User.UserName, null); if (rgEndorsements.Count() > 0) { Page p = new FormlessPage(); p.Controls.Add(new HtmlForm()); IEndorsementListUpdate el = (IEndorsementListUpdate)p.LoadControl("~/Controls/mfbEndorsement.ascx"); foreach (Endorsement en in rgEndorsements) { el.SetEndorsement(en); try { ((UserControl)el).RenderControl(tw); } catch { } // don't write bogus or incomplete HTML } } // And any BasicMed stuff IEnumerable <BasicMedEvent> lstBMed = BasicMedEvent.EventsForUser(User.UserName); foreach (BasicMedEvent bme in lstBMed) { ImageList ilBasicMed = new ImageList(MFBImageInfo.ImageClass.BasicMed, bme.ImageKey); ilBasicMed.Refresh(true); foreach (MFBImageInfo mfbii in ilBasicMed.ImageArray) { zip.AddFile(System.Web.Hosting.HostingEnvironment.MapPath(mfbii.PathThumbnail), szThumbFolder); mfbii.ToHtml(tw); mfbii.UnCache(); } } // Write out flight images tw.RenderBeginTag(HtmlTextWriterTag.H1); tw.Write(HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.ImagesBackupFlightsHeader, User.UserFullName))); tw.RenderEndTag(); // H1 // We'll get images from the DB rather than slamming the disk // this is a bit of a hack, but limits our queries const string szQ = @"SELECT f.idflight, img.* FROM Images img INNER JOIN flights f ON f.idflight=img.ImageKey WHERE f.username=?user AND img.VirtPathID=0 ORDER BY f.Date desc, f.idFlight desc"; DBHelper dbhImages = new DBHelper(szQ); Dictionary <int, List <MFBImageInfo> > dImages = new Dictionary <int, List <MFBImageInfo> >(); dbhImages.ReadRows((comm) => { comm.Parameters.AddWithValue("user", User.UserName); }, (dr) => { int idFlight = Convert.ToInt32(dr["idflight"], CultureInfo.InvariantCulture); List <MFBImageInfo> lstMFBii; if (dImages.ContainsKey(idFlight)) { lstMFBii = dImages[idFlight]; } else { dImages[idFlight] = lstMFBii = new List <MFBImageInfo>(); } lstMFBii.Add(MFBImageInfo.ImageFromDBRow(dr)); }); // Get all of the user's flights, including telemetry const int PageSize = 200; // get 200 flights at a time. int offset = 0; int iRow = 0; bool fCouldBeMore = true; while (fCouldBeMore) { FlightQuery fq = new FlightQuery(User.UserName); DBHelper dbhFlights = new DBHelper(LogbookEntry.QueryCommand(fq, offset, PageSize, true, LogbookEntry.LoadTelemetryOption.LoadAll)); dbhFlights.ReadRows((comm) => { }, (dr) => { LogbookEntry le = new LogbookEntry(dr, User.UserName, LogbookEntry.LoadTelemetryOption.LoadAll); le.FlightImages = (dImages.ContainsKey(le.FlightID)) ? dImages[le.FlightID].ToArray() : new MFBImageInfo[0]; // skip any flights here that don't have images, videos, or telemetry if (le.FlightImages.Length > 0 || le.Videos.Count > 0 || le.HasFlightData) { WriteFlightInfo(tw, zip, le); } iRow++; }); if (fCouldBeMore = (iRow == offset + PageSize)) { offset += PageSize; } } tw.RenderEndTag(); // Body tw.RenderEndTag(); // Html zip.AddEntry(Branding.ReBrand(String.Format(CultureInfo.InvariantCulture, "{0}.htm", Resources.LocalizedText.ImagesBackupFilename), activeBrand), sw.ToString()); zip.Save(ms); } return(ms); }