public BlogPostEditingContext(string destinationBlogId, BlogPost blogPost, PostEditorFile localFile, PostEditorFile autoSaveLocalFile, string serverSupportingFileDirectory, BlogPostSupportingFileStorage supportingFileStorage, BlogPostImageDataList imageDataList, BlogPostExtensionDataList extensionDataList, ISupportingFileService supportingFileService) : this(destinationBlogId, blogPost, localFile) { _serverSupportingFileDirectory = serverSupportingFileDirectory; _supportingFileStorage = supportingFileStorage; _imageDataList = imageDataList; _extensionDataList = extensionDataList; _fileService = supportingFileService; _autoSaveLocalFile = autoSaveLocalFile; }
private void SaveCore(IBlogPostEditingContext editingContext, PostEditorFile autoSaveSourceFile, string filePath) { // did this file exist prior to the attempt to save (if no, we need to delete // it if an exceptoin occurs -- otherwise we leave a "zombie" post file with // no available streams bool isPreviouslyUnsaved = !IsSaved; try { try { // alias blog-post BlogPost blogPost = editingContext.BlogPost; Debug.Assert(!blogPost.IsTemporary, "Saving temporary style detection post!?"); // write out all of the fields using (Storage postStorage = new Storage(filePath, StorageMode.OpenOrCreate, true)) { // file-format clsid postStorage.Clsid = Version2FormatCLSID; // meta-data WriteString(postStorage, DESTINATION_BLOG_ID, editingContext.BlogId); WriteString(postStorage, SERVER_SUPPORTING_FILE_DIR, editingContext.ServerSupportingFileDirectory); // blog post WriteString(postStorage, POST_ID, blogPost.Id); WriteBoolean(postStorage, POST_ISPAGE, blogPost.IsPage); WriteString(postStorage, POST_TITLE, blogPost.Title); WriteXml(postStorage, POST_CATEGORIES, blogPost.Categories, new XmlWriteHandler(WriteCategories)); WriteXml(postStorage, POST_NEW_CATEGORIES, blogPost.NewCategories, new XmlWriteHandler(WriteCategories)); WriteDateTime(postStorage, POST_DATEPUBLISHED, blogPost.DatePublished); WriteDateTime(postStorage, POST_DATEPUBLISHED_OVERRIDE, blogPost.DatePublishedOverride); WriteCommentPolicy(postStorage, blogPost.CommentPolicy); WriteTrackbackPolicy(postStorage, blogPost.TrackbackPolicy); WriteString(postStorage, POST_KEYWORDS, blogPost.Keywords); WriteString(postStorage, POST_EXCERPT, blogPost.Excerpt); WriteString(postStorage, POST_PERMALINK, blogPost.Permalink); WriteString(postStorage, POST_LINK, blogPost.Permalink); // write for legacy compatability with beta 1 WriteXml(postStorage, POST_PINGURLS_PENDING, blogPost.PingUrlsPending, new XmlWriteHandler(WritePingUrls)); WriteXml(postStorage, POST_PINGURLS_SENT, blogPost.PingUrlsSent, new XmlWriteHandler(WritePingUrls)); WriteString(postStorage, POST_SLUG, blogPost.Slug); WriteString(postStorage, POST_PASSWORD, blogPost.Password); WriteString(postStorage, POST_AUTHOR_ID, blogPost.Author.Id); WriteString(postStorage, POST_AUTHOR_NAME, blogPost.Author.Name); WriteString(postStorage, POST_PAGE_PARENT_ID, blogPost.PageParent.Id); WriteString(postStorage, POST_PAGE_PARENT_NAME, blogPost.PageParent.Name); WriteString(postStorage, POST_PAGE_ORDER, blogPost.PageOrder); WriteString(postStorage, POST_ETAG, blogPost.ETag); WriteXml(postStorage, POST_ATOM_REMOTE_POST, blogPost.AtomRemotePost, new XmlWriteHandler(XmlDocWriteHandler)); //save the post info hash WriteString(postStorage, POST_CONTENTS_VERSION_SIGNATURE, blogPost.ContentsVersionSignature); // contents (with fixups for local files) SupportingFilePersister supportingFilePersister = new SupportingFilePersister(postStorage.OpenStorage(POST_SUPPORTING_FILES, StorageMode.Create, true)); //BlogPostReferenceFixedHandler fixedReferenceHandler = new BlogPostReferenceFixedHandler(editingContext.ImageDataList); //string fixedUpPostContents = supportingFilePersister.SaveFilesAndFixupReferences(blogPost.Contents, new ReferenceFixedCallback(fixedReferenceHandler.HandleReferenceFixed)) ; //write the attached file data //supportingFilePersister. SupportingFileReferenceList referenceList = SupportingFileReferenceList.CalculateReferencesForSave(editingContext); WriteXml(postStorage, POST_ATTACHED_FILES, null, new XmlWriteHandler(new AttachedFileListWriter(supportingFilePersister, editingContext, referenceList).WriteAttachedFileList)); WriteXml(postStorage, POST_IMAGE_FILES, editingContext.ImageDataList, new XmlWriteHandler(new AttachedImageListWriter(referenceList).WriteImageFiles)); //write the extension data WriteXml(postStorage, POST_EXTENSION_DATA_LIST, editingContext.ExtensionDataList, new XmlWriteHandler(new ExtensionDataListWriter(supportingFilePersister, blogPost.Contents).WriteExtensionDataList)); //Convert file references in the HTML contents to the new storage path string fixedUpPostContents = supportingFilePersister.FixupHtmlReferences(blogPost.Contents); WriteStringUtf8(postStorage, POST_CONTENTS, fixedUpPostContents); string originalSourcePath = autoSaveSourceFile == null ? "" : autoSaveSourceFile.IsSaved ? autoSaveSourceFile.TargetFile.FullName : autoSaveSourceFile.TargetDirectory.FullName; WriteStringUtf8(postStorage, ORIGINAL_SOURCE_PATH, originalSourcePath); // save to storage postStorage.Commit(); // mark file as saved TargetFile = new FileInfo(filePath); } } catch (Exception ex) { Trace.Fail("Unexpected exception type in PostEditorFile.Save. It is critical that only IO exceptions occur at this level of the system so please check the code which threw the exeption and see if there is a way to behave more robustly!\r\n" + ex.ToString()); throw PostEditorStorageException.Create(ex); } } catch { // if we had no file previously and an exception occurs then // we need to delete the file if (isPreviouslyUnsaved && File.Exists(filePath)) try { File.Delete(filePath); } catch { } throw; } }
/// <summary> /// Saves state to this file that includes changes the user has made /// but not saved to autoSaveSourceFile. /// </summary> /// <param name="editingContext"></param> /// <param name="autoSaveSourceFile">The original blog post file that /// this AutoSave operation is storing changes for.</param> public void AutoSave(IBlogPostEditingContext editingContext, PostEditorFile autoSaveSourceFile) { SaveCore(editingContext, autoSaveSourceFile, ManagePostFilePath(editingContext.BlogPost.IsPage, editingContext.BlogPost.Title)); }
private PostEditorFile GetFileFromSourcePath(string originalSourcePath, out PostEditorFile autoSaveFile) { if (string.IsNullOrEmpty(originalSourcePath)) { autoSaveFile = null; return this; } if (Directory.Exists(originalSourcePath)) { autoSaveFile = this; return new PostEditorFile(new DirectoryInfo(originalSourcePath)); } if (File.Exists(originalSourcePath)) { autoSaveFile = this; return new PostEditorFile(new FileInfo(originalSourcePath)); } Trace.WriteLine("Original source path no longer exists: " + originalSourcePath); autoSaveFile = this; return new PostEditorFile(DraftsFolder); }
private bool DeletePostFile(PostEditorFile postFile) { try { // screen files which have already been deleted through other means if (!postFile.IsDeleted) postFile.Delete(); return true; } catch (Exception ex) { DisplayableException displayableException = new DisplayableException( StringId.ErrorOccurredDeletingDraft, StringId.ErrorOccurredDeletingDraftDetails, ex.Message); DisplayableExceptionDisplayForm.Show(_mainFrameWindow, displayableException); return false; } }
private BlogPostEditingContext(string destinationBlogId, BlogPost blogPost, PostEditorFile localFile) { _blogId = destinationBlogId; _blogPost = blogPost; _localFile = localFile; }