Last active
March 21, 2020 08:20
-
-
Save nul800sebastiaan/2dfb053cc5523e735e48afbed456f091 to your computer and use it in GitHub Desktop.
ClientDependency WebApi
This file contains hidden or 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.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 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
Does this have to be used in conjunction with Kevin's Health Check for Client Dependency Framework?