MetaWeblogApi.newMediaObject (blogid, username, password, fileObject)
metaWeblog.newMediaObject uploads a file to the webserver

  • Parameter blogid:
    String
  • Parameter username:
    String
  • Parameter password:
    String
  • Parameter fileObject:
    Object, containing the following properties .bits base64, the base64-encoded contents of the file .name String containing the filename .type String representing a MIME-type .description String (optional) description of the object .antville_maxheight Integer (optional), just applies for images .antville_maxwidth Integer (optional), just applies for images
  • Returns
    Object containing the following properties .url String containing the URL of the uploaded file .antville_alias Sting containing the alias of the uploaded file .antville_message String human readable message indicating success .antville_macro String a macro which can be embedded into storys to insert this object .antville_thumbmacro String (optional) a macro to display a thumbnail .antville_popupmacro String (optional) a macro to display the image as a popup .antville_staticUrl String (optional) url of the Image .antville_popupUrl String (optional) javascript code to open a popup with the image .antville_width Int (optional) the width of the image .antville_height Int (optional) the height of the image
Sourcecode in MetaWeblogApi/metaWeblogAPI.js:
1:   function newMediaObject(blogid, username, password, fileObject) {
2:      var usr = root.blogger.getUser(username, password);
3:      var blog = root.blogger.getBlog(blogid.toString());
4:      if (!blog)
5:         throw("Couldn't find the blog " + blogid);
6:      var level = blog.members.getMembershipLevel(usr);
7:      var str = new java.lang.String(fileObject.bits);
8:      var bytes = Packages.helma.util.Base64.decode(str.toCharArray());
9:      var param = new Object();
10:     var ret = new Object();
11:     if (fileObject.type.toLowerCase().startsWith("image/")) {
12:        // handle new image
13:        try {
14:           blog.images.checkAdd(usr, level);
15:           param.rawimage = new Packages.helma.util.MimePart(fileObject.name, bytes, fileObject.type);
16:           param.maxheight = fileObject.antville_maxheight;
17:           param.maxwidth = fileObject.antville_maxwidth;
18:           param.alttext = fileObject.description;
19:           var result = blog.images.evalImg(param, usr);
20:           var mediaObject = result.obj;
21:           ret.antville_alias = mediaObject.alias;
22:           ret.antville_staticUrl = mediaObject.getUrl();
23:           ret.antville_popupUrl = mediaObject.getPopupUrl();
24:           ret.antville_width = mediaObject.width;
25:           ret.antville_height = mediaObject.height;
26:           ret.antville_macro = "<% image name=\"" + blog.alias + "/" + mediaObject.alias + "\" %>";
27:           if (mediaObject.thumbnail) {
28:              ret.antville_popupmacro = "<% image name=\"" + blog.alias + "/" + mediaObject.alias + "\" as=\"popup\" %>";
29:              ret.antville_thumbmacro = "<% image name=\"" + blog.alias + "/" + mediaObject.alias + "\" as=\"thumbnail\" %>";
30:           }
31:        } catch (e) {
32:           if (e instanceof DenyException)
33:              throw("You're not allowed to upload images to the blog " + blog.alias);
34:           else
35:              throw("Error while creating new Media Object: " + e.toString());
36:        }
37:     } else {
38:        // handle new file
39:        try {
40:           blog.files.checkAdd(usr, level);
41:           param.rawfile = new Packages.helma.util.MimePart(fileObject.name, bytes, fileObject.type);
42:           param.description = fileObject.description;
43:           var result = blog.files.evalFile(param, usr);
44:           var mediaObject = result.obj;
45:           ret.antville_macro = "<% file name=\"" + blog.alias + "/" + mediaObject.alias + "\" %>";
46:        } catch (e) {
47:           if (e instanceof DenyException)
48:              throw("You're not allowed to upload files to the blog " + blog.alias);
49:           else
50:              throw("Error occured while creating new Media Object: " + e.toString());
51:        }
52:     }
53:     ret.url = mediaObject.getUrl();
54:     ret.antville_message = result.toString();
55:     return ret;
56:  }