Update zip archive functionality
add download buttons for multiple input/output
This commit is contained in:
+87
-40
@@ -5,6 +5,7 @@ using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using LeafWeb.Core.Utility;
|
||||
|
||||
namespace LeafWeb.Core.Entities
|
||||
{
|
||||
@@ -133,56 +134,102 @@ namespace LeafWeb.Core.Entities
|
||||
|
||||
#endregion
|
||||
|
||||
public override string ToString()
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Id}_{Identifier}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// Contains all output files in a zip
|
||||
/// </summary>
|
||||
public byte[] GetOutputFileZip(LeafOutputFileType? fileType)
|
||||
{
|
||||
using (var compressedFileStream = new MemoryStream())
|
||||
{
|
||||
using (var archive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
|
||||
{
|
||||
foreach (var outputFile in OutputFiles.Where(f => !fileType.HasValue || f.FileType == fileType.Value))
|
||||
{
|
||||
var entry = archive.CreateEntry(outputFile.Filename);
|
||||
using (var originalFileStream = new MemoryStream(outputFile.FileContents.Contents))
|
||||
using (var entryStream = entry.Open())
|
||||
originalFileStream.CopyTo(entryStream);
|
||||
}
|
||||
}
|
||||
return compressedFileStream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Contains all input files in a zip
|
||||
/// </summary>
|
||||
public byte[] GetInputFileZip()
|
||||
{
|
||||
using (var compressedFileStream = new MemoryStream())
|
||||
{
|
||||
using (var archive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
|
||||
{
|
||||
foreach (var inputFile in InputFiles)
|
||||
{
|
||||
var entry = archive.CreateEntry(inputFile.Filename);
|
||||
using (var originalFileStream = new MemoryStream(inputFile.Contents))
|
||||
using (var entryStream = entry.Open())
|
||||
originalFileStream.CopyTo(entryStream);
|
||||
}
|
||||
}
|
||||
return compressedFileStream.ToArray();
|
||||
}
|
||||
{
|
||||
return NewMemoryZipArchive((archive, stream) =>
|
||||
CompressOutputFiles(
|
||||
OutputFiles.Where(f => !fileType.HasValue || f.FileType == fileType.Value),
|
||||
archive));
|
||||
}
|
||||
|
||||
public int GetOutputFileSizeSum()
|
||||
/// <summary>
|
||||
/// Contains all input files in a zip
|
||||
/// </summary>
|
||||
public byte[] GetInputFileZip() =>
|
||||
NewMemoryZipArchive((archive, stream) => CompressInputFiles(InputFiles, archive));
|
||||
|
||||
/// <summary>
|
||||
/// Contains all input files in a zip, ordered into directories
|
||||
/// </summary>
|
||||
public static byte[] GetInputFilesZip(IEnumerable<LeafInput> leafInputs) =>
|
||||
NewMemoryZipArchive((archive, stream) =>
|
||||
{
|
||||
var inputs = leafInputs.ToList();
|
||||
|
||||
foreach (var leafInput in inputs)
|
||||
{
|
||||
var directory = $"{leafInput.Added:yyyy-dd-MM--HH-mm}_{leafInput.Identifier.FilterValidFilename()}";
|
||||
CompressInputFiles(leafInput.InputFiles, archive, directory);
|
||||
}
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Contains all input files in a zip, ordered into directories
|
||||
/// </summary>
|
||||
public static byte[] GetOutputFilesZip_ToUser(IEnumerable<LeafInput> leafInputs) =>
|
||||
NewMemoryZipArchive((archive, stream) =>
|
||||
{
|
||||
var inputs = leafInputs.ToList();
|
||||
|
||||
foreach (var leafInput in inputs)
|
||||
{
|
||||
if (stream.Length > 2560000L)
|
||||
continue;
|
||||
var directory = $"{leafInput.Added:yyyy-dd-MM--HH-mm}_{leafInput.Identifier.FilterValidFilename()}";
|
||||
CompressOutputFiles(leafInput.OutputFiles.Where(f => f.FileType == LeafOutputFileType.ToUser), archive, directory);
|
||||
}
|
||||
});
|
||||
|
||||
private static byte[] NewMemoryZipArchive(Action<ZipArchive, MemoryStream> addFiles)
|
||||
{
|
||||
using (var compressedFileStream = new MemoryStream())
|
||||
using (var archive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
|
||||
{
|
||||
addFiles(archive, compressedFileStream);
|
||||
return compressedFileStream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static long CompressInputFiles(IEnumerable<LeafInputFile> inputFiles, ZipArchive archive, string prefix = "")
|
||||
{
|
||||
var contentsLength = 0L;
|
||||
foreach (var inputFile in inputFiles)
|
||||
{
|
||||
contentsLength += inputFile.Contents.LongLength;
|
||||
var entry = archive.CreateEntry(Path.Combine(prefix, inputFile.Filename));
|
||||
using (var originalFileStream = new MemoryStream(inputFile.Contents))
|
||||
using (var entryStream = entry.Open())
|
||||
originalFileStream.CopyTo(entryStream);
|
||||
}
|
||||
|
||||
return contentsLength;
|
||||
}
|
||||
|
||||
private static long CompressOutputFiles(IEnumerable<LeafOutputFile> outputFiles, ZipArchive archive, string prefix = "")
|
||||
{
|
||||
var contentsLength = 0L;
|
||||
foreach (var outputFile in outputFiles)
|
||||
{
|
||||
contentsLength += outputFile.FileContents.Contents.LongLength;
|
||||
var entry = archive.CreateEntry(Path.Combine(prefix, outputFile.Filename));
|
||||
using (var originalFileStream = new MemoryStream(outputFile.FileContents.Contents))
|
||||
using (var entryStream = entry.Open())
|
||||
originalFileStream.CopyTo(entryStream);
|
||||
}
|
||||
return contentsLength;
|
||||
}
|
||||
|
||||
public int GetOutputFileSizeSum()
|
||||
{
|
||||
return OutputFiles.Sum(o => o.FileContents.Contents.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user