Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
- @{
- // Source from user Splinx
- // https://our.umbraco.com/forum/using-umbraco-and-getting-started/88255-export-media-folder
- Layout = null;
- // Enter your export-media page ID
- int pageId = YourPageIDHere;
- debug = "";
- string basepath = HttpContext.Current.Server.MapPath("~/").TrimEnd('\\');
- string savePath = HttpContext.Current.Server.MapPath("~/App_Data/ExportMedia/" + DateTime.Now.Ticks);
- }
- @if(@Model.Content.GetPropertyValue<bool>("enableExport"))
- {
- <h1>Export enabled!</h1>
- <h2>Paths:</h2>
- <ul>
- <li>basepath = @basepath</li>
- <li>savePath = @savePath</li>
- </ul>
- ExportAllMedia(basepath, savePath, null);
- <h2>Debug output</h2>
- @Html.Raw(debug);
- <h2>Export disabled again</h2>
- var contentItem = ApplicationContext.Services.ContentService.GetById(@pageId);
- contentItem.SetValue("enableExport", false);
- ApplicationContext.Services.ContentService.SaveAndPublish(contentItem);
- }
- else
- {
- <h1>Export disabled!</h1>
- <p>Nothing will happen.</p>
- }
- @functions
- {
- // read-write variable for debug (I know it's nasty btw - quick and dirty view).
- public static string debug
- {
- get { return HttpContext.Current.Application["debug"] as string; }
- set { HttpContext.Current.Application["debug"] = value; }
- }
- private void ExportAllMedia(string basePath, string curFolder, IMedia uNode)
- {
- var _contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
- var _mediaService = ApplicationContext.Current.Services.MediaService;
- var folderType = _contentTypeService.GetMediaType("Folder");
- Directory.CreateDirectory(curFolder);
- IEnumerable<IMedia> curMedia = null;
- if (uNode == null)
- {
- curMedia = _mediaService.GetRootMedia();
- }
- else
- {
- curMedia = uNode.Children();
- }
- // loop through folders
- foreach (IMedia media in curMedia.Where(a => a.ContentTypeId == folderType.Id))
- {
- // recurse inward with this folder
- ExportAllMedia(basePath, curFolder + "\\" + media.Name, media);
- }
- // regular files
- foreach (IMedia media in curMedia.Where(a => a.ContentTypeId != folderType.Id))
- {
- string umbracoFile = media.GetValue<string>(Constants.Conventions.Media.File);
- string url = "";
- bool decoded = false;
- try
- {
- var obj = Newtonsoft.Json.Linq.JObject.Parse(umbracoFile);
- decoded = true;
- }
- catch (Exception ex)
- {
- decoded = false;
- }
- if (decoded == true)
- {
- url = Newtonsoft.Json.JsonConvert.DeserializeObject<Umbraco.Web.Models.ImageCropDataSet>(umbracoFile).Src;
- }
- else
- {
- // No JSON so it must be a not cropped file type (e.g. PDF or similar)
- url = umbracoFile;
- }
- string filePath = url.Replace("/", "\\");
- string source = basePath + filePath;
- string destination = Path.Combine(curFolder, Path.GetFileName(filePath));
- debug += "Exporting [" + source + "] to [" + destination + "]<br />";
- // IMPORTANT - Uncomment this to actually create the export !!!
- System.IO.File.Copy(source, destination, true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement