#region File History /// /// Name : Photos.cs /// History : /// 013565 - 10.02.07 - Created - Gareth Durant. /// #endregion File History #region Using using System; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.IO; using Frontier.Communication; using Frontier.Communication.GTR; using Frontier.Communication.EventArguments; using Frontier.Communication.Settings; using Frontier.Consumables.Composite; #endregion Using namespace Frontier.Consumables.Composite { /// Photo Compound /// /// Used to view the default staff photo attached to the Staff Details - Details form [DET] /// within the BRE. /// /// Attachment /// My remarks public class Photo : Panel { #region " Member Variables " private StringBuilder _returnedHex; public StringBuilder ReturnedHex { get { return _returnedHex; } set { _returnedHex = value; } } private bool _photoExists; public bool PhotoExists { get { return _photoExists; } set { _photoExists = value; } } private string _photoID; public string PhotoID { get { return _photoID; } set { _photoID = value; } } private string _photoDescription; public string PhotoDescription { get { return _photoDescription; } set { _photoDescription = value; } } private string _photoSize; public string PhotoSize { get { return _photoSize; } set { _photoSize = value; } } private int _photoWidth = 150; public int PhotoWidth { get { return _photoWidth; } set { _photoWidth = value; } } private int _photoHeight = 150; public int PhotoHeight { get { return _photoHeight; } set { _photoHeight = value; } } private string _photoExtension; public string PhotoExtension { get { return _photoExtension; } set { _photoExtension = value; } } private string _photoPath; public string PhotoPath { get { return _photoPath; } set { _photoPath = value; } } private string _physicalPath; public string PhysicalPath { get { return _src; } set { _physicalPath = value; } } private string _src; public string Src { get { return _src; } set { _src = value; } } private string _formMnemonic; public string FormMnemonic { get { return _formMnemonic; } set { _formMnemonic = value.ToLower(); } } private string _user; public string User { get { return _user; } set { _user = value; } } private string _fileName; public string FileName { get { return _fileName; } set { _fileName = value; } } #endregion " Member Variables " #region " Constructor " public Photo(string formMnemonic, string detNumber) { _returnedHex = new StringBuilder(); _photoID = string.Empty; _photoDescription = string.Empty; _photoSize = string.Empty; _photoExtension = string.Empty; _photoPath = "metadata/attachment"; _physicalPath = string.Empty; _src = string.Empty; _formMnemonic = formMnemonic.ToLower(); _user = detNumber; this.ID = "Photo_" + _user; this.CssClass = "Photo"; this.Style.Add("text-align", "center"); sendGTR(_formMnemonic + "att"); if (this.PhotoExists) { createFile(); } } public Photo() { _returnedHex = new StringBuilder(); _photoID = string.Empty; _photoDescription = string.Empty; _photoSize = string.Empty; _photoExtension = string.Empty; _photoPath = "metadata/attachment"; _physicalPath = string.Empty; _src = string.Empty; _formMnemonic = "det"; _user = UserSettings.ThisInstance.sUser; this.ID = "Photo_" + _user; this.CssClass = "Photo"; this.Style.Add("text-align", "center"); sendGTR("detatt"); if (this.PhotoExists) { createFile(); } else { LiteralControl iFrame = new LiteralControl(""); this.Controls.Add(iFrame); } } #endregion " Constructor " #region " Private Methods " private void createFile() { string src = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Photo"); _fileName = this.User + "_" + this.PhotoDescription + "." + this.PhotoExtension; _src = src + "//" + _fileName; if (!File.Exists(this.PhysicalPath)) { sendGTR("repositorytransfer"); string hex = this.ReturnedHex.ToString(); try { FileStream fs = File.Create(this.PhysicalPath); BinaryWriter bw = new BinaryWriter(fs); byte[] buffer = new Byte[1]; for (int i = 0; i < hex.Length; i++) { if ((i & 1) == 0) { buffer[0] = Convert.ToByte(hex.Substring(i, 2), 16); bw.Write(buffer, 0, buffer.Length); } } bw.Close(); fs.Close(); } catch (Exception ex) { Frontier.Communication.Utilities.Utilities.ErrorLog(ex); this.PhotoExists = false; } } } /// /// /// private void RespondWithPhoto(HttpResponse response) { sendGTR("repositorytransfer"); string hex = this.ReturnedHex.ToString(); string contentType = contentType = "image/" + this.PhotoExtension; try { response.Clear(); response.ContentType = contentType; response.Charset = ""; response.AddHeader("Content-Disposition", "attachment; filename=" + this.PhotoDescription + "." + this.PhotoExtension); response.AddHeader("Content-Length", this.PhotoSize); byte[] buffer = new Byte[1]; for (int i = 0; i < hex.Length; i += 2) { buffer[0] = Convert.ToByte(hex.Substring(i, 2), 16); response.OutputStream.Write(buffer, 0, buffer.Length); } response.End(); } catch (System.Threading.ThreadAbortException taEx) { throw taEx; } catch (Exception ex) { this.PhotoExists = false; Frontier.Communication.Utilities.Utilities.ErrorLog(ex); response.Write("

Exception has occurred.

"); // CAT.NET. Do not show the user specific exception information. } } private void sendGTR(string gtrType) { GTRAdapter gtrHolder; gtrHolder = new GTRAdapter(); switch (gtrType) { case "repositorytransfer": { gtrHolder.SetGTRField("cbr", gtrType); gtrHolder.SetGTRField("process", "fetch"); gtrHolder.SetGTRField("reference", this.PhotoID); gtrHolder.SetGTRField("firstsegment", "1"); double _segmentSize = 3072; int _fileSize = (int)Convert.ToInt64(this.PhotoSize); double tempSegments = _fileSize / _segmentSize; int lastSegment = (int)Math.Ceiling(tempSegments); gtrHolder.SetGTRField("lastsegment", lastSegment.ToString()); } break; default: { gtrHolder.SetGTRField("cbr", this.FormMnemonic + "att"); if (this.FormMnemonic == "apd") gtrHolder.SetGTRField("apdnumber", this.User); else gtrHolder.SetGTRField("detnumber", this.User); gtrHolder.SetGTRField("filter01", "fr2default:eq:Y:"); } break; } gtrHolder.SendGTR(); interpretGTR(gtrType, gtrHolder); gtrHolder = null; return; } private void interpretGTR(string gtrType, GTRAdapter gtrContainer) { if (gtrContainer.IsStatusNotOK) { this.PhotoExists = false; } else if (gtrContainer.ReturnedGTRContainer.Count > 0) { switch (gtrType) { case "repositorytransfer": { foreach (GTRContainer container in gtrContainer.ReturnedGTRContainer) { this.ReturnedHex.Append(container.GetGTRField("hexdata")); } } break; default: { foreach (GTRContainer container in gtrContainer.ReturnedGTRContainer) { this.PhotoID = container.GetGTRField("fr2fileid"); this.PhotoDescription = container.GetGTRField("fr2descript"); this.PhotoSize = container.GetGTRField("fr2filesize"); this.PhotoExtension = container.GetGTRField("fr2fileext"); this.PhotoExists = true; } } break; } } } protected override void Render(HtmlTextWriter writer) { // The Photo class produces a div element that contains an image. // So the resultant HTML looks like this: //
// To do this we have to create the thumbnail class which will // stream the file we have written back to the browser. if (!string.IsNullOrEmpty(this.Src) && File.Exists(this.Src)) { //this.Controls.Add(new LiteralControl(""
                //            + this.PhotoDescription + "")); Image thumbnail = new Image(); thumbnail.ImageUrl = "../images/Thumbnail.aspx?file=" + this.Src; thumbnail.Attributes["style"] = "border:solid black 1px;"; thumbnail.Width = Unit.Pixel(PhotoWidth); thumbnail.Height = Unit.Pixel(PhotoHeight); thumbnail.Style.Add("margin-top", "3px"); this.Controls.Add(thumbnail); } base.Render(writer); } // This is setup as a constructor, but it never returns. Instead, if successful, it sends back a HttpResponse & aborts the thread. public Photo(HttpResponse response, string formMnemonic) { string detNumber = System.Web.HttpContext.Current.Session["CURRENTLYSELECTEDSTAFF"] as string; if (string.IsNullOrEmpty(detNumber)) { Frontier.Communication.Settings.UserSettings oUser = HttpContext.Current.Session["oUser"] as Frontier.Communication.Settings.UserSettings; if (oUser != null) detNumber = oUser.sUser; } _returnedHex = new StringBuilder(); _photoID = string.Empty; _photoDescription = string.Empty; _photoSize = string.Empty; _photoExtension = string.Empty; _photoPath = "metadata/attachment"; _physicalPath = string.Empty; _src = string.Empty; _formMnemonic = formMnemonic.ToLower(); _user = detNumber; sendGTR(_formMnemonic + "att"); if (this.PhotoExists) { RespondWithPhoto(response); } } #endregion " Private Methods " } }