Example #1
0
        public string Clean(string value, ImportSettings importSettings) {
            if (string.IsNullOrEmpty(value))
                return value;

            try {
                if (importSettings.FixUrlsInContentToLocal) {
                    value = UpdateImgLocations(value, importSettings);
                }

                if (importSettings.CleanData) {
                    value = RemoveEmptyWhiteSpace(value, "span,p,b");

                    HtmlDocument htmlDocument = new HtmlDocument();
                    htmlDocument.LoadHtml(value);

                    RemoveComments(htmlDocument);
                    RemoveMicrosoftCrap(htmlDocument);

                    if (importSettings.FixUrlsInContentToLocal) {
                        //UpdateUrlTagLocations("title", htmlDocument, importSettings);
                        UpdateUrlTagLocations("href", htmlDocument, importSettings);
                    }

                    RemoveTags(htmlDocument, "br");

                    value = htmlDocument.DocumentNode.OuterHtml;

                    value = RemoveEmptyWhiteSpace(value, "span,p,b");
                }
                return value;
            } catch (Exception exception) {
                Console.WriteLine(exception.ToString());
            }
            return value;
        }
 public void Import(FileInfo fileInfo, ImportSettings importSettings) {
     using (Stream stream = fileInfo.OpenRead()) {
         try {
             ImportStream(importSettings, stream);
         }
         catch (Exception ex) {
             _orchardServices.Notifier.Error(T("An error occured loading your blog, please check permissions and file is accessible, error: {0}", ex.Message));
         }
         finally {
             stream.Close();
         }
     }
 }
        public void Import(ImportSettings importSettings, ContentItem parentContentItem, ICollection<Post> posts, int batchNumber) {

            Console.WriteLine("Started Batch Number {0}", batchNumber);
            int i = 0;
            foreach (var post in posts) {
                _blogPostImportStrategy.Import(importSettings, post, parentContentItem);

                _contentManager.Clear();
                i++;
                Console.WriteLine("Batch Number {0}, Imported record {1}", batchNumber, i);
            }
            Console.WriteLine("Finished Batch Number {0}", batchNumber);
        }
        public void Import(HttpPostedFileBase httpPostedFileBase, ImportSettings importSettings) {
            if (importSettings == null)
                throw new ArgumentNullException("importSettings");

            if (httpPostedFileBase.FileName.EndsWith(".zip")) {
                var blogs = UnzipMediaFileArchiveToBlogMLBlog(httpPostedFileBase, importSettings);
                foreach (var blog in blogs) {
                    ImportBlog(blog, importSettings);
                }
            }
            else if ((httpPostedFileBase.FileName.EndsWith(".xml"))) {
                ImportStream(importSettings, httpPostedFileBase.InputStream);
            }
        }
 public void Import(string urlItemPath, ImportSettings importSettings) {
     using (var client = new WebClient()) {
         using (Stream stream = client.OpenRead(urlItemPath)) {
             try {
                 ImportStream(importSettings, stream);
             }
             catch (Exception ex) {
                 _orchardServices.Notifier.Error(T("An error occured loading your blog, please check permissions and file is accessible, error: {0}", ex.Message));
             }
             finally {
                 if (stream != null)
                     stream.Close();
             }
         }
     }
 }
        public void Import() {
            var importSettings = new ImportSettings();

            importSettings.SelectedSchema = SelectedSchema;

            if (!string.IsNullOrEmpty(DefaultBlogSlug))
                importSettings.DefaultBlogSlug = DefaultBlogSlug;

            if (!string.IsNullOrEmpty(OffendingHosts))
                importSettings.OffendingHosts = OffendingHosts.Split(',');

            if (!string.IsNullOrEmpty(Override))
                importSettings.Override = Boolean.Parse(Override);

            if (!string.IsNullOrEmpty(StartRecordNumber))
                importSettings.StartRecordNumber = Int32.Parse(StartRecordNumber);

            if (!string.IsNullOrEmpty(RecordsToProcess))
                importSettings.RecordsToProcess = Int32.Parse(RecordsToProcess);

            var fileInfo = new FileInfo(Filename);
            
            _importService.Import(fileInfo, importSettings);
        }
 private Blog AssembleBlog(Stream stream, ImportSettings importSettings) {
     return _blogAssemblers.Single(o => o.Name == importSettings.SelectedSchema).Assemble(stream);
 }
 private void ImportStream(ImportSettings importSettings, Stream stream) {
     try {
         var blog = AssembleBlog(stream, importSettings);
         ImportBlog(blog, importSettings);
     }
     catch {
         _orchardServices.Notifier.Error(T("An error occured importing your blog"));
     }
 }
        private IEnumerable<Blog> UnzipMediaFileArchiveToBlogMLBlog(HttpPostedFileBase postedFile, ImportSettings importSettings) {
            var postedFileLength = postedFile.ContentLength;
            var postedFileStream = postedFile.InputStream;
            var postedFileData = new byte[postedFileLength];
            postedFileStream.Read(postedFileData, 0, postedFileLength);

            var blogMlBlogs = new List<Blog>();

            using (var zipStream = new MemoryStream(postedFileData)) {
                using (var fileInflater = ZipFile.Read(zipStream)) {
                    foreach (ZipEntry entry in fileInflater) {
                        if (entry == null) {
                            continue;
                        }
                        if (!entry.IsDirectory && !string.IsNullOrEmpty(entry.FileName) &&
                            entry.FileName.EndsWith(".xml")) {
                            using (var stream = entry.OpenReader()) {
                                blogMlBlogs.Add(AssembleBlog(stream, importSettings));
                            }
                        }
                    }
                }
            }

            return blogMlBlogs.ToArray();
        }
        private void ImportBlog(Blog blog, ImportSettings importSettings) {
            _blogImportStrategy.Import(importSettings, blog, null);

            RebuildOrchardIndexes();

            _orchardServices.Notifier.Information(T("Blog Import has completed successfully. All errors will be listed below, and you may review report generated via the reports link in the menu at anytime."));
        }
Example #11
0
        private void UpdateUrlTagLocations(string attribute, HtmlDocument html, ImportSettings importSettings) {
            var anchorTags = html.DocumentNode.SelectNodes("//a[@" + attribute + "]");
            if (anchorTags == null)
                return;

            foreach (HtmlNode link in anchorTags) {
                try {
                    HtmlAttribute att = link.Attributes[attribute];
                    var value = att.Value;

                    if (string.IsNullOrWhiteSpace(value))
                        continue;

                    Uri uriPreFormatted;
                    if (!Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out uriPreFormatted)) {
                        //Logger.Debug("Could not create Uri");
                        continue;
                    }

                    if (!uriPreFormatted.IsAbsoluteUri) {
                        //var absolutePublicUrl = _storageProvider.GetPublicUrl(uriPreFormatted.OriginalString.Replace("\r\n", string.Empty).TrimStart('/'));

                        //att.Value = value.Replace(value, absolutePublicUrl);

                        //Logger.Debug(string.Format("Converted absolute Url To: {0}", absolutePublicUrl));

                        //Logger.Debug(string.Format("Is not an absolute path: {0}", uriPreFormatted.OriginalString));
                        continue;
                    }

                    if (!IsHostCorrect(uriPreFormatted, importSettings)) {
                        //Logger.Debug(string.Format("Host Not in offending list: {0}", uriPreFormatted.Host));
                        continue;
                    }

                    att.Value = uriPreFormatted.AbsolutePath;//.TrimStart('/');

                    //Logger.Debug(string.Format("Converted Url To: {0}", absoluteUri));
                } catch (Exception ex) {
                    Logger.Debug("Error converting URI, exception: " + ex.ToString());
                }
            }
        }
Example #12
0
        private static bool IsHostCorrect(Uri uriPreFormatted, ImportSettings importSettings) {
            if (uriPreFormatted.Scheme == "mailto")
                return true;

            if (importSettings.OffendingHosts == null)
                return true;

            return importSettings.OffendingHosts.Contains(uriPreFormatted.Host);
        }
Example #13
0
        private string UpdateImgLocations(string value, ImportSettings importSettings) {
            if (string.IsNullOrEmpty(value))
                return value;

            var matches = Regex.Matches(value, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.Singleline);

            if (matches.Count == 0)
                return value;

            foreach (Match match in matches) {
                var urlPreFormatted = 
                    match.Value.Substring(match.Value.IndexOf("src=\"") + 5, (match.Value.IndexOf("\"", match.Value.IndexOf("src=\"") + 6)) - match.Value.IndexOf("src=\"") - 5);

                Logger.Debug(string.Format("Url Before: {0}", urlPreFormatted));

                Uri uriPreFormatted;
                if (!Uri.TryCreate(urlPreFormatted, UriKind.RelativeOrAbsolute, out uriPreFormatted)) {
                    Logger.Debug("Could not create Uri");
                    continue;
                }

                if (!uriPreFormatted.IsAbsoluteUri) {
                    var absolutePublicUrl = _storageProvider.GetPublicUrl(uriPreFormatted.OriginalString.Replace("\r\n", string.Empty).TrimStart('/'));

                    value = value.Replace(urlPreFormatted, absolutePublicUrl);

                    Logger.Debug(string.Format("Converted absolute Url To: {0}", absolutePublicUrl));

                    Logger.Debug(string.Format("Is not an absolute path: {0}", uriPreFormatted.OriginalString));
                    continue;
                }

                if (!IsHostCorrect(uriPreFormatted, importSettings)) {
                    Logger.Debug(string.Format("Host Not in offending list: {0}", uriPreFormatted.Host));
                    continue;
                }

                var publicUrl = _storageProvider.GetPublicUrl(uriPreFormatted.AbsolutePath.TrimStart('/'));

                var formattedUrl = uriPreFormatted.GetLeftPart(UriPartial.Authority) + publicUrl;

                // New
                var absoluteUri = new Uri(formattedUrl).AbsolutePath;

                value = value.Replace(urlPreFormatted, absoluteUri);

                Logger.Debug(string.Format("Converted Url To: {0}", absoluteUri));

                // Old
                //value = value.Replace(urlPreFormatted, formattedUrl);

                //value = CleanHost(value);

                //Console.WriteLine(string.Format("Url After: {0}", formattedUrl));
            }

            return value;
        }