Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Last active March 21, 2020 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nul800sebastiaan/2dfb053cc5523e735e48afbed456f091 to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/2dfb053cc5523e735e48afbed456f091 to your computer and use it in GitHub Desktop.
ClientDependency WebApi
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Hosting;
using System.Web.Http;
using Umbraco.Web.WebApi;
namespace My.Namespace
{
public class TempFileController : UmbracoApiController
{
private readonly string _cdfDirectory = ClientDependency.Core.Config.ClientDependencySettings.Instance.
DefaultCompositeFileProcessingProvider.CompositeFilePath.FullName;
[HttpGet]
// Link: /Umbraco/Api/TempFile/GetCDFFiles
public IEnumerable<string> GetCDFFiles()
{
var result = new List<string> { _cdfDirectory };
foreach (var file in new DirectoryInfo(_cdfDirectory).GetFiles("*.*"))
{
result.Add(file.FullName);
}
return result;
}
[HttpGet]
// Link: /Umbraco/Api/TempFile/GetCDFTempFiles
public HttpResponseMessage GetCDFTempFiles()
{
var tempPath = _cdfDirectory;
var fileName = "cdftemp.zip";
var zipPath = HostingEnvironment.MapPath("~/App_Data") + "\\" + fileName;
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
ZipFile.CreateFromDirectory(tempPath, zipPath);
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(new FileStream(zipPath, FileMode.Open, FileAccess.Read))
};
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{ FileName = fileName };
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
return response;
}
[HttpGet]
// Link: /Umbraco/Api/TempFile/DeleteCDFTempFiles
public IEnumerable<string> DeleteCDFTempFiles()
{
var result = new List<string>();
foreach (var file in new DirectoryInfo(_cdfDirectory).GetFiles("*.*"))
{
file.Delete();
result.Add(file.FullName);
}
return result;
}
}
}
@bobi33
Copy link

bobi33 commented Mar 20, 2020

Does this have to be used in conjunction with Kevin's Health Check for Client Dependency Framework?

@nul800sebastiaan
Copy link
Author

@bobi33 You can use Kevin's health check too, which doesn't allow you to download the files before deleting them.

Note: Delete this immediately when you're done. Anybody can try these endpoints on your site as long as this file is in your App_Code directory!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment