public string newPost(string blogid, string username, string password, MetaWeblog.Post post, bool publish) { if(ValidateUser(username,password)) { IGraffitiUser user = GraffitiUsers.Current; Graffiti.Core.Post postToAdd = new Graffiti.Core.Post(); postToAdd.ContentType = "text/html"; postToAdd.PostStatus = (publish ? PostStatus.Publish : PostStatus.Draft); postToAdd.IsPublished = publish; postToAdd.PostBody = post.description; postToAdd.Title = post.title; postToAdd.TagList = post.GetTagList(); postToAdd.UserName = username; postToAdd.EnableComments = CommentSettings.Get().EnableCommentsDefault; if (post.categories != null && post.categories.Length > 0) { postToAdd.CategoryId = AddOrFetchCategory(post.categories[0],user).Id; } else { postToAdd.CategoryId = CategoryController.UnCategorizedId; } postToAdd.Name = post.GetSlug(); if (!string.IsNullOrEmpty(post.mt_text_more)) { postToAdd.ExtendedBody = post.mt_text_more; } // Get UserTime safely (some clients pass in a DateTime that is not valid) try { if (post.dateCreated != DateTime.MinValue) { DateTime dtUTC = post.dateCreated; DateTime dtLocal = dtUTC.ToLocalTime(); postToAdd.Published = dtLocal.AddHours(SiteSettings.Get().TimeZoneOffSet); } } catch { postToAdd.Published = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet); } if(postToAdd.Published <= new DateTime(2000,1,1)) postToAdd.Published = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet); try { return PostRevisionManager.CommitPost(postToAdd, user, false, false).ToString(); } catch(Exception ex) { if (ex.Message.IndexOf("UNIQUE") > -1) throw new XmlRpcFaultException(2,"Duplicate Post Name"); else { Log.Error("MetaBlog Error", "An error occored editing the post {0}. Exception: {1} Stack: {2}", post.postid, ex.Message, ex.StackTrace); throw; } } } throw new XmlRpcFaultException(0,"User does not exist"); }
public MetaWeblog.MediaObjectInfo newMediaObject(string blogid, string username, string password, MetaWeblog.MediaObject mediaObject) { if (ValidateUser(username, password)) { if (mediaObject.bits != null && !string.IsNullOrEmpty(mediaObject.name)) { string fileName = mediaObject.name; //Mars Edit appends '/' and this causes .NET to save the file at the root of Windows. if (fileName.StartsWith("/")) fileName = fileName.Substring(1); int i = fileName.LastIndexOf(Path.DirectorySeparatorChar); if (i != -1) fileName = fileName.Substring(i + 1); bool isImage = Regex.IsMatch(Path.GetExtension(mediaObject.name), ".jpg|.jpeg|.png|.gif",RegexOptions.IgnoreCase); string name = isImage ? Path.Combine(Context.Server.MapPath("~/files/media/image"), fileName) : Path.Combine(Context.Server.MapPath("~/files/media/file"), fileName); FileInfo fi = new FileInfo(name); if (!fi.Directory.Exists) fi.Directory.Create(); using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.None)) { byte[] buffer = new byte[256*1024]; int bytes; MemoryStream content = new MemoryStream(mediaObject.bits); while ((bytes = content.Read(buffer, 0, 256*1024)) > 0) fs.Write(buffer, 0, bytes); fs.Close(); } MediaObjectInfo info = new MediaObjectInfo(); string absolteUrl = VirtualPathUtility.ToAbsolute((isImage ? "~/files/media/image/" : "~/files/media/file/") + fileName); info.url = new Macros().FullUrl(absolteUrl); return info; } } throw new XmlRpcFaultException(0, "User does not exist"); }