/// <summary>
        /// Exports the Attachments folder contents (including subdirectories) in ZIP format.
        /// </summary>
        /// <returns>A <see cref="FileStreamResult"/> called 'attachments-export-{date}.zip'. This file is saved in the App_Data folder first.
        /// If an error occurs, a <see cref="HttpNotFound"/> result is returned and the error message written to the trace.</returns>
        /// </returns>
        public ActionResult ExportAttachments()
        {
            PageManager manager = new PageManager();
            IEnumerable<PageSummary> pages = manager.AllPages();

            try
            {
                string exportFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"\App_Data", "export");
                Directory.CreateDirectory(exportFolder);

                string zipFilename = string.Format("attachments-export-{0}.zip", DateTime.Now.ToString("yyy-MM-dd-HHss"));
                string zipFullPath = Path.Combine(exportFolder, zipFilename);
                using (ZipFile zip = new ZipFile(zipFullPath))
                {
                    zip.AddDirectory(Server.MapPath(RoadkillSettings.AttachmentsFolder), "Attachments");
                    zip.Save();
                }

                return File(zipFullPath, "application/zip", zipFilename);
            }
            catch (IOException e)
            {
                Log.Warn(e, "Unable to export attachments");
                TempData["Message"] = string.Format(SiteStrings.SiteSettings_Tools_ExportAttachments_Error, e.Message);

                return RedirectToAction("Tools");
            }
        }
Beispiel #2
0
 /// <summary>
 /// Displays all pages in Roadkill.
 /// </summary>
 /// <returns>An <see cref="IEnumerable`PageSummary"/> as the model.</returns>
 public ActionResult AllPages()
 {
     PageManager manager = new PageManager();
     return View(manager.AllPages());
 }
        /// <summary>
        /// Exports the pages of the site as .wiki files, in ZIP format.
        /// </summary>
        /// <returns>A <see cref="FileStreamResult"/> called 'export-{date}.zip'. This file is saved in the App_Data folder first.
        /// If an error occurs, a <see cref="HttpNotFound"/> result is returned and the error message written to the trace.</returns>
        /// </returns>
        public ActionResult ExportAsWikiFiles()
        {
            PageManager manager = new PageManager();
            IEnumerable<PageSummary> pages = manager.AllPages();

            try
            {
                string exportFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"\App_Data", "export");
                Directory.CreateDirectory(exportFolder);

                string zipFilename = string.Format("export-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HHmm"));
                string zipFullPath = Path.Combine(exportFolder, zipFilename);
                using (ZipFile zip = new ZipFile(zipFullPath))
                {
                    int index = 0;
                    List<string> filenames = new List<string>();

                    foreach (PageSummary summary in pages.OrderBy(p => p.Title))
                    {
                        // Ensure the filename is unique as its title based.
                        string filePath = summary.Title.AsValidFilename();
                        if (filenames.Contains(filePath))
                            filePath += (++index) + "";
                        else
                            index = 0;

                        filenames.Add(filePath);

                        filePath = Path.Combine(exportFolder, filePath);
                        filePath += ".wiki";
                        string content = "Tags:" + summary.Tags.SpaceDelimitTags() + "\r\n" + summary.Content;

                        System.IO.File.WriteAllText(filePath, content);
                        zip.AddFile(filePath, "");
                    }

                    zip.Save();
                }

                return File(zipFullPath, "application/zip", zipFilename);
            }
            catch (IOException e)
            {
                Log.Warn(e, "Unable to export wiki content");
                TempData["Message"] = string.Format(SiteStrings.SiteSettings_Tools_ExportContent_Error, e.Message);

                return RedirectToAction("Tools");
            }
        }