📄 metawebloghandler.cs
字号:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Xml;
using BlogEngine.Core;
namespace BlogEngine.Core.API.MetaWeblog
{
/// <summary>
/// HTTP Handler for MetaWeblog API
/// </summary>
public class MetaWeblogHandler : IHttpHandler, IMetaWeblogAPI
{
private HttpContext _request;
#region IHttpHandler Members
/// <summary>
/// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"></see> instance.
/// </summary>
/// <value></value>
/// <returns>true if the <see cref="T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
public bool IsReusable
{
get { return false; }
}
/// <summary>
/// Process the HTTP Request. Create XMLRPC request, find method call, process it and create response object and sent it back.
/// This is the heart of the MetaWeblog API
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
_request = context;
try
{
XMLRPCRequest input = new XMLRPCRequest(context);
XMLRPCResponse output = new XMLRPCResponse(input.MethodName);
switch (input.MethodName)
{
case "metaWeblog.newPost":
output.PostID = NewPost(input.BlogID, input.UserName, input.Password, input.Post, input.Publish);
break;
case "metaWeblog.editPost":
output.Completed = EditPost(input.PostID, input.UserName, input.Password, input.Post, input.Publish);
break;
case "metaWeblog.getPost":
output.Post = GetPost(input.PostID, input.UserName, input.Password);
break;
case "metaWeblog.newMediaObject":
output.MediaInfo = NewMediaObject(input.BlogID, input.UserName, input.Password, input.MediaObject);
break;
case "metaWeblog.getCategories":
output.Categories = GetCategories(input.BlogID, input.UserName, input.Password);
break;
case "metaWeblog.getRecentPosts":
output.Posts = GetRecentPosts(input.BlogID, input.UserName, input.Password, input.NumberOfPosts);
break;
case "blogger.getUsersBlogs":
case "metaWeblog.getUsersBlogs":
output.Blogs = GetUserBlogs(input.AppKey, input.UserName, input.Password);
break;
case "blogger.deletePost":
output.Completed = DeletePost(input.AppKey, input.PostID, input.UserName, input.Password, input.Publish);
break;
case "blogger.getUserInfo":
//TODO: Implement getUserInfo
throw new MetaWeblogException("10", "The method GetUserInfo is not implemented.");
}
output.Response(context);
}
catch (MetaWeblogException mex)
{
XMLRPCResponse output = new XMLRPCResponse("fault");
MWAFault fault = new MWAFault();
fault.faultCode = mex.Code;
fault.faultString = mex.Message;
output.Fault = fault;
output.Response(context);
}
catch (Exception ex)
{
XMLRPCResponse output = new XMLRPCResponse("fault");
MWAFault fault = new MWAFault();
fault.faultCode = "0";
fault.faultString = ex.Message;
output.Fault = fault;
output.Response(context);
}
}
#endregion
#region IMetaWeblogAPI Members
/// <summary>
/// metaWeblog.newPost
/// </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="sentPost">struct with post details</param>
/// <param name="publish">mark as published?</param>
/// <returns>postID as string</returns>
public string NewPost(string blogID, string userName, string password, MWAPost sentPost, bool publish)
{
ValidateRequest(userName, password);
Post post = new Post();
post.Author = userName;
post.Title = sentPost.title;
post.Content = sentPost.description;
post.IsPublished = publish;
post.Categories.Clear();
foreach (string item in sentPost.categories)
{
Category cat;
if (LookupCategoryGuidByName(item, out cat))
post.Categories.Add(cat);
}
post.Tags.Clear();
foreach (string item in sentPost.tags)
{
post.Tags.Add(item);
}
if (sentPost.postDate != new DateTime())
post.DateCreated = sentPost.postDate;
post.Save();
return post.Id.ToString();
}
/// <summary>
/// metaWeblog.editPost
/// </summary>
/// <param name="postID">post guid in string format</param>
/// <param name="userName">login username</param>
/// <param name="password">login password</param>
/// <param name="sentPost">struct with post details</param>
/// <param name="publish">mark as published?</param>
/// <returns>1 if successful</returns>
public bool EditPost(string postID, string userName, string password, MWAPost sentPost, bool publish)
{
ValidateRequest(userName, password);
Post post = Post.GetPost(new Guid(postID));
post.Author = userName;
post.Title = sentPost.title;
post.Content = sentPost.description;
post.IsPublished = publish;
post.Categories.Clear();
foreach (string item in sentPost.categories)
{
// Ignore categories not found (as per spec)
//Guid key;
Category cat;
if (LookupCategoryGuidByName(item, out cat))
post.Categories.Add(cat);
//if (LookupCategoryGuidByName(item, out key))
// post.Categories.Add(key);
}
post.Tags.Clear();
foreach (string item in sentPost.tags)
{
post.Tags.Add(item);
}
post.Save();
return true;
}
/// <summary>
/// metaWeblog.getPost
/// </summary>
/// <param name="postID">post guid in string format</param>
/// <param name="userName">login username</param>
/// <param name="password">login password</param>
/// <returns>struct with post details</returns>
public MWAPost GetPost(string postID, string userName, string password)
{
ValidateRequest(userName, password);
MWAPost sendPost = new MWAPost();
Post post = Post.GetPost(new Guid(postID));
sendPost.postID = post.Id.ToString();
sendPost.postDate = post.DateCreated;
sendPost.title = post.Title;
sendPost.description = post.Content;
sendPost.link = post.AbsoluteLink.AbsoluteUri;
sendPost.publish = post.IsPublished;
List<string> cats = new List<string>();
for (int i = 0; i < post.Categories.Count; i++)
{
cats.Add(Category.GetCategory(post.Categories[i].Id).ToString());
}
sendPost.categories = cats;
List<string> tags = new List<string>();
for (int i = 0; i < post.Tags.Count; i++)
{
tags.Add(post.Tags[i]);
}
sendPost.tags = tags;
return sendPost;
}
/// <summary>
/// metaWeblog.newMediaObject
/// </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="mediaObject">struct with media details</param>
/// <returns>struct with url to media</returns>
public MWAMediaInfo NewMediaObject(string blogID, string userName, string password, MWAMediaObject mediaObject)
{
ValidateRequest(userName, password);
MWAMediaInfo mediaInfo = new MWAMediaInfo();
string rootPath = BlogSettings.Instance.StorageLocation + "files/";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -