internal void DeregisterSupportingFile(ISupportingFile file)
        {
            _supportingFilesByUri.Remove(file.FileUri);
            _supportingFilesById.Remove(file.FileId);

            OnFileRemoved(file);
        }
 private void fileService_FileChanged(ISupportingFile file)
 {
     if (file == listBoxFiles.SelectedItem)
     {
         ShowFileProperties((SupportingFileFactory.VersionedSupportingFile)file);
     }
 }
Example #3
0
        /// <summary>
        /// Saves the emoticon image to disk and returns the path. Should only be called if the emoticon image has not been saved previously for this blog post.
        /// </summary>
        private Uri CreateInlineImage(Emoticon emoticon)
        {
            Debug.Assert(_imageEditingContext.ImageList != null && _imageEditingContext.SupportingFileService != null, "ImageEditingContext not initalized yet.");

            Stream          emoticonStream = StreamHelper.AsStream(ImageHelper.GetBitmapBytes(emoticon.Bitmap, ImageFormat.Png));
            ISupportingFile sourceFile     = _imageEditingContext.SupportingFileService.CreateSupportingFile(emoticon.Id + emoticon.FileExtension, emoticonStream);
            ImageFileData   sourceFileData = new ImageFileData(sourceFile, emoticon.Bitmap.Width, emoticon.Bitmap.Height, ImageFileRelationship.Source);

            BlogPostImageData imageData = new BlogPostImageData(sourceFileData);

            if (GlobalEditorOptions.SupportsFeature(ContentEditorFeature.ShadowImageForDrafts))
            {
                imageData.InitShadowFile(_imageEditingContext.SupportingFileService);
            }

            emoticonStream.Seek(0, SeekOrigin.Begin);
            ISupportingFile inlineFile = _imageEditingContext.SupportingFileService.CreateSupportingFile(emoticon.Id + emoticon.FileExtension, emoticonStream);

            imageData.InlineImageFile = new ImageFileData(inlineFile, emoticon.Bitmap.Width, emoticon.Bitmap.Height, ImageFileRelationship.Inline);

            _imageEditingContext.ImageList.AddImage(imageData);

            SetInlineImageUri(emoticon, imageData.InlineImageFile.Uri);

            return(imageData.InlineImageFile.Uri);
        }
 protected virtual void OnFileChanged(ISupportingFile file)
 {
     if (FileChanged != null)
     {
         FileChanged(file);
     }
 }
            internal string Transform(BeginTag tag, string reference)
            {
                if (UrlHelper.IsUrl(reference))
                {
                    Uri localReferenceUri = new Uri(reference);

                    /*
                     * If we need to drop a hint to the photo uploader about
                     * whether Lightbox-like preview is enabled, so that we know to link to
                     * the image itself rather than the photo "self" page on photos.live.com;
                     * this is where we would figure that out (by looking at the tag) and
                     * pass that info through to the DoUploadWork call.
                     */
                    bool isLightboxCloneEnabled = false;

                    _referenceFixer._fileUploadWorker.DoUploadWork(reference, _uploader, isLightboxCloneEnabled);

                    ISupportingFile supportingFile = _fileService.GetFileByUri(localReferenceUri);
                    if (supportingFile != null)
                    {
                        Uri uploadUri = supportingFile.GetUploadInfo(_uploader.DestinationContext).UploadUri;
                        if (uploadUri != null)
                        {
                            return(UrlHelper.SafeToAbsoluteUri(uploadUri));
                        }
                    }
                }
                return(reference);
            }
Example #6
0
        /// <summary>
        /// Creates an embedded shadow copy of a source image file.
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="fileService"></param>
        private void CreateShadowFile(ImageFileData sourceFile, ISupportingFileService fileService)
        {
            Size shadowSize = new Size(1280, 960);

            using (MemoryStream shadowStream = new MemoryStream())
            {
                ImageFormat format;
                string      fileExt;
                ImageHelper2.GetImageFormat(sourceFile.SupportingFile.FileName, out fileExt, out format);
                using (Bitmap sourceImage = new Bitmap(sourceFile.Uri.LocalPath))
                {
                    if (sourceImage.Width > shadowSize.Width || sourceImage.Height > shadowSize.Height)
                    {
                        shadowSize = ImageHelper2.SaveScaledThumbnailImage(Math.Min(shadowSize.Width, sourceImage.Width),
                                                                           Math.Min(shadowSize.Height, sourceImage.Height),
                                                                           sourceImage, format, shadowStream);
                    }
                    else
                    {
                        shadowSize = sourceImage.Size;
                        using (FileStream fs = File.OpenRead(sourceFile.Uri.LocalPath))
                        {
                            StreamHelper.Transfer(fs, shadowStream);
                        }
                    }
                }
                shadowStream.Seek(0, SeekOrigin.Begin);

                ISupportingFile supportingFile = fileService.CreateSupportingFile(sourceFile.SupportingFile.FileName, shadowStream);
                _imageSourceShadowFile = new ImageFileData(supportingFile, shadowSize.Width, shadowSize.Height, ImageFileRelationship.SourceShadow);
            }
        }
Example #7
0
 public ImageFileData(ISupportingFile supportingFile, int width, int height, ImageFileRelationship relationship)
 {
     _supportingFile = supportingFile;
     Width           = width;
     Height          = height;
     Relationship    = relationship;
 }
Example #8
0
 private void AddReference(ISupportingFile supportingFile)
 {
     SupportingFileFactory.VersionedSupportingFile versionedSupportingFile =
         (SupportingFileFactory.VersionedSupportingFile)supportingFile;
     supportingFilesByUri[supportingFile.FileUri] = supportingFile;
     supportingFilesById[supportingFile.FileId]   = supportingFile;
     filesById[versionedSupportingFile.SupportingFileFactory.FileId] = versionedSupportingFile.SupportingFileFactory;
 }
        internal ISupportingFile CreateSupportingFileFromStoragePath(string fileName, string storagePath)
        {
            SupportingFileFactory fileFactory = new SupportingFileFactory(this, Guid.NewGuid().ToString(), fileName, 1);

            _factoriesById[fileFactory.FileId] = fileFactory;
            ISupportingFile file = fileFactory.CreateSupportingFile(new Uri(UrlHelper.CreateUrlFromPath(storagePath)), fileName, CreateUniqueFileNameToken(fileName), true);

            return(file);
        }
 public FileUploadContext(BlogFileUploader fileUploader, string postId, ISupportingFile supportingFile, ISupportingFileUploadInfo uploadInfo, bool forceDirectImageLink)
 {
     _fileUploader         = fileUploader;
     _blogId               = fileUploader.BlogId;
     _postId               = postId;
     _supportingFile       = supportingFile;
     _uploadInfo           = uploadInfo;
     _forceDirectImageLink = forceDirectImageLink;
 }
        internal ISupportingFile RegisterSupportingFile(ISupportingFile file)
        {
            Debug.Assert(!_supportingFilesByUri.ContainsKey(file.FileUri), "File already exists.");
            Debug.Assert(!_supportingFilesById.ContainsKey(file.FileId), "File already exists.");
            _supportingFilesByUri[file.FileUri] = file;
            _supportingFilesById[file.FileId]   = file;
            RegisterUniqueFileNameToken(file.FileName, file.FileNameUniqueToken);

            OnFileAdded(file);
            return(file);
        }
Example #12
0
 public string CreateFileCallback(string requestedFileName)
 {
     if (ImageSupportingFile == null)
     {
         ImageSupportingFile = _fileService.CreateSupportingFile(requestedFileName, new MemoryStream(new byte[0]));
     }
     else
     {
         ImageSupportingFile = ImageSupportingFile.UpdateFile(new MemoryStream(new byte[0]), requestedFileName);
     }
     return(ImageSupportingFile.FileUri.LocalPath);
 }
Example #13
0
 private string AddHtmlReference(BeginTag tag, string reference)
 {
     if (UrlHelper.IsUrl(reference))
     {
         ISupportingFile supportingFile = _editingContext.SupportingFileService.GetFileByUri(new Uri(reference));
         if (supportingFile != null && supportingFile.Embedded)
         {
             AddReference(supportingFile);
         }
     }
     return(reference);
 }
        public ISupportingFile AddLinkedSupportingFileReference(Uri sourceUri)
        {
            ISupportingFile file = GetFileByUri(sourceUri);

            if (file == null)
            {
                string fileName = Path.GetFileName(sourceUri.LocalPath);
                SupportingFileFactory fileFactory = new SupportingFileFactory(this, Guid.NewGuid().ToString(), fileName, 1);
                _factoriesById[fileFactory.FileId] = fileFactory;
                file = fileFactory.CreateSupportingFile(sourceUri, fileName, CreateUniqueFileNameToken(fileName + "_linked"), false);
            }
            return(file);
        }
Example #15
0
 public virtual bool DoesFileNeedUpload(ISupportingFile file, IFileUploadContext uploadContext)
 {
     // Check to see if we have already uploaded this file.
     if (!file.IsUploaded(DestinationContext))
     {
         return(true);
     }
     else
     {
         Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "File is up-to-date: {0}", file.FileName));
         return(false);
     }
 }
        public Stream GetFileStream(string fileName)
        {
            ISupportingFile file = GetSupportingFile(fileName);

            if (file != null)
            {
                return(new FileStream(file.FileUri.LocalPath, FileMode.Open));
            }
            else
            {
                return(null);
            }
        }
        public Uri GetFileUri(string fileName)
        {
            ISupportingFile file = GetSupportingFile(fileName);

            if (file != null)
            {
                return(file.FileUri);
            }
            else
            {
                return(null);
            }
        }
Example #18
0
            public void DoUploadWork(string fileReference, BlogFileUploader fileUploader, bool isLightboxCloneEnabled)
            {
                // Get both strings into the same state which is unescaped
                string          unescapedFileReference = new Uri(fileReference).ToString();
                ISupportingFile file = null;

                foreach (ISupportingFile supportingFile in _fileList)
                {
                    if (supportingFile.FileUri.ToString() == unescapedFileReference)
                    {
                        file = supportingFile;
                        break;
                    }
                }

                if (file == null)
                {
                    string listString = "";
                    foreach (ISupportingFile supportingFile in _fileList)
                    {
                        listString += supportingFile.FileUri + "\r\n";
                    }
                    Trace.Fail(String.Format(CultureInfo.InvariantCulture, "Reference found to file that does not exist in SupportingFileService \r\nfileReference: {0}\r\n_fileList:\r\n{1}", fileReference, listString));
                    return;
                }


                string uploadContext = fileUploader.DestinationContext;

                ISupportingFileUploadInfo uploadInfo        = file.GetUploadInfo(uploadContext);
                FileUploadContext         fileUploadContext = new FileUploadContext(fileUploader, _postId, file, uploadInfo, isLightboxCloneEnabled);

                if (fileUploader.DoesFileNeedUpload(file, fileUploadContext))
                {
                    if (!_uploadedFiles.ContainsKey(file.FileId))
                    {
                        _uploadedFiles[file.FileId] = file;
                        Uri uploadUri = fileUploader.DoUploadWorkBeforePublish(fileUploadContext);
                        if (uploadUri != null)
                        {
                            file.MarkUploaded(uploadContext, uploadUri);
                            Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "File Uploaded: {0}", file.FileName));
                        }
                    }
                    else
                    {
                        Trace.Fail("This file has already been uploaded during this publish operation: " + file.FileName);
                    }
                }
            }
Example #19
0
        public override bool DoesFileNeedUpload(ISupportingFile file, IFileUploadContext uploadContext)
        {
            // Let the blog client decide if it wants to upload this file or not
            bool?shouldUpload = _blogClient.DoesFileNeedUpload(uploadContext);

            // Check to see if the blog client made a decision, if so, then use it
            if (shouldUpload != null)
            {
                return(shouldUpload.Value);
            }

            // Check to see if it was already uploaded and saved in the content for this post
            return(base.DoesFileNeedUpload(file, uploadContext));
        }
        public ISupportingFile CreateSupportingFile(string fileName, string fileNameUniqueToken, Stream s)
        {
            string storagePath = _fileStorage.CreateFile(fileName);

            using (Stream outStream = new FileStream(storagePath, FileMode.OpenOrCreate, FileAccess.Write))
                StreamHelper.Transfer(s, outStream);

            SupportingFileFactory fileFactory = new SupportingFileFactory(this, Guid.NewGuid().ToString(), fileName, 1);

            _factoriesById[fileFactory.FileId] = fileFactory;
            ISupportingFile file = fileFactory.CreateSupportingFile(new Uri(UrlHelper.CreateUrlFromPath(storagePath)), fileName, fileNameUniqueToken, true);

            return(file);
        }
        public string AddFile(string fileName, Stream s)
        {
            ISupportingFile file = GetSupportingFile(fileName);

            if (file == null)
            {
                file = _fileService.CreateSupportingFile(fileName, s);
            }
            else
            {
                file = file.UpdateFile(s);
            }
            _fileIds[fileName] = file;

            return(file.FileId);
        }
            public void ReferenceFixedCallback(string oldReference, string newReference)
            {
                ISupportingFile file = _editingContext.SupportingFileService.GetFileByUri(new Uri(newReference));

                if (file != null)
                {
                    foreach (BlogPostImageData imageData in _editingContext.ImageDataList)
                    {
                        // We can no longer trust the target settings for this file, so we must remove them
                        // this means the first time the object is clicked it will read the settings from the DOM
                        if (file.FileId == imageData.InlineImageFile.SupportingFile.FileId)
                        {
                            BlogPostSettingsBag settings =
                                imageData.ImageDecoratorSettings.GetSubSettings(HtmlImageTargetDecorator.Id);
                            foreach (string settingName in HtmlImageTargetDecoratorSettings.ImageReferenceFixedStaleProperties)
                            {
                                settings.Remove(settingName);
                            }
                        }
                    }
                }
            }
 protected override void OnBeginTag(BeginTag tag)
 {
     if (tag != null && LightWeightHTMLDocument.AllUrlElements.ContainsKey(tag.Name.ToUpper(CultureInfo.InvariantCulture)))
     {
         Attr attr = tag.GetAttribute((string)LightWeightHTMLDocument.AllUrlElements[tag.Name.ToUpper(CultureInfo.InvariantCulture)]);
         if (attr != null && attr.Value != null)
         {
             if (UrlHelper.IsUrl(attr.Value))
             {
                 Uri reference = new Uri(attr.Value);
                 if (_fileReferenceList.IsReferenced(reference))
                 {
                     ISupportingFile supportingFile = _fileReferenceList.GetSupportingFileByUri(reference);
                     if (supportingFile.Embedded)
                     {
                         AddFileReference(supportingFile);
                     }
                 }
             }
         }
     }
     base.OnBeginTag(tag);
 }
Example #24
0
        private void CalculateReferencesForSave()
        {
            //calculate references in the html content
            string postContents = _editingContext.BlogPost.Contents;

            HtmlReferenceFixer.FixReferences(postContents, new ReferenceFixer(AddHtmlReference));

            //calculate the images referenced by the HTML
            AddImageReferences(_editingContext.ImageDataList);

            //calculate files referenced by plugins
            foreach (BlogPostExtensionData extensionData in _editingContext.ExtensionDataList.CalculateReferencedExtensionData(postContents))
            {
                foreach (string fileId in extensionData.FileIds)
                {
                    ISupportingFile file = extensionData.GetSupportingFile(fileId);
                    if (file != null)
                    {
                        AddReference(file);
                    }
                }
            }
        }
 internal string AddStorageFileMapping(string fileName, ISupportingFile file)
 {
     _fileIds[fileName] = file;
     return(fileName);
 }
        internal ISupportingFile RegisterSupportingFile(ISupportingFile file)
        {
            Debug.Assert(!_supportingFilesByUri.ContainsKey(file.FileUri), "File already exists.");
            Debug.Assert(!_supportingFilesById.ContainsKey(file.FileId), "File already exists.");
            _supportingFilesByUri[file.FileUri] = file;
            _supportingFilesById[file.FileId] = file;
            RegisterUniqueFileNameToken(file.FileName, file.FileNameUniqueToken);

            OnFileAdded(file);
            return file;
        }
 private void fileService_FileChanged(ISupportingFile file)
 {
     if (file == listBoxFiles.SelectedItem)
         ShowFileProperties((SupportingFileFactory.VersionedSupportingFile)file);
 }
 private void AddFileReference(ISupportingFile supportingFile)
 {
     _fileUploadWorker.AddFile(supportingFile);
 }
Example #29
0
        internal static void UpdateImageSource(ImagePropertiesInfo imgProperties, IHTMLElement imgElement, IBlogPostImageEditingContext editorContext, ImageInsertHandler imageInsertHandler, ImageDecoratorInvocationSource invocationSource)
        {
            ISupportingFile oldImageFile = null;

            try
            {
                oldImageFile = editorContext.SupportingFileService.GetFileByUri(new Uri((string)imgElement.getAttribute("src", 2)));
            }
            catch (UriFormatException) { }
            if (oldImageFile != null) //then this is a known supporting image file
            {
                using (new WaitCursor())
                {
                    BlogPostImageData imageData = BlogPostImageDataList.LookupImageDataByInlineUri(editorContext.ImageList, oldImageFile.FileUri);
                    if (imageData != null)
                    {
                        //Create a new ImageData object based on the image data attached to the current image src file.
                        BlogPostImageData newImageData = (BlogPostImageData)imageData.Clone();

                        //initialize some handlers for creating files based on the image's existing ISupportingFile objects
                        //This is necessary so that the new image files are recognized as being updates to an existing image
                        //which allows the updates to be re-uploaded back to the same location.
                        CreateImageFileHandler inlineFileCreator = new CreateImageFileHandler(editorContext.SupportingFileService,
                                                                                              newImageData.InlineImageFile != null ? newImageData.InlineImageFile.SupportingFile : null);
                        CreateImageFileHandler linkedFileCreator = new CreateImageFileHandler(editorContext.SupportingFileService,
                                                                                              newImageData.LinkedImageFile != null ? newImageData.LinkedImageFile.SupportingFile : null);

                        //re-write the image files on disk using the latest settings
                        imageInsertHandler.WriteImages(imgProperties, true, invocationSource, new CreateFileCallback(inlineFileCreator.CreateFileCallback), new CreateFileCallback(linkedFileCreator.CreateFileCallback), editorContext.EditorOptions);

                        //update the ImageData file references
                        Size imageSizeWithBorder = imgProperties.InlineImageSizeWithBorder;

                        //force a refresh of the image size values in the DOM by setting the new size attributes
                        imgElement.setAttribute("width", imageSizeWithBorder.Width, 0);
                        imgElement.setAttribute("height", imageSizeWithBorder.Height, 0);

                        newImageData.InlineImageFile.SupportingFile = inlineFileCreator.ImageSupportingFile;
                        newImageData.InlineImageFile.Height         = imageSizeWithBorder.Height;
                        newImageData.InlineImageFile.Width          = imageSizeWithBorder.Width;
                        if (imgProperties.LinkTarget == LinkTargetType.IMAGE)
                        {
                            newImageData.LinkedImageFile = new ImageFileData(linkedFileCreator.ImageSupportingFile, imgProperties.LinkTargetImageSize.Width, imgProperties.LinkTargetImageSize.Height, ImageFileRelationship.Linked);
                        }
                        else
                        {
                            newImageData.LinkedImageFile = null;
                        }

                        //assign the image decorators applied during WriteImages
                        //Note: this is a clone so the sidebar doesn't affect the decorator values for the newImageData image src file
                        newImageData.ImageDecoratorSettings = (BlogPostSettingsBag)imgProperties.ImageDecorators.SettingsBag.Clone();

                        //update the upload settings
                        newImageData.UploadInfo.ImageServiceId = imgProperties.UploadServiceId;

                        //save the new image data in the image list
                        editorContext.ImageList.AddImage(newImageData);
                    }
                    else
                    {
                        Debug.Fail("imageData could not be located");
                    }
                }
            }

            if (imgProperties.LinkTarget == LinkTargetType.NONE)
            {
                imgProperties.RemoveLinkTarget();
            }
        }
 public CreateImageFileHandler(ISupportingFileService fileService, ISupportingFile supportingFile)
 {
     _fileService = fileService;
     ImageSupportingFile = supportingFile;
 }
 internal void NotifyFileChanged(ISupportingFile file)
 {
     OnFileChanged(file);
 }
 public ImageFileData(ISupportingFile supportingFile)
 {
     _supportingFile = supportingFile;
 }
 public ImageFileData(ISupportingFile supportingFile, int width, int height, ImageFileRelationship relationship)
 {
     _supportingFile = supportingFile;
     Width = width;
     Height = height;
     Relationship = relationship;
 }
 private void AddReference(ISupportingFile supportingFile)
 {
     SupportingFileFactory.VersionedSupportingFile versionedSupportingFile =
         (SupportingFileFactory.VersionedSupportingFile)supportingFile;
     supportingFilesByUri[supportingFile.FileUri] = supportingFile;
     supportingFilesById[supportingFile.FileId] = supportingFile;
     filesById[versionedSupportingFile.SupportingFileFactory.FileId] = versionedSupportingFile.SupportingFileFactory;
 }
 private void AddFile(ISupportingFile file)
 {
     listBoxFiles.Items.Add(file);
 }
 public virtual bool DoesFileNeedUpload(ISupportingFile file, IFileUploadContext uploadContext)
 {
     // Check to see if we have already uploaded this file.
     if (!file.IsUploaded(DestinationContext))
     {
         return true;
     }
     else
     {
         Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "File is up-to-date: {0}", file.FileName));
         return false;
     }
 }
 public FileUploadContext(BlogFileUploader fileUploader, string postId, ISupportingFile supportingFile, ISupportingFileUploadInfo uploadInfo, bool forceDirectImageLink)
 {
     _fileUploader = fileUploader;
     _blogId = fileUploader.BlogId;
     _postId = postId;
     _supportingFile = supportingFile;
     _uploadInfo = uploadInfo;
     _forceDirectImageLink = forceDirectImageLink;
 }
 private void fileService_FileRemoved(ISupportingFile file)
 {
     listBoxFiles.Items.Remove(file);
 }
 public void AddFile(ISupportingFile supportingFile)
 {
     _fileList.Add(supportingFile);
 }
        private void listBoxFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            ISupportingFile file = listBoxFiles.SelectedItem as ISupportingFile;

            ShowFileProperties((SupportingFileFactory.VersionedSupportingFile)file);
        }
 public string CreateFileCallback(string requestedFileName)
 {
     if (ImageSupportingFile == null)
         ImageSupportingFile = _fileService.CreateSupportingFile(requestedFileName, new MemoryStream(new byte[0]));
     else
         ImageSupportingFile = ImageSupportingFile.UpdateFile(new MemoryStream(new byte[0]), requestedFileName);
     return ImageSupportingFile.FileUri.LocalPath;
 }
Example #42
0
 public ImageFileData(ISupportingFile supportingFile)
 {
     _supportingFile = supportingFile;
 }
Example #43
0
 public CreateImageFileHandler(ISupportingFileService fileService, ISupportingFile supportingFile)
 {
     _fileService        = fileService;
     ImageSupportingFile = supportingFile;
 }
 protected virtual void OnFileChanged(ISupportingFile file)
 {
     if (FileChanged != null)
         FileChanged(file);
 }
 internal string AddStorageFileMapping(string fileName, ISupportingFile file)
 {
     _fileIds[fileName] = file;
     return fileName;
 }
        internal void DeregisterSupportingFile(ISupportingFile file)
        {
            _supportingFilesByUri.Remove(file.FileUri);
            _supportingFilesById.Remove(file.FileId);

            OnFileRemoved(file);
        }
 private void fileService_FileRemoved(ISupportingFile file)
 {
     listBoxFiles.Items.Remove(file);
 }
 private void fileService_FileAdded(ISupportingFile file)
 {
     AddFile(file);
 }
        public override bool DoesFileNeedUpload(ISupportingFile file, IFileUploadContext uploadContext)
        {
            // Let the blog client decide if it wants to upload this file or not
            bool? shouldUpload = _blogClient.DoesFileNeedUpload(uploadContext);

            // Check to see if the blog client made a decision, if so, then use it
            if (shouldUpload != null)
            {
                return shouldUpload.Value;
            }

            // Check to see if it was already uploaded and saved in the content for this post
            return base.DoesFileNeedUpload(file, uploadContext);
        }
 public bool IsReferenced(ISupportingFile supportingFile)
 {
     return supportingFilesById[supportingFile.FileId] != null;
 }