Last active
August 29, 2015 14:15
-
-
Save mattiasnorell/1e044eb7eb02fba18a2f to your computer and use it in GitHub Desktop.
EPiServer CMS 7.5 gadget to export a zip-file containing all files for a specific page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Shell.Gadgets; | |
using Ionic.Zip; | |
namespace Website.Web.Gadgets.Controllers | |
{ | |
[Gadget(Name = "Export page files", Title = "Export page files")] | |
[Authorize(Roles = "WebAdmins")] | |
public class ExportPageFilesController : Controller | |
{ | |
private readonly IContentLoader _contentLoader; | |
private readonly ContentAssetHelper _contentAssetHelper; | |
public ExportPageFilesController(IContentLoader contentLoader, ContentAssetHelper contentAssetHelper) | |
{ | |
_contentLoader = contentLoader; | |
_contentAssetHelper = contentAssetHelper; | |
} | |
public ActionResult Index(string url) | |
{ | |
var model = new ExportFileModel() | |
{ | |
Url = url | |
}; | |
if (string.IsNullOrEmpty(url)) return View(model); | |
object page; | |
var pr = Global.UrlRewriteProvider.ConvertToInternal(new UrlBuilder(url), out page); | |
if (pr) | |
{ | |
var pageReference = page as PageReference; | |
if (pageReference != null) | |
{ | |
var folder = _contentAssetHelper.GetAssetFolder(pageReference); | |
if (folder != null) | |
{ | |
model.PageReference = pageReference; | |
model.Files = GetFiles(folder).ToList(); | |
model.TotalFileSize = model.Files.Sum(e => e.FileSize); | |
} | |
} | |
} | |
return View(model); | |
} | |
public void DownloadFiles(string reference) | |
{ | |
var pageReference = PageReference.Parse(reference); | |
if (pageReference == null) return; | |
var folder = _contentAssetHelper.GetAssetFolder(pageReference); | |
if (folder == null) return; | |
var files = GetFiles(folder); | |
CreateZipFile(files); | |
} | |
public void CreateZipFile(IEnumerable<ExportFileItemModel> files) | |
{ | |
using (var zip = new ZipFile()) | |
{ | |
var zipName = String.Format("pagefileexport_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); | |
zip.AlternateEncodingUsage = ZipOption.AsNecessary; | |
foreach (var file in files) | |
{ | |
zip.AddFile(file.Path, "Files").FileName = file.FileName; | |
} | |
Response.Clear(); | |
Response.BufferOutput = false; | |
Response.ContentType = "application/zip"; | |
Response.AddHeader("content-disposition", "attachment; filename=" + zipName); | |
zip.Save(Response.OutputStream); | |
Response.End(); | |
} | |
} | |
private IEnumerable<ExportFileItemModel> GetFiles(ContentAssetFolder folder) | |
{ | |
var appDataBasePath = EPiServer.Framework.Configuration.EPiServerFrameworkSection.Instance.AppData.BasePath; | |
foreach (var item in _contentLoader.GetChildren<MediaData>(folder.ContentLink)) | |
{ | |
var virtualPath = item.BinaryData.ID.AbsolutePath; | |
yield return new ExportFileItemModel | |
{ | |
FileName = item.Name, | |
Path = string.Format(@"{0}\blobs\{1}", appDataBasePath, virtualPath), | |
FileSize = (item.BinaryData != null) ? (item.BinaryData.OpenRead().Length/1024.0)/1024.0 : 0, | |
ContentLink = item.ContentLink | |
}; | |
} | |
} | |
} | |
public class ExportFileModel | |
{ | |
public string Url { get; set; } | |
public List<ExportFileItemModel> Files { get; set; } | |
public PageReference PageReference { get; set; } | |
public double TotalFileSize { get; set; } | |
} | |
public class ExportFileItemModel | |
{ | |
public string FileName { get; set; } | |
public string Path { get; set; } | |
public ContentReference ContentLink { get; set; } | |
public double FileSize { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using EPiServer.Shell.Web.Mvc.Html | |
@model Website.Web.Gadgets.Controllers.ExportFileModel | |
@{ | |
Layout = null; | |
} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width" /> | |
<title>Export page files</title> | |
<style type="text/css"> | |
.gadget-body { | |
padding: 20px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="gadget-body"> | |
@using (Html.BeginGadgetForm("Index")) | |
{ | |
<h2>Page URL</h2> | |
<input name="url" value="@(Model.Url ?? "")" /> | |
<input type="submit" /> | |
} | |
@if (Model != null) | |
{ | |
if (Model.Files != null) | |
{ | |
<h2>Page files</h2> | |
if (Model.PageReference != null) | |
{ | |
<span class="epi-button"> | |
<span class="epi-button-child"> | |
<a class="epi-button-child-item" href='@Url.Action("DownloadFiles", "ExportPageFiles", new {reference = Model.PageReference})'> | |
Download all files (@Model.Files.Count file(s), @Math.Round(Model.TotalFileSize, 2) mb) | |
</a> | |
</span> | |
</span> | |
<br /><br /> | |
} | |
<table> | |
@foreach (var file in Model.Files) | |
{ | |
<tr> | |
<td><a href="@Url.ContentUrl(file.ContentLink)" target="_blank">@file.FileName</a></td> | |
<td>@Math.Round(file.FileSize, 2) mb</td> | |
</tr> | |
} | |
</table> | |
} | |
} | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment