shawnwildermuth/MetaWeblog

How to use it?

nj-sketch opened this issue · 2 comments

How can I use it. It would be great if you could add a test case on implementing it and its advantages. I found it used on https://github.com/madskristensen/Miniblog.Core but just can't identify how it benefits or how actually it works in his project.

It's only useful if you need to support Metaweblog API for blog authoring (e.g. for use with LiveWriter or MarkdownMonster). If you're not implementing Metaweblog API, then there is no need. The readme shows an example of how to implement, and you can see a fully functional example at my shawnwildermuth/wilderblog example.

@supernarwen Thanks for finding the Miniblog.Core implementation.

Below is a simple implementation using the file system for testing purposes.

public class MetaWeblogService : IMetaWeblogProvider
{
    private readonly string _postsPath = @"C:\Content\Posts";
    private readonly string _mediaPath = @"C:\Content\Media";

    public UserInfo GetUserInfo(string key, string username, string password)
    {
        AuthUser(username, password);
        throw new NotImplementedException();
    }

    public CategoryInfo[] GetCategories(string blogid, string username, string password)
    {
        AuthUser(username, password);
        throw new NotImplementedException();
    }

    public int AddCategory(string key, string username, string password, NewCategory category)
    {
        AuthUser(username, password);
        throw new NotImplementedException();
    }

    public BlogInfo[] GetUsersBlogs(string key, string username, string password)
    {
        AuthUser(username, password);

        return new[] { new BlogInfo {
            blogid = "Default",
            blogName = "Example",
            url = "https://www.example.com/"
        }};
    }

    public Post[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
    {
        AuthUser(username, password);

        var posts = new List<Post>();

        var di = new DirectoryInfo(_postsPath);
        foreach (var fi in di.GetFiles("*.htm").OrderByDescending(f => f.CreationTime).Take(numberOfPosts))
            posts.Add(LoadPost(fi));

        return posts.ToArray();
    }

    public Post GetPost(string postid, string username, string password)
    {
        AuthUser(username, password);

        string path = Path.Combine(_postsPath, "Drafts", postid + ".htm");
        if (File.Exists(path))
            return LoadPost(new FileInfo(path));
        else
            return LoadPost(new FileInfo(Path.Combine(_postsPath, postid + ".htm")));
    }

    public string AddPost(string blogid, string username, string password, Post post, bool publish)
    {
        AuthUser(username, password);

        string postid = Guid.NewGuid().ToString();

        string path = Path.Combine(Path.Combine(_postsPath, publish ? String.Empty : "Drafts"), postid + ".htm");
        File.WriteAllText(path, $"<h1>{post.title}</h1>\r\n{post.description}");

        return postid;
    }

    public bool EditPost(string postid, string username, string password, Post post, bool publish)
    {
        AuthUser(username, password);

        string path = Path.Combine(_postsPath, publish ? String.Empty : "Drafts", postid + ".htm");
        File.WriteAllText(path, $"<h1>{post.title}</h1>\r\n{post.description}");

        if (publish)
        {
            path = Path.Combine(_postsPath, "Drafts", postid + ".htm");
            if (File.Exists(path))
                File.Delete(path);
        }

        return true;
    }

    public bool DeletePost(string key, string postid, string username, string password, bool publish)
    {
        AuthUser(username, password);

        string path = Path.Combine(_postsPath, "Drafts", postid + ".htm");
        if (File.Exists(path))
            File.Delete(path);

        path = Path.Combine(_postsPath, postid + ".htm");
        if (File.Exists(path))
            File.Delete(path);

        return true;
    }

    public MediaObjectInfo NewMediaObject(string blogid, string username, string password, MediaObject mediaObject)
    {
        AuthUser(username, password);

        string file = Guid.NewGuid() + Path.GetExtension(mediaObject.name);

        byte[] bytes = Convert.FromBase64String(mediaObject.bits);
        File.WriteAllBytes(Path.Combine(_mediaPath, file), bytes);

        return new MediaObjectInfo { url = "/media/" + file };
    }

    private void AuthUser(string username, string password)
    {
        if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
            throw new MetaWeblogException("Unauthorized");
    }

    private Post LoadPost(FileInfo fi)
    {
        string[] lines = File.ReadAllLines(fi.FullName);
        var m = Regex.Match(lines[0], @"<h1>(.*?)<\/h1>");
        string id = Path.GetFileNameWithoutExtension(fi.Name);

        return new Post
        {
            postid = id,
            dateCreated = fi.LastWriteTime,
            title = m.Groups[1].Value,
            description = String.Join(Environment.NewLine, lines.Skip(1))
        };
    }
}