nobugs

江湖无辈,哥们可能满目皆是; 社会有序,朋友也许屈指可数。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

当用户拷贝其它网站上的文章时,可能存在一些图片。在保存后,图片仍是链接该网站上,而会导致以后该连接失效的可能。所以增加在发新贴时,将远程图片抓到本地,改为本地链接。

 

其实涉及一些概念:
forums_Posts中Body和FormattdBody有什么区别?


Body:是存储用户上贴的真实内容
FormattedBody:顾名思义,是经过HTML语法格式化后的Body内容,可以直接输出到页面上.

但由于FTB可以使用HTML语法,这样导致了很多Body内容和FormattedBody内容是相同的.

主要目的是:以空间换时间(宝玉语)


Body是在编辑帖子的时候
FormattedBody是在显示帖子的时候


步骤:

1)、创建图片抓取类RemoteImageGather.cs
2)、在Posts.cs中的public static Post AddPost(Post post, User postAuthor)修改

post.Body = Formatter.FormatIrcCommands(post.Body, postAuthor.Nickname);

            //抓取贴子中的远程图片 add by zs.tan 2008-10-20
            post.Body = RemoteImageGather.RemoteImagesSave( Formatter.FormatIrcCommands(post.Body, postAuthor.Nickname));


RemoteImageGather.cs的关键点是找出图片的URL
        /// <summary>
        /// 取HTML代码中的图片路径
        /// </summary>
        /// <param name="htmlCode"></param>
        /// <returns></returns>
        public static ArrayList GetImageLinks(string htmlCode)
        {
            ArrayList al = new ArrayList();

            //匹配全路径
            Match match = Regex.Match(htmlCode, @"http(s)?://+(((/?)+[\w-.]+(/))*)+[\w-./]+\.+(jpg|jpeg|png|bmp|gif)", RegexOptions.IgnoreCase);

            while (match.Success)
            {
                //过滤相同的图片
                if (!al.Contains(match.Value)) al.Add(match.Value);
                match = match.NextMatch();
            }

            return al;

        }

posted on 2008-10-22 15:38  Zero.Tan  阅读(398)  评论(0编辑  收藏  举报