/// <summary> /// Directly posts a payment letter to C-Access as a CMO message. /// </summary> /// <param name="electionCycle">The election cycle context of the letter to post.</param> /// <param name="candidateID">The ID of the candidate receipient of the letter to post.</param> /// <param name="run">The payment run number.</param> /// <param name="creator">The network username of the user posting the letter.</param> /// <param name="title">The letter subject.</param> /// <param name="body">The letter body.</param> /// <param name="receiptEmail">The e-mail address for the recipient of open receipt e-mails if desired; otherwise, null to decline open receipts for this letter.</param> /// <param name="data">The raw binary data contents of the payment letter attachment.</param> /// <param name="name">The filename of the payment letter.</param> /// <param name="notify">Indicates whether to generate an e-mail notification to the campaign's C-Access accounts upon post.</param> /// <returns>The ID of the message if succesfully posted; otherwise, a negative value representing an error code.</returns> /// <exception cref="ArgumentNullException">Any parameters are null, except <paramref name="receiptEmail"/>.</exception> public int PostPaymentLetter(string electionCycle, string candidateID, byte run, string creator, string title, string body, string receiptEmail, byte[] data, string name, bool notify) { return(PostPaymentLetter(electionCycle, candidateID, run, creator, title, body, receiptEmail, notify, delegate(CmoMessage message) { return message != null && CmoAttachment.Add(candidateID, message.ID, data, name) != null; })); }
/// <summary> /// Adds a list item for an attachment to the attachments list. /// </summary> /// <param name="attachment">The attachment to add a list item for.</param> /// <param name="numbered">Indicates whether or not to number the list item.</param> private void AddAttachmentsListItem(CmoAttachment attachment) { ListItem item = new ListItem(string.Format("View Attachment {0}", attachment.DownloadName), string.Format("Attachments/{0}.{1}", Server.UrlEncode(attachment.DownloadName), attachment.Extension)); item.Attributes["class"] = string.Format("popup filetype {0}-file", attachment.Extension.ToLowerInvariant()); this.AttachmentsList.Items.Add(item); }
public ActionResult Attachment(string messageid, string id) { // check for valid, opened message var message = CmoMessage.GetMessage(messageid); if (message == null) { return(HttpNotFound()); } if (!message.IsOpened) { return(RedirectToAction(ActionName_Details, new { id = messageid })); } // find attachment and download var attachment = CmoAttachment.GetAttachment(string.Join("-", messageid, id)); if (attachment == null) { return(HttpNotFound()); } Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", attachment.FileName)); return(new FilePathResult("~/App_Data/SampleAttachment.pdf", "application/octet-stream") { FileDownloadName = attachment.FileName }); }
/// <summary> /// Deletes this instance from the persistence storage medium. /// </summary> /// <returns>true if this instance was deleted successfully; otherwise, false.</returns> public bool Delete(CmoAttachment attachment) { if (attachment == null) { return(false); } using (DataClient client = new DataClient()) { return(client.DeleteCmoAttachment(attachment)); } }
/// <summary> /// Obtains a <see cref="AddAttachmentEventHandler"/> to handle events for adding attachments to CMO messages. /// </summary> /// <param name="candidateID">The ID of the candidate whose attachment is being added.</param> /// <param name="path">The full UNC file path to the attachment.</param> /// <returns>A <see cref="AddAttachmentEventHandler"/> for adding the attachment located at <paramref name="path"/>.</returns> /// <exception cref="FileNotFoundException"><paramref name="path"/> is invalid or does not exist.</exception> private static Func <CmoMessage, bool> GetAddAttachmentEventHandler(string candidateID, string path) { if (!File.Exists(path)) { throw new FileNotFoundException(string.Format("Unable to access the specified file from UNC path \"{0}\". Please make sure the path is valid, the file exists, and the service has sufficient permissions to read it.", path), path); } string name = Path.GetFileName(path); return(delegate(CmoMessage message) { return message != null && CmoAttachment.Add(candidateID, message.ID, File.ReadAllBytes(path), name) != null; }); }
/// <summary> /// Attaches a file attachment to the message using the default CMO attachment repository. /// </summary> /// <param name="message">The CMO message to attach to.</param> /// <param name="data">The binary data contents of the attachment.</param> /// <param name="name">The filename of the attachment.</param> /// <returns>A <see cref="CmoAttachment"/> for the attachment if the file was attached successfully; otherwise, null.</returns> public CmoAttachment Attach(CmoMessage message, byte[] data, string name) { if (message.IsPosted) { return(null); } CmoAttachment attachment = this.AddCmoAttachment(message.CandidateID, message.ID, data, name); if (attachment != null) { message.Attachments.Add(attachment.ID, attachment); return(attachment); } return(null); }
/// <summary> /// Updates a CMO message attachment instance in the persistence storage medium by overwriting the existing record. /// </summary> /// <param name="attachment">The CMO message attachment to update.</param> /// <returns>true if this instance was saved successfuly; otherwise, false.</returns> public bool Update(CmoAttachment attachment) { using (Data.CmoEntities context = new Data.CmoEntities()) { var data = context.CmoAttachments.FirstOrDefault(a => a.CandidateId == attachment.CandidateID && a.MessageId == attachment.MessageID && a.AttachmentId == attachment.ID); if (data != null) { data.AttachmentName = attachment.FileName; try { return(context.SaveChanges() > 0); } catch (OptimisticConcurrencyException) { } } return(false); } }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="IHttpHandler"/> interface. /// </summary> /// <param name="context">An <see cref="HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { context.CheckPitStops(); // process query string for attachment string id = context.Request.Path; if (id.EndsWith("/")) { context.Server.RedirectPageNotFound(); } if (id.Contains("/")) { id = id.Substring(id.LastIndexOf('/') + 1); } if (id.Contains(".")) { id = id.Substring(0, id.IndexOf('.')); } CmoAttachment attachment = CmoAttachment.GetAttachment(id); if (attachment == null || !attachment.CandidateID.Equals(CPSecurity.Provider.GetCid(context.User.Identity.Name), StringComparison.InvariantCultureIgnoreCase)) { context.Server.RedirectPageNotFound(); } CmoMessage owner = CmoMessage.GetMessage(attachment.CandidateID, attachment.MessageID); if ((owner == null) || !owner.IsOpened) { // if owner message is not posted, open the message first context.Response.Redirect(string.Format("/Messages/View.aspx?id={0}", owner.UniqueID)); } else { context.Response.Clear(); context.Response.Expires = 0; context.Response.ContentType = context.Response.GetContentType(attachment.FileName); // BUGFIX #49 - content-disposition filename need to be surrounded by quotes context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", attachment.FileName)); context.Response.AddHeader("Content-Length", new FileInfo(attachment.Path).Length.ToString()); // BUGFIX #32 - KB823409: Response.WriteFile cannot download a large file so read/write file in chunks context.Response.TransmitFile(attachment.Path); } context.ApplicationInstance.CompleteRequest(); }
/// <summary> /// Adds an attachment to a CMO message. /// </summary> /// <param name="candidateID">The CFIS ID of the candidate the attachment is for.</param> /// <param name="messageID">The ID of the targeted message.</param> /// <param name="name">The attachment file name to be used for display.</param> /// <param name="saveAttachment">The delegate method to use for saving the attachment.</param> /// <returns>A <see cref="CmoAttachment"/> that represents the newly added attachment.</returns> private CmoAttachment AddCmoAttachment(string candidateID, int messageID, string name, SaveAttachmentEventHandler saveAttachment) { CmoMessage owner = GetCmoMessage(candidateID, messageID); if ((owner != null) && (CmoRepository.Repository != null)) { using (Data.CmoEntities context = new Data.CmoEntities()) { var ret = context.CmoSaveAttachment(candidateID, messageID, byte.MinValue, name).FirstOrDefault(); byte id; if (ret.HasValue && byte.TryParse(ret.Value.ToString(), out id) && id > byte.MinValue) { CmoAttachment attachment = new CmoAttachment(candidateID, messageID, id); attachment.FileName = name; try { if (saveAttachment != null && saveAttachment(attachment)) { return(attachment); } else { // rollback attachment if it could not be saved CmoRepository.Repository.Delete(attachment); Delete(attachment); } } catch (Exception e) { // rollback attachment if it could not be saved CmoRepository.Repository.Delete(attachment); Delete(attachment); throw e; } } } } return(null); }
/// <summary> /// Deletes a CMO message attachment instance from the persistence storage medium. /// </summary> /// <param name="attachment">The CMO message attachment to delete.</param> /// <returns>true if this instance was deleted successfully; otherwise, false.</returns> public bool Delete(CmoAttachment attachment) { if (attachment != null) { using (Data.CmoEntities context = new Data.CmoEntities()) { var data = context.CmoAttachments.FirstOrDefault(a => a.CandidateId == attachment.CandidateID && a.MessageId == attachment.MessageID && a.AttachmentId == attachment.ID); if (data != null) { context.DeleteObject(data); try { return(CmoRepository.Repository != null && context.SaveChanges() > 0 && CmoRepository.Repository.Delete(attachment)); } catch (OptimisticConcurrencyException) { return(false); } } } } return(true); }