Ejemplo n.º 1
0
        /// <summary>
        /// Changes all image source to mm protocol, downloads and saves the locally
        /// </summary>
        public static void ProcessImages(HtmlDocument document, PersistentTree tree)
        {
            using (var web = new WebClient())
            {
                var elemCol = document.GetElementsByTagName("img");

                for (int i = 0; i < elemCol.Count; i++)
                {
                    var elem        = elemCol[i];
                    var originalSrc = elem.GetAttribute("src");
                    if (originalSrc.Length > 4 && originalSrc.Substring(0, 4).Equals("http", StringComparison.OrdinalIgnoreCase))
                    {
                        byte[] data = null;
                        string extension;
                        try
                        {
                            data      = web.DownloadData(originalSrc);
                            extension = MimeTypes.GetExtension(web.ResponseHeaders["Content-Type"]);
                        }
                        catch (WebException) //resource not found (404) and no connection
                        {
                            extension = "png";
                        }

                        var newInternalSrc = ImageLocalPath.CreateNewLocalPath(extension);
                        tree.SetLargeObject(newInternalSrc.FileName, new BytesLob(data));

                        elem.SetAttribute("srcOrig", originalSrc);
                        elem.SetAttribute("src", newInternalSrc.Url);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void InsertFormFile(NoteEditor editor, string fileName, MapTree tree)
        {
            var localPath = ImageLocalPath.CreateNewLocalPath(Path.GetExtension(fileName).Substring(1));

            tree.SetLargeObject(localPath.FileName, new BytesLob(File.ReadAllBytes(fileName)));

            var htmlImage = new HtmlImageCreator(editor);

            htmlImage.InsertImage(localPath.Url, "");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Changes all image source to mm protocol, downloads and saves the locally
        /// </summary>
        public static void ProcessImages(HtmlDocument document, PersistentTree tree)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls
                                                    | SecurityProtocolType.Tls11
                                                    | SecurityProtocolType.Tls12
                                                    | SecurityProtocolType.Ssl3;

            using (var web = new WebClient())
            {
                var elemCol = document.GetElementsByTagName("img");

                for (int i = 0; i < elemCol.Count; i++)
                {
                    var elem        = elemCol[i];
                    var originalSrc = elem.GetAttribute("src");
                    if (originalSrc.Length > 4 && originalSrc.Substring(0, 4).Equals("http", StringComparison.OrdinalIgnoreCase))
                    {
                        byte[] data = null;
                        string extension;
                        try
                        {
                            data = web.DownloadData(originalSrc);
                        }
                        catch (Exception exp)
                        {
                            Log.Write("[ImageLocalSaver] Cannot download the image: " + exp.Message);
                        }
                        if (data != null)
                        {
                            try
                            {
                                extension = MimeTypes.GetExtension(web.ResponseHeaders["Content-Type"]);
                            }
                            catch (WebException exp) //resource not found (404) and no connection
                            {
                                Log.Write("[ImageLocalSaver] Cannot get the content type of image: " + exp.Message);
                                extension = "png";
                            }

                            var newInternalSrc = ImageLocalPath.CreateNewLocalPath(extension);
                            tree.SetLargeObject(newInternalSrc.FileName, new BytesLob(data));

                            elem.SetAttribute("srcOrig", originalSrc);
                            elem.SetAttribute("src", newInternalSrc.Url);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override byte[] GetUrlData(string url, out string contentType)
        {
            ImageLocalPath path = ImageLocalPath.ConvertTo(url);

            var data = persistence.CurrentTree.GetByteArray(path.FileName);

            if (data != null)
            {
                contentType = MimeTypes.GetMimeType(path.Extension);
            }
            else
            {
                contentType = "text/html";
                data        = Encoding.UTF8.GetBytes(@"<b>Page not found!</b>");
            }

            return(data);
        }
Ejemplo n.º 5
0
        public override byte[] GetUrlData(string url, out string contentType)
        {
            ImageLocalPath path = ImageLocalPath.ConvertTo(url);

            byte[] data;

            if (persistence.CurrentTree.TryGetLargeObject(path.FileName, out BytesLob lob))
            {
                data        = lob.Bytes;
                contentType = MimeTypes.GetMimeType(path.Extension);
            }
            else
            {
                contentType = "text/html";
                data        = Encoding.UTF8.GetBytes(@"<b>Page not found!</b>");
            }

            return(data);
        }
Ejemplo n.º 6
0
        public static bool PasteFromClipboard(NoteEditor editor, PersistentTree tree)
        {
            if (Clipboard.ContainsImage())
            {
                Image image = Clipboard.GetImage();

                var imagePath = ImageLocalPath.CreateNewLocalPath("png");

                MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                tree.SetByteArray(imagePath.FileName, ms.ToArray());

                var htmlImage = new HtmlImageCreator(editor);
                htmlImage.InsertImage(imagePath.Url, "");

                return(true);
            }
            else if (Clipboard.ContainsFileDropList())
            {
                var fileList  = Clipboard.GetFileDropList();
                var imageList = FilterImageFiles(fileList);
                if (imageList.Any())
                {
                    imageList.ForEach(i =>
                    {
                        var localPath = ImageLocalPath.CreateNewLocalPath(Path.GetExtension(i).Substring(1));
                        tree.SetByteArray(localPath.FileName, File.ReadAllBytes(i));

                        var htmlImage = new HtmlImageCreator(editor);
                        htmlImage.InsertImage(localPath.Url, "");
                    });
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 7
0
        public static bool PasteFromClipboard(NoteEditor editor, PersistentTree tree)
        {
            if (Clipboard.ContainsImage())
            {
                Image image = Clipboard.GetImage();

                var imagePath = ImageLocalPath.CreateNewLocalPath("png");

                MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                tree.SetLargeObject(imagePath.FileName, new BytesLob(ms.ToArray()));

                var htmlImage = new HtmlImageCreator(editor);
                htmlImage.InsertImage(imagePath.Url, "");

                return(true);
            }
            else if (Clipboard.ContainsFileDropList())
            {
                var fileList  = Clipboard.GetFileDropList();
                var imageList = FilterImageFiles(fileList);
                if (imageList.Any())
                {
                    imageList.ForEach(i =>
                    {
                        InsertFormFile(editor, i, tree);
                    });
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }