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; }
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"))); }
public void Add(BlogMLAttachment value) { base.Add(value); }
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 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 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; } } }
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; }
private static IList GetPostAttachments(BlogMLPost bmlPost, IBlogMLContext bmlContext) { IList attachments = new ArrayList(); string[] attachmentUrls = BlogMLWriterBase.SgmlUtil.GetAttributeValues(bmlPost.Content.Text, "img", "src"); if (attachmentUrls.Length > 0) { bool embed = bmlContext.EmbedAttachments; foreach (string attachmentUrl in attachmentUrls) { string blogHostUrl = Config.CurrentBlog.HostFullyQualifiedUrl.ToString().ToLower(CultureInfo.InvariantCulture); // If the URL for the attachment is local then we'll want to build a new BlogMLAttachment // add add it to the list of attchements for this post. if (BlogMLWriterBase.SgmlUtil.IsRootUrlOf(blogHostUrl, attachmentUrl.ToLower(CultureInfo.InvariantCulture))) { BlogMLAttachment attachment = new BlogMLAttachment(); string attachVirtualPath = attachmentUrl.Replace(blogHostUrl, "/"); // If we are embedding attachements then we need to get the data stream // for the attachment, else the datastream can be null. if (embed) { string attachPhysicalPath = HttpUtility.UrlDecode(HttpContext.Current.Server.MapPath(attachVirtualPath)); //using (Stream attachStream = new StreamReader(attachPhysicalPath).BaseStream) using (FileStream attachStream = File.OpenRead(attachPhysicalPath)) { using (BinaryReader reader = new BinaryReader(attachStream)) { reader.BaseStream.Position = 0; byte[] data = reader.ReadBytes((int)attachStream.Length); attachment.Data = data; } } } attachment.Embedded = embed; attachment.MimeType = BlogMLWriter.GetMimeType(attachmentUrl); attachment.Path = attachVirtualPath; attachment.Url = attachmentUrl; attachments.Add(attachment); } } } return attachments; }
/// <summary> /// The url to the attachment directory /// </summary> /// <remarks> /// The attachment is passed in to give the blog engine /// the opportunity to use attachment specific directories /// (ex. based on mime type) should it choose. /// </remarks> public override string GetAttachmentDirectoryUrl(BlogMLAttachment attachment) { return Config.CurrentBlog.ImagePath; }
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>"); }
/// <summary> /// The url to the attachment directory /// </summary> /// <remarks> /// The attachment is passed in to give the blog engine /// the opportunity to use attachment specific directories /// (ex. based on mime type) should it choose. /// </remarks> public abstract string GetAttachmentDirectoryUrl(BlogMLAttachment attachment);