News

Laurent Kempé MVP JetBrains Academy Member

Contact

My status

View Laurent Kempé's profile on LinkedIn
XING
twitter
facebook


Xbox 360



Map

Locations of visitors to this page

.NET Dudes

Family

French .NET Dudes

Friends

Links

Tech Head Brothers

C# implementation of newMediaObject for the MetaWeblog API

If you have a blog you might know about the MetaWeblog API. I implemented it for Tech Head Brothers portal to be able to post news from a client. Today I am using Live Writer to post on my blog and I also wanted to have the possibility to post news on the new version of Tech Head Brothers portal but with pictures and without the usage of a ftp server.

Checking the API I found a new method that I had not implemented: newMediaObject.

metaWeblog.newMediaObject (blogid, username, password, struct) returns struct

The blogid, username and password params are as in the Blogger API.

The struct must contain at least three elements, name, type and bits.

name is a string, it may be used to determine the name of the file that stores the object, or to display it in a list of objects. It determines how the weblog refers to the object. If the name is the same as an existing object stored in the weblog, it may replace the existing object.

type is a string, it indicates the type of the object, it's a standard MIME type, like audio/mpeg or image/jpeg or video/quicktime.

bits is a base64-encoded binary value containing the content of the object.

The struct may contain other elements, which may or may not be stored by the content management system.

If newMediaObject fails, it throws an error. If it succeeds, it returns a struct, which must contain at least one element, url, which is the url through which the object can be accessed. It must be either an FTP or HTTP url.

I defined in the interface two struct as following:

public struct MediaObjectUrl
{
    public string url;
}

public struct MediaObject
{
    public string name;
    public string type;
    public byte[] bits;
}

Added the method in the IMetaWeblog interface:

[XmlRpcMethod("metaWeblog.newMediaObject",
    Description="Add a media object to a post using the "
                + "metaWeblog API. Returns media url as a string.")]
MediaObjectUrl newMediaObject(
    string blogid,
    string username,
    string password,
    MediaObject mediaObject);

And finally the following implementation:

/// <summary>
/// Post a media object.
/// </summary>
/// <param name="blogid">The blogid.</param>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="mediaObject">The media object.</param>
/// <returns>MediaObjectUrl  defining the url of the media</returns>
public MediaObjectUrl newMediaObject(string blogid, 
                                     string username, 
                                     string password, 
                                     MediaObject mediaObject)
{
    if (!ValidUser(username, password))
        throw new XmlRpcFaultException(0, "You have no right to do that.");
 
    string filename = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, 
                                   "images/" + mediaObject.name);
 
    if (!Directory.Exists(Path.GetDirectoryName(filename)))
        Directory.CreateDirectory(Path.GetDirectoryName(filename));
 
    File.WriteAllBytes(filename, mediaObject.bits);
 
    MediaObjectUrl mediaObjectUrl = new MediaObjectUrl();
    mediaObjectUrl.url = ConfigurationManager.AppSettings["BlogUrl"] + 
                         "/images/" + 
                         mediaObject.name;
 
    return mediaObjectUrl;
}

The good point now is that I am able to let the authors of the site post news with embeded pictures without managing a ftp server. 

Comments

Tiernans Comms Closet said:

The MetaWeblog API is one of those interesting APIs that a lot of people couldn't live without (as a...

# August 27, 2006 6:20 PM

robrohr.org said:

Testing graphic upload capabilities via newMediaObject handler for Community Server. We shall see whether

# September 8, 2006 12:23 PM

Community Server Daily News said:

news of the day a grab bag for what's happening in Community Server Anyone whose been part of the Community

# September 18, 2006 4:05 PM

Alpha said:

if (!ValidUser(username, password))

Or

if(!ValidateUser(username,password))??

When I use LiveWriter,i have Error.

System.UriFormatException: 无效的 URI: 无法确定 URI 的格式。

在 System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)

在 System.Uri..ctor(String uriString)

在 WindowsLive.Writer.BlogClient.WeblogBlogFileUploader.UploadFile(FileInfo file)

在 WindowsLive.Writer.PostEditor.ImageServices.BlogFileUploadImageService.UploadImages(IUploadImageContext uploadImageContext)

在 WindowsLive.Writer.PostEditor.BlogPostReferenceFixer.ImageUploadWorker.DoUploadWork()

在 WindowsLive.Writer.PostEditor.BlogPostReferenceFixer.LocalFileTransformer(String reference)

在 WindowsLive.Writer.CoreServices.HTML.HtmlReferenceFixer.LocalFileReferenceFixupFilter.FixReferences(String reference)

在 WindowsLive.Writer.CoreServices.HTML.HtmlReferenceFixer.OnBeginTag(BeginTag tag)

在 WindowsLive.Writer.CoreServices.LightWeightHTMLDocumentIterator.Parse()

在 WindowsLive.Writer.CoreServices.HTML.HtmlReferenceFixer.FixLocalFileReferences(String html, ReferenceFixer fixer)

在 WindowsLive.Writer.PostEditor.UpdateWeblogAsyncOperation.LocalSupportingFileUploader.UploadFiles()

在 WindowsLive.Writer.PostEditor.UpdateWeblogAsyncOperation.DoWork()

在 WindowsLive.Writer.CoreServices.AsyncOperation.InternalStart()

在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)

在 System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)

在 System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)

# October 2, 2006 11:41 AM

Kevin Harder said:

I'm back from my annual blogging vacation! I'm sure everyone wants to know all the super exciting things

# November 13, 2006 12:54 PM

Herman Ng said:

I'm writing MetaWeblog API from Windows Live Space. We just realize that Space doesn't come with newMediaObject method in their live space.

Do I must go for WindowsLive.Writer ?

# August 19, 2008 4:29 AM

lkempe said:

Herman: Sorry but I don't get your question! I don't know if Windows Live Space supports MetaWeblog API, and neither know if you can upload binary content using Live Writer to Live space.

# August 19, 2008 5:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)