Laurent Kempé
Implémentation en C# de la méthode newMediaObject de l'API MetaWeblog
Comment poster des fichiers binaires en utilisant un outil de blog tel que Live Writer. La solution, implémenter la méthode newMediaObject de l'API MetaWeblog.
Par Laurent Kempé publié le 15/11/2006 à 23:37, lu 9218 fois,
Si vous avez un blog et que vous aimez la technique vous connaissez sans aucun doute la MetaWeblog API. . Nous utilisons cette API afin de pouvoir publier de l'information sur Tech Head Brothers. Cela nous permet d'utiliser un outil de blog du genre Live Writer afin de poster de l'information sans utiliser un serveur ftp.
Le but de la méthode newMediaObject est de pouvoir attacher des fichiers de type binaire, images ou autres, à une publication.
Voici la documentation de cette méthode (en anglais) :
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.
Pour commencer nous définissons deux interfaces comme cela :

public struct MediaObjectUrl

{

    public string url;

}

et

public struct MediaObject

{

    public string name;

    public string type;

    public byte[] bits;

}

Puis il faut ajouter la méthode suivante à l'interface IMetaWeblog :

[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);

Enfin nous pouvons implémenter cette méthode :

/// <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></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;

}

Cela permet de se passer de site ftp lors de la publication d'informations contenant des fichiers binaires. Et c'est très pratique.
 
» Démarrer une discussion