Advertisement
Guest User

Untitled

a guest
Nov 29th, 2021
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 3.59 KB | None | 0 0
  1. @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
  2. @{
  3.     // Source from user Splinx
  4.     // https://our.umbraco.com/forum/using-umbraco-and-getting-started/88255-export-media-folder
  5.  
  6.     Layout = null;
  7.  
  8.     // Enter your export-media page ID
  9.     int pageId = YourPageIDHere;
  10.    
  11.     debug = "";
  12.    
  13.     string basepath = HttpContext.Current.Server.MapPath("~/").TrimEnd('\\');
  14.     string savePath = HttpContext.Current.Server.MapPath("~/App_Data/ExportMedia/" + DateTime.Now.Ticks);
  15. }
  16.  
  17. @if(@Model.Content.GetPropertyValue<bool>("enableExport"))
  18. {
  19.     <h1>Export enabled!</h1>
  20.    
  21.     <h2>Paths:</h2>
  22.     <ul>
  23.         <li>basepath = @basepath</li>
  24.         <li>savePath = @savePath</li>
  25.     </ul>
  26.    
  27.     ExportAllMedia(basepath, savePath, null);
  28.    
  29.     <h2>Debug output</h2>
  30.     @Html.Raw(debug);
  31.    
  32.     <h2>Export disabled again</h2>
  33.     var contentItem = ApplicationContext.Services.ContentService.GetById(@pageId);
  34.     contentItem.SetValue("enableExport", false);
  35.     ApplicationContext.Services.ContentService.SaveAndPublish(contentItem);
  36. }
  37.  
  38. else
  39. {
  40.     <h1>Export disabled!</h1>
  41.     <p>Nothing will happen.</p>
  42. }
  43.  
  44. @functions
  45. {
  46.     // read-write variable for debug (I know it's nasty btw - quick and dirty view).
  47.     public static string debug
  48.     {
  49.         get { return HttpContext.Current.Application["debug"] as string; }
  50.         set { HttpContext.Current.Application["debug"] = value; }
  51.     }
  52.  
  53.     private void ExportAllMedia(string basePath, string curFolder, IMedia uNode)
  54.     {
  55.  
  56.         var _contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
  57.         var _mediaService = ApplicationContext.Current.Services.MediaService;
  58.         var folderType = _contentTypeService.GetMediaType("Folder");
  59.  
  60.         Directory.CreateDirectory(curFolder);
  61.         IEnumerable<IMedia> curMedia = null;
  62.  
  63.         if (uNode == null)
  64.         {
  65.             curMedia = _mediaService.GetRootMedia();
  66.         }
  67.         else
  68.         {
  69.             curMedia = uNode.Children();
  70.         }
  71.  
  72.         // loop through folders
  73.         foreach (IMedia media in curMedia.Where(a => a.ContentTypeId == folderType.Id))
  74.         {
  75.             // recurse inward with this folder
  76.             ExportAllMedia(basePath, curFolder + "\\" + media.Name, media);
  77.  
  78.         }
  79.  
  80.         // regular files
  81.         foreach (IMedia media in curMedia.Where(a => a.ContentTypeId != folderType.Id))
  82.         {
  83.             string umbracoFile = media.GetValue<string>(Constants.Conventions.Media.File);
  84.  
  85.             string url = "";
  86.             bool decoded = false;
  87.  
  88.             try
  89.             {
  90.                 var obj = Newtonsoft.Json.Linq.JObject.Parse(umbracoFile);
  91.                 decoded = true;
  92.             }
  93.             catch (Exception ex)
  94.             {
  95.                 decoded = false;
  96.             }
  97.  
  98.             if (decoded == true)
  99.             {
  100.                 url = Newtonsoft.Json.JsonConvert.DeserializeObject<Umbraco.Web.Models.ImageCropDataSet>(umbracoFile).Src;
  101.             }
  102.             else
  103.             {
  104.                 // No JSON so it must be a not cropped file type (e.g. PDF or similar)
  105.                 url = umbracoFile;
  106.             }
  107.  
  108.             string filePath = url.Replace("/", "\\");
  109.             string source = basePath + filePath;
  110.             string destination = Path.Combine(curFolder, Path.GetFileName(filePath));
  111.  
  112.             debug += "Exporting [" + source + "] to [" + destination + "]<br />";
  113.  
  114.             // IMPORTANT - Uncomment this to actually create the export !!!
  115.             System.IO.File.Copy(source, destination, true);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement