public void GetAttachmentReturnsNamedAttachment() { var log = new StructuredDocument(); var data = new AttachmentData("foo", MimeTypes.Binary, AttachmentType.Binary, null, new byte[0]); log.Attachments.Add(data); log.Attachments.Add(new AttachmentData("bar", MimeTypes.Binary, AttachmentType.Binary, null, new byte[0])); Assert.AreSame(data, log.GetAttachment("foo")); }
public void FromAttachmentData_Text() { var attachmentData = new AttachmentData("name", MimeTypes.PlainText, AttachmentType.Text, "content", null); var attachment = (TextAttachment)Attachment.FromAttachmentData(attachmentData); Assert.AreEqual("name", attachment.Name); Assert.AreEqual(MimeTypes.PlainText, attachment.ContentType); Assert.AreEqual("content", attachment.Text); }
public void FromAttachmentData_Binary() { byte[] bytes = new byte[] { 1, 2, 3 }; var attachmentData = new AttachmentData("name", MimeTypes.Binary, AttachmentType.Binary, null, bytes); var attachment = (BinaryAttachment)Attachment.FromAttachmentData(attachmentData); Assert.AreEqual("name", attachment.Name); Assert.AreEqual(MimeTypes.Binary, attachment.ContentType); Assert.AreEqual(bytes, attachment.Bytes); }
/// <summary> /// Recovers the attachment information from serializable attachment data. /// </summary> /// <param name="data">The attachment data.</param> /// <returns>The attachment.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="data"/> is null.</exception> public static Attachment FromAttachmentData(AttachmentData data) { if (data == null) { throw new ArgumentNullException("data"); } return((data.Type == AttachmentType.Text) ? (Attachment) new TextAttachment(data.Name, data.ContentType, data.GetText()) : (Attachment) new BinaryAttachment(data.Name, data.ContentType, data.GetBytes())); }
private void SaveAttachmentContents(AttachmentData attachmentData, string attachmentPath) { var encoding = new UTF8Encoding(false); using (Stream attachmentStream = reportContainer.OpenWrite(attachmentPath, attachmentData.ContentType, encoding)) attachmentData.SaveContents(attachmentStream, encoding); }
public static void AreEqual(AttachmentData expected, AttachmentData actual) { if (expected == null) { Assert.IsNull(actual); return; } Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.ContentType, actual.ContentType); Assert.AreEqual(expected.Type, actual.Type); Assert.AreEqual(expected.ContentPath, actual.ContentPath); Assert.AreEqual(expected.SerializedContents, actual.SerializedContents); }
private static string GetAttachmentPath(string stepId, AttachmentData attachment) { string fileName = FileUtils.EncodeFileName(attachment.Name); string extension = MimeTypes.GetExtensionByMimeType(attachment.ContentType); if (extension != null) fileName += extension; return Path.Combine(ReportName, Path.Combine(FileUtils.EncodeFileName(stepId), fileName)); }
private FileInfo GetAttachmentFileInfo(string stepId, AttachmentData attachment) { return cacheGroup.GetFileInfo(GetAttachmentPath(stepId, attachment)); }
private void SaveAttachment(string stepId, AttachmentData attachmentData) { string attachmentPath = GetAttachmentPath(stepId, attachmentData); if (attachmentPaths.Contains(attachmentPath)) return; attachmentPaths.Add(attachmentPath); using (Stream fs = cacheGroup.OpenFile(attachmentPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) attachmentData.SaveContents(fs, Encoding.Default); }
private void LoadAttachmentContents(AttachmentData attachmentData, string attachmentPath) { using (Stream attachmentStream = reportContainer.OpenRead(attachmentPath)) { // TODO: How should we handle missing attachments? Currently we just throw an exception. try { attachmentData.LoadContents(attachmentStream); } catch (Exception ex) { throw new IOException(String.Format(CultureInfo.CurrentCulture, "Unable to load report attachment from file: '{0}'.", attachmentPath), ex); } } }