Uses easier unzip for ZipDownloader.cs & handles multiple artifacts in DocFxController

This commit is contained in:
Sebastiaan Janssen
2016-05-27 16:00:19 +02:00
parent 26d7b60159
commit 1a65664c6c
2 changed files with 36 additions and 108 deletions
@@ -12,6 +12,7 @@ using Examine;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Umbraco.Core.Logging;
using ZipFile = System.IO.Compression.ZipFile;
namespace OurUmbraco.Documentation.Busineslogic.GithubSourcePull
{
@@ -122,7 +123,7 @@ namespace OurUmbraco.Documentation.Busineslogic.GithubSourcePull
public void Process(string url, string foldername)
{
var zip = Download(url, foldername);
Unzip(zip, foldername, RootFolder);
ZipFile.ExtractToDirectory(zip, foldername);
BuildSitemap(foldername);
//YUCK, this is horrible but unfortunately the way that the doc indexes are setup are not with
@@ -434,88 +435,7 @@ namespace OurUmbraco.Documentation.Busineslogic.GithubSourcePull
return path;
}
private void Unzip(string path, string foldername, string rootFolder)
{
using (var zipInputStream = new ZipInputStream(File.OpenRead(path)))
{
var stopDir = "\\Documentation";
var serverFolder = string.Format("{0}\\{1}", rootFolder, foldername);
if (Directory.Exists(serverFolder))
{
foreach (var folder in Directory.GetDirectories(serverFolder))
Retry.Do(() => Directory.Delete(folder, true), TimeSpan.FromSeconds(1), 5);
foreach (var mdfile in Directory.GetFiles(serverFolder, "*.md"))
Retry.Do(() => File.Delete(mdfile), TimeSpan.FromSeconds(1), 5);
}
else
{
Directory.CreateDirectory(serverFolder);
}
try
{
var stopDirSet = false;
ZipEntry theEntry;
while ((theEntry = zipInputStream.GetNextEntry()) != null)
{
if (IsProjectDocumentation && !stopDirSet)
{
stopDir = Path.GetDirectoryName(theEntry.Name);
stopDirSet = true;
}
var directoryName = Path.GetDirectoryName(theEntry.Name);
var fileName = Path.GetFileName(theEntry.Name);
HttpContext.Current.Trace.Write("git", theEntry.Name + " " + fileName + " - " + directoryName);
if (directoryName != null && stopDir != null && directoryName.Contains(stopDir))
{
var startIndex = directoryName.LastIndexOf(stopDir, StringComparison.Ordinal) +
stopDir.Length;
directoryName = directoryName.Substring(startIndex);
// create directory
Directory.CreateDirectory(serverFolder + directoryName);
if (fileName == string.Empty)
continue;
var filepath = serverFolder + directoryName + "\\" + fileName;
using (var streamWriter = File.Create(filepath))
{
var data = new byte[2048];
while (true)
{
var size = zipInputStream.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
var createEventArgs = new CreateEventArgs { FilePath = filepath };
FireOnCreate(createEventArgs);
}
}
}
catch (Exception ex)
{
LogHelper.Error<ZipDownloader>("Error processing documentation", ex);
}
}
}
private readonly Events _events = new Events();
public static event EventHandler<UpdateEventArgs> OnUpdate;
@@ -23,36 +23,44 @@ namespace OurUmbraco.Documentation.Controllers
var job = model.eventData.jobs.FirstOrDefault();
if (job == null) throw new FormatException("No job found in payload");
var artifact = job.artifacts.FirstOrDefault();
if (artifact == null) throw new FormatException("No artifact found in payload");
var artifacts = job.artifacts;
if (artifacts == null) throw new FormatException("No artifacts found in payload");
using (var client = new HttpClient())
foreach (var artifact in artifacts)
{
var bytes = await client.GetByteArrayAsync(artifact.url);
var docZip = HostingEnvironment.MapPath("~/App_Data/Documentation/csharp/docs.zip");
var docsFolder = HostingEnvironment.MapPath("~/App_Data/Documentation/csharp");
var hostedDocsFolder = HostingEnvironment.MapPath("~/apidocs/csharp");
//clear everything
if (Directory.Exists(docsFolder))
Directory.Delete(docsFolder);
Directory.CreateDirectory(docsFolder);
if (Directory.Exists(hostedDocsFolder))
Directory.Delete(hostedDocsFolder);
Directory.CreateDirectory(hostedDocsFolder);
using (var memStream = new MemoryStream(bytes))
using (var stream = new FileStream(docZip, FileMode.Create))
if (artifact.fileName.Contains("-"))
{
await memStream.CopyToAsync(stream);
var folder = artifact.fileName.Split('-')[0];
using (var client = new HttpClient())
{
var bytes = await client.GetByteArrayAsync(artifact.url);
var docZip = HostingEnvironment.MapPath(string.Format("~/App_Data/Documentation/{0}/{1}", folder, artifact.fileName));
var docsFolder = HostingEnvironment.MapPath(string.Format("~/App_Data/Documentation/{0}", folder));
var hostedDocsFolder = HostingEnvironment.MapPath(string.Format("~/apidocs/{0}", folder));
//clear everything
if (Directory.Exists(docsFolder))
Directory.Delete(docsFolder, true);
Directory.CreateDirectory(docsFolder);
if (Directory.Exists(hostedDocsFolder))
Directory.Delete(hostedDocsFolder, true);
Directory.CreateDirectory(hostedDocsFolder);
using (var memStream = new MemoryStream(bytes))
using (var stream = new FileStream(docZip, FileMode.Create))
{
await memStream.CopyToAsync(stream);
}
ZipFile.ExtractToDirectory(docZip, hostedDocsFolder);
}
}
ZipFile.ExtractToDirectory(docZip, hostedDocsFolder);
return Request.CreateResponse(HttpStatusCode.OK);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}