⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metawebloghandler.cs

📁 个人博客系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
            string serverPath = _request.Server.MapPath(rootPath);
            string saveFolder = serverPath;
            string fileName = mediaObject.name;

            // Check/Create Folders & Fix fileName
            if (mediaObject.name.LastIndexOf('/') > -1)
            {
                saveFolder += mediaObject.name.Substring(0, mediaObject.name.LastIndexOf('/'));
                saveFolder = saveFolder.Replace('/', '\\');


                fileName = mediaObject.name.Substring(mediaObject.name.LastIndexOf('/') + 1);
            }
            else
            {
                if (saveFolder.EndsWith("\\"))
                    saveFolder = saveFolder.Substring(0, saveFolder.Length - 1);
            }
            if (!Directory.Exists(saveFolder))
                Directory.CreateDirectory(saveFolder);
            saveFolder += "\\";

            // Save File
            FileStream fs = new FileStream(saveFolder + fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(mediaObject.bits);
            bw.Close();

            // Set Url
            string rootUrl = _request.Request.Url.ToString().Substring(0, _request.Request.Url.ToString().IndexOf("metaweblog.axd"));

            string mediaType = mediaObject.type;
            if (mediaType.IndexOf('/') > -1)
                mediaType = mediaType.Substring(0, mediaType.IndexOf('/'));
            switch (mediaType)
            {
                case "image":
                case "notsent": // If there wasn't a type, let's pretend it is an image.  (Thanks Zoundry.  This is for you.)
                    rootUrl += "image.axd?picture=";
                    break;
                default:
                    rootUrl += "file.axd?file=";
                    break;
            }

            mediaInfo.url = rootUrl + mediaObject.name;
            return mediaInfo;
        }

        /// <summary>
        /// metaWeblog.getCategories
        /// </summary>
        /// <param name="blogID">always 1000 in BlogEngine since it is a singlar blog instance</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <returns>array of category structs</returns>
        public List<MWACategory> GetCategories(string blogID, string userName, string password)
        {
            List<MWACategory> categories = new List<MWACategory>();

            ValidateRequest(userName, password);

            string rootUrl = _request.Request.Url.ToString().Substring(0, _request.Request.Url.ToString().IndexOf("metaweblog.axd"));

            foreach (Category cat in Category.Categories)
            {
                MWACategory temp = new MWACategory();
                temp.title = cat.Title;
                temp.description = cat.Description;
                temp.htmlUrl = rootUrl + "category/" + cat.Title + ".aspx";
                temp.rssUrl = rootUrl + "category/syndication.axd?category=" + cat.Id.ToString();
                categories.Add(temp);

            }

            return categories;
        }

        /// <summary>
        /// metaWeblog.getRecentPosts
        /// </summary>
        /// <param name="blogID">always 1000 in BlogEngine since it is a singlar blog instance</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <param name="numberOfPosts">number of posts to return</param>
        /// <returns>array of post structs</returns>
        public List<MWAPost> GetRecentPosts(string blogID, string userName, string password, int numberOfPosts)
        {
            ValidateRequest(userName, password);

            List<MWAPost> sendPosts = new List<MWAPost>();
            List<Post> posts = Post.Posts;

            // Set End Point
            int stop = numberOfPosts;
            if (stop > posts.Count)
                stop = posts.Count;

            foreach (Post post in posts.GetRange(0, stop))
            {
                MWAPost tempPost = new MWAPost();
                List<string> tempCats = new List<string>();
                List<string> tempTags = new List<string>();

                tempPost.postID = post.Id.ToString();
                tempPost.postDate = post.DateCreated;
                tempPost.title = post.Title;
                tempPost.description = post.Content;
                tempPost.link = post.AbsoluteLink.AbsoluteUri; //post.PermaLink.AbsoluteUri;
                tempPost.publish = post.IsPublished;
                for (int i = 0; i < post.Categories.Count; i++)
                {
                    tempCats.Add(Category.GetCategory(post.Categories[i].Id).ToString());
                }
                tempPost.categories = tempCats;

                for (int i = 0; i < post.Tags.Count; i++)
                {
                    tempTags.Add(post.Tags[i]);
                }
                tempPost.tags = tempTags;

                sendPosts.Add(tempPost);

            }

            return sendPosts;
        }

        /// <summary>
        /// blogger.getUsersBlogs
        /// </summary>
        /// <param name="appKey">Key from application.  Outdated methodology that has no use here.</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <returns>array of blog structs</returns>
        public List<MWABlogInfo> GetUserBlogs(string appKey, string userName, string password)
        {
            List<MWABlogInfo> blogs = new List<MWABlogInfo>();

            ValidateRequest(userName, password);

            MWABlogInfo temp = new MWABlogInfo();
            temp.url = _request.Request.Url.ToString().Substring(0, _request.Request.Url.ToString().IndexOf("metaweblog.axd"));
            temp.blogID = "1000";
            temp.blogName = BlogSettings.Instance.Name;
            blogs.Add(temp);

            return blogs;
        }

        /// <summary>
        /// blogger.deletePost
        /// </summary>
        /// <param name="appKey">Key from application.  Outdated methodology that has no use here.</param>
        /// <param name="postID">post guid in string format</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <param name="publish">mark as published?</param>
        /// <returns></returns>
        public bool DeletePost(string appKey, string postID, string userName, string password, bool publish)
        {
            ValidateRequest(userName, password);
            try
            {
                Post post = Post.GetPost(new Guid(postID));
                post.Delete();
                post.Save();
            }
            catch (Exception ex)
            {
                throw new MetaWeblogException("12", "DeletePost failed.  Error: " + ex.Message);
            }

            return true;
        }

        /// <summary>
        /// blogger.getUserInfo
        /// </summary>
        /// <remarks>
        /// This is never called in anything I've tested.
        /// </remarks>
        /// <param name="appKey">Key from application.  Outdated methodology that has no use here.</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <returns>struct with user data</returns>
        public MWAUserInfo GetUserInfo(string appKey, string userName, string password)
        {
            throw new MetaWeblogException("10", "The method GetUserInfo is not implemented.");
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Checks username and password.  Throws error if validation fails.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        private void ValidateRequest(string userName, string password)
        {
            if (!Membership.ValidateUser(userName, password))
            {
                throw new MetaWeblogException("11", "User authentication failed");
            }
        }

        /// <summary>
        /// Returns Category Guid from Category name.
        /// </summary>
        /// <remarks>
        /// Reverse dictionary lookups are ugly.
        /// </remarks>
        /// <param name="name"></param>
        /// <param name="cat"></param>
        /// <returns></returns>
        private bool LookupCategoryGuidByName(string name, out Category cat)
        {
            cat = new Category();
            foreach (Category item in Category.Categories)
            {
                if (item.Title == name)
                {
                    cat = item;
                    return true;
                }
            }
            return false;
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -