public void Import_WithBlogPostHavingBase64EncodedContentWithAttachments_ProperlyRewritesAttachments() { // arrange var blog = new BlogMLBlog(); const string originalPostContent = @"<img src=""http://old.example.com/images/my-mug.jpg"" />"; var post = new BlogMLPost { Content = BlogMLContent.Create(originalPostContent, ContentTypes.Base64) }; var attachment = new BlogMLAttachment { Url = "http://old.example.com/images/my-mug.jpg", Embedded = false }; post.Attachments.Add(attachment); blog.Posts.Add(post); var repository = new Mock <IBlogImportRepository>(); repository.Setup(r => r.GetAttachmentDirectoryUrl()).Returns("http://new.example.com/images/"); repository.Setup(r => r.GetAttachmentDirectoryPath()).Returns(@"c:\web\images"); BlogMLPost publishedPost = null; repository.Setup(r => r.CreateBlogPost(blog, post)).Callback <BlogMLBlog, BlogMLPost>((b, p) => publishedPost = p); var service = new BlogImportService(repository.Object); // act service.Import(blog); // assert Assert.AreEqual(ContentTypes.Base64, publishedPost.Content.ContentType); Assert.AreEqual(@"<img src=""http://new.example.com/images/my-mug.jpg"" />", publishedPost.Content.UncodedText); }
private BlogMLAttachment GetAttachment(string blogHostUrl, string attachmentUrl, string attachmentUrlLowerCase, bool embed) { string attachVirtualPath = attachmentUrlLowerCase.Replace(blogHostUrl, "/"); var attachment = new BlogMLAttachment { Embedded = embed, MimeType = attachmentUrl.GetMimeType(), Path = attachVirtualPath, Url = attachmentUrl }; if (embed) { try { SetAttachmentData(attachVirtualPath, attachment); } catch (FileNotFoundException e) { Log.Error("The attachment we wish to embed was not found", e); attachment.Embedded = false; } } return(attachment); }
public void Import_WithEmbeddedAttachments_CreatesFilesForAttachmentsAndRewritesBlogPost() { // arrange var data = new byte[] { 1, 2, 3 }; var attachment = new BlogMLAttachment { Url = "http://old.example.com/images/my-mug.jpg", Embedded = true, Data = data }; var post = new BlogMLPost { Content = new BlogMLContent { Text = @"<img src=""http://old.example.com/images/my-mug.jpg"" />" } }; post.Attachments.Add(attachment); var blog = new BlogMLBlog(); blog.Posts.Add(post); var repository = new Mock <IBlogImportRepository>(); repository.Setup(r => r.GetAttachmentDirectoryPath()).Returns(ImageDirectory + "/wlw"); repository.Setup(r => r.GetAttachmentDirectoryUrl()).Returns("http://example.com/images/wlw/"); var service = new BlogImportService(repository.Object); // act service.Import(blog); // assert Assert.IsTrue(File.Exists(Path.Combine(ImageDirectory, @"wlw\my-mug.jpg"))); Assert.AreEqual(@"<img src=""http://example.com/images/wlw/my-mug.jpg"" />", post.Content.UncodedText); }
public static string CreateFileFromAttachment(BlogMLAttachment attachment, string attachmentDirectoryPath, string attachmentDirectoryUrl, string postContent) { string fileName = Path.GetFileName(attachment.Url); string attachmentPath = HttpUtility.UrlDecode(Path.Combine(attachmentDirectoryPath, fileName)); string newAttachmentUrl = attachmentDirectoryUrl + fileName; postContent = BlogMLWriterBase.SgmlUtil.CleanAttachmentUrls( postContent, attachment.Url, newAttachmentUrl); if (attachment.Embedded) { if (!File.Exists(attachmentPath)) { using (var fStream = new FileStream(attachmentPath, FileMode.CreateNew)) { using (var writer = new BinaryWriter(fStream)) { writer.Write(attachment.Data); } } } } return(postContent); }
private void SetAttachmentData(string attachVirtualPath, BlogMLAttachment attachment) { string attachPhysicalPath = HttpUtility.UrlDecode(SubtextContext.HttpContext.Server.MapPath(attachVirtualPath)); using (FileStream attachStream = File.OpenRead(attachPhysicalPath)) { using (var reader = new BinaryReader(attachStream)) { reader.BaseStream.Position = 0; byte[] data = reader.ReadBytes((int)attachStream.Length); attachment.Data = data; } } }
public void Write_WithBlogContainingEmbeddedAttachmentsWithComments_WritesPostAttachmentsToWriter() { // arrange var stringWriter = new StringWriter(); var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented }; var source = new Mock <IBlogMLSource>(); var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture); var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime }; source.Setup(s => s.GetBlog()).Returns(blog); var post = new BlogMLPost { Title = "This is a blog post" }; var attachment = new BlogMLAttachment { Data = new byte[] { 1, 2, 3, 4, 5 }, Path = @"c:\\path-to-attachment.jpg", Url = "/foo/path-to-attachment.jpg", Embedded = true, MimeType = "img/jpeg" }; post.Attachments.Add(attachment); var posts = new List <BlogMLPost> { post }; source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts); var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/); // act ((IBlogMLWriter)writer).Write(xmlWriter); // assert string output = stringWriter.ToString(); Assert.Contains(output, @"external-uri=""c:\\path-to-attachment.jpg"""); Assert.Contains(output, @"url=""/foo/path-to-attachment.jpg"""); Assert.Contains(output, @"mime-type=""img/jpeg"""); Assert.Contains(output, @"embedded=""true"""); Assert.Contains(output, @"AQIDBAU=</attachment>"); }
public void CreateFileFromAttachment_WithOutEmbeddedAttachment_RewritesPostContent() { // arrange var attachment = new BlogMLAttachment { Url = "http://old.example.com/images/my-mug.jpg", Embedded = false }; string attachmentDirectoryPath = ImageDirectory; Directory.CreateDirectory(attachmentDirectoryPath); const string originalPostContent = @"<img src=""http://old.example.com/images/my-mug.jpg"" />"; // act string postContent = BlogImportService.CreateFileFromAttachment(attachment, attachmentDirectoryPath, "http://example.com/images/", originalPostContent); // assert Assert.AreEqual(@"<img src=""http://example.com/images/my-mug.jpg"" />", postContent); }
public void CreateFileFromAttachment_WithEmbeddedAttachment_CreatesFile() { // arrange var data = new byte[] { 1, 2, 3 }; var attachment = new BlogMLAttachment { Url = "http://old.example.com/images/my-mug.jpg", Embedded = true, Data = data }; string attachmentDirectoryPath = Path.Combine(Environment.CurrentDirectory, "images"); Directory.CreateDirectory(ImageDirectory); // act BlogImportService.CreateFileFromAttachment(attachment, attachmentDirectoryPath, "http://example.com/images/", "Some Content"); // assert Assert.IsTrue(File.Exists(Path.Combine(ImageDirectory, "my-mug.jpg"))); }