Improvements for leaf data zipping
This commit is contained in:
+53
-26
@@ -139,77 +139,92 @@ namespace LeafWeb.Core.Entities
|
|||||||
return $"{Id}_{Identifier}";
|
return $"{Id}_{Identifier}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Zip
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains all output files in a zip
|
/// Contains all output files in a zip
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] GetOutputFileZip(LeafOutputFileType? fileType)
|
public byte[] GetOutputFileZip(LeafOutputFileType? fileType)
|
||||||
{
|
{
|
||||||
return NewMemoryZipArchive((archive, stream) =>
|
return NewMemoryZipArchive((archive, stream) =>
|
||||||
CompressOutputFiles(
|
CompressOutputFiles(archive, OutputFiles.Where(f => !fileType.HasValue || f.FileType == fileType.Value)));
|
||||||
OutputFiles.Where(f => !fileType.HasValue || f.FileType == fileType.Value),
|
|
||||||
archive));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains all input files in a zip
|
/// Contains all input files in a zip
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] GetInputFileZip() =>
|
public byte[] GetInputFileZip() =>
|
||||||
NewMemoryZipArchive((archive, stream) => CompressInputFiles(InputFiles, archive));
|
NewMemoryZipArchive((archive, stream) => CompressInputFiles(archive, InputFiles));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains all input files in a zip, ordered into directories
|
/// Contains all input files files in a zip, ordered into directories
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static byte[] GetInputFilesZip(IEnumerable<LeafInput> leafInputs) =>
|
public static byte[] GetInputFilesZip(IEnumerable<LeafInput> leafInputs) =>
|
||||||
NewMemoryZipArchive((archive, stream) =>
|
GetLeafFilesZip(
|
||||||
{
|
leafInputs,
|
||||||
var inputs = leafInputs.ToList();
|
(archive, input, directory) =>
|
||||||
|
CompressInputFiles(archive, input.InputFiles, directory));
|
||||||
foreach (var leafInput in inputs)
|
|
||||||
{
|
|
||||||
if (stream.Length > 2560000L)
|
|
||||||
continue;
|
|
||||||
var directory = $"{leafInput.Added:yyyy-dd-MM--HH-mm}_{leafInput.Identifier.FilterValidFilename()}";
|
|
||||||
CompressInputFiles(leafInput.InputFiles,
|
|
||||||
archive, directory);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains all input files in a zip, ordered into directories
|
/// Contains all output files files in a zip, ordered into directories
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static byte[] GetOutputFilesZip_ToUser(IEnumerable<LeafInput> leafInputs) =>
|
public static byte[] GetOutputFilesZip_ToUser(IEnumerable<LeafInput> leafInputs) =>
|
||||||
|
GetLeafFilesZip(
|
||||||
|
leafInputs,
|
||||||
|
(archive, input, directory) =>
|
||||||
|
CompressOutputFiles(archive, input.OutputFiles
|
||||||
|
.Where(f => f.FileType == LeafOutputFileType.ToUser),
|
||||||
|
directory));
|
||||||
|
|
||||||
|
private static byte[] GetLeafFilesZip(IEnumerable<LeafInput> leafInputs,
|
||||||
|
Action<ZipArchive, LeafInput, string> compressAction) =>
|
||||||
NewMemoryZipArchive((archive, stream) =>
|
NewMemoryZipArchive((archive, stream) =>
|
||||||
{
|
{
|
||||||
var inputs = leafInputs.ToList();
|
var inputs = leafInputs.ToList();
|
||||||
|
|
||||||
|
var leftBehindFiles = new List<string>();
|
||||||
|
|
||||||
foreach (var leafInput in inputs)
|
foreach (var leafInput in inputs)
|
||||||
{
|
{
|
||||||
if (stream.Length > 2560000L)
|
if (stream.Length > 2560000L)
|
||||||
|
{
|
||||||
|
leftBehindFiles.Add(leafInput.Identifier.FilterValidFilename());
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var directory = $"{leafInput.Added:yyyy-dd-MM--HH-mm}_{leafInput.Identifier.FilterValidFilename()}";
|
var directory = $"{leafInput.Added:yyyy-dd-MM--HH-mm}_{leafInput.Identifier.FilterValidFilename()}";
|
||||||
CompressOutputFiles(leafInput.OutputFiles
|
compressAction(archive, leafInput, directory);
|
||||||
.Where(f => f.FileType == LeafOutputFileType.ToUser),
|
}
|
||||||
archive, directory);
|
|
||||||
|
if (leftBehindFiles.Count > 0)
|
||||||
|
{
|
||||||
|
leftBehindFiles.Insert(0, "NOTICE. The following LeafInput were not included due to file size issue.");
|
||||||
|
leftBehindFiles.Insert(1, "---");
|
||||||
|
CompressStringToFile(archive, leftBehindFiles.Join(Environment.NewLine), "README.TXT");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
private static byte[] NewMemoryZipArchive(Action<ZipArchive, MemoryStream> addFiles)
|
private static byte[] NewMemoryZipArchive(Action<ZipArchive, MemoryStream> addFiles)
|
||||||
{
|
{
|
||||||
using (var compressedFileStream = new MemoryStream())
|
using (var compressedFileStream = new MemoryStream())
|
||||||
|
{
|
||||||
using (var archive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
|
using (var archive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
|
||||||
{
|
{
|
||||||
addFiles(archive, compressedFileStream);
|
addFiles(archive, compressedFileStream);
|
||||||
|
}
|
||||||
|
// ZipArchive must be disposed before returning the data
|
||||||
return compressedFileStream.ToArray();
|
return compressedFileStream.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long CompressInputFiles(IEnumerable<LeafInputFile> inputFiles, ZipArchive archive, string prefix = "")
|
private static long CompressInputFiles(ZipArchive archive, IEnumerable<LeafInputFile> inputFiles,
|
||||||
|
string directory = "")
|
||||||
{
|
{
|
||||||
var contentsLength = 0L;
|
var contentsLength = 0L;
|
||||||
foreach (var inputFile in inputFiles)
|
foreach (var inputFile in inputFiles)
|
||||||
{
|
{
|
||||||
contentsLength += inputFile.Contents.LongLength;
|
contentsLength += inputFile.Contents.LongLength;
|
||||||
var entry = archive.CreateEntry(Path.Combine(prefix, inputFile.Filename));
|
var entry = archive.CreateEntry(Path.Combine(directory, inputFile.Filename));
|
||||||
using (var originalFileStream = new MemoryStream(inputFile.Contents))
|
using (var originalFileStream = new MemoryStream(inputFile.Contents))
|
||||||
using (var entryStream = entry.Open())
|
using (var entryStream = entry.Open())
|
||||||
originalFileStream.CopyTo(entryStream);
|
originalFileStream.CopyTo(entryStream);
|
||||||
@@ -218,13 +233,14 @@ namespace LeafWeb.Core.Entities
|
|||||||
return contentsLength;
|
return contentsLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long CompressOutputFiles(IEnumerable<LeafOutputFile> outputFiles, ZipArchive archive, string prefix = "")
|
private static long CompressOutputFiles(ZipArchive archive, IEnumerable<LeafOutputFile> outputFiles,
|
||||||
|
string directory = "")
|
||||||
{
|
{
|
||||||
var contentsLength = 0L;
|
var contentsLength = 0L;
|
||||||
foreach (var outputFile in outputFiles)
|
foreach (var outputFile in outputFiles)
|
||||||
{
|
{
|
||||||
contentsLength += outputFile.FileContents.Contents.LongLength;
|
contentsLength += outputFile.FileContents.Contents.LongLength;
|
||||||
var entry = archive.CreateEntry(Path.Combine(prefix, outputFile.Filename));
|
var entry = archive.CreateEntry(Path.Combine(directory, outputFile.Filename));
|
||||||
using (var originalFileStream = new MemoryStream(outputFile.FileContents.Contents))
|
using (var originalFileStream = new MemoryStream(outputFile.FileContents.Contents))
|
||||||
using (var entryStream = entry.Open())
|
using (var entryStream = entry.Open())
|
||||||
originalFileStream.CopyTo(entryStream);
|
originalFileStream.CopyTo(entryStream);
|
||||||
@@ -232,9 +248,20 @@ namespace LeafWeb.Core.Entities
|
|||||||
return contentsLength;
|
return contentsLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void CompressStringToFile(ZipArchive archive, string contents, string filename)
|
||||||
|
{
|
||||||
|
var entry = archive.CreateEntry(filename);
|
||||||
|
using (var writer = new StreamWriter(entry.Open()))
|
||||||
|
{
|
||||||
|
writer.Write(contents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int GetOutputFileSizeSum()
|
public int GetOutputFileSizeSum()
|
||||||
{
|
{
|
||||||
return OutputFiles.Sum(o => o.FileContents.Contents.Length);
|
return OutputFiles.Sum(o => o.FileContents.Contents.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user