Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#457: Added support for altcover embedded files
- Loading branch information
1 parent
607fa8f
commit 540cfdf
Showing
12 changed files
with
327 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
src/ReportGenerator.Core.Test/Parser/FileReading/AltCoverEmbeddedFileReaderTest.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Palmmedia.ReportGenerator.Core.Parser.FileReading; | ||
using Xunit; | ||
|
||
namespace Palmmedia.ReportGenerator.Core.Test.Parser.FileReading | ||
{ | ||
/// <summary> | ||
/// This is a test class for AltCoverEmbeddedFileReader and is intended | ||
/// to contain all AltCoverEmbeddedFileReader Unit Tests | ||
/// </summary> | ||
public class AltCoverEmbeddedFileReaderTest | ||
{ | ||
[Fact] | ||
public void ValidEncodedFile_CorrectContentAndNoError() | ||
{ | ||
var sut = new AltCoverEmbeddedFileReader("hY7BCoJQEEX3Qf8wS4XoAxIDd21qk62ixWiDCqOJM4+Q6Mta9En9Qs+eGRTS3TyYd++593G7Tydgtd+2olTOI9WmSIzSTjAj7/saY5ORyjwSoTLhdgYR8+m8NqxFzRRqY8g/OGRRKTUVMggh0xFSRhFYG1FMcxqQC/gucfGLezrVJuEiBbH/VQYbLAkuYHcEcB0zxVTWjPrfuELJx00/a70+VtkRszdD+7LhkFuo/4F1CpfgbV6heHB33T6E4DnaB/PKBw5gRz0B"); | ||
|
||
string[] lines = sut.LoadFile("DoesNotMatter", out string error); | ||
Assert.Null(error); | ||
Assert.True(lines.Length > 0); | ||
} | ||
|
||
[Fact] | ||
public void InvalidEncodedFile_InvalidContent_Error() | ||
{ | ||
var sut = new AltCoverEmbeddedFileReader("xyz"); | ||
|
||
string[] lines = sut.LoadFile("DoesNotMatter", out string error); | ||
Assert.NotNull(error); | ||
Assert.Null(lines); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
89 changes: 89 additions & 0 deletions
89
src/ReportGenerator.Core/Parser/FileReading/AltCoverEmbeddedFileReader.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Text; | ||
using Palmmedia.ReportGenerator.Core.Common; | ||
using Palmmedia.ReportGenerator.Core.Properties; | ||
|
||
namespace Palmmedia.ReportGenerator.Core.Parser.FileReading | ||
{ | ||
/// <summary> | ||
/// File reader for reading files from local disk. | ||
/// </summary> | ||
internal class AltCoverEmbeddedFileReader : IFileReader | ||
{ | ||
/// <summary> | ||
/// Line endings to split lines on Windows and Unix. | ||
/// </summary> | ||
private static readonly string[] LineEndings = new string[] | ||
{ | ||
"\r\n", | ||
"\n" | ||
}; | ||
|
||
/// <summary> | ||
/// The Base64 and deflate compressed file. | ||
/// </summary> | ||
private readonly string base64DeflateCompressedFile; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AltCoverEmbeddedFileReader" /> class. | ||
/// </summary> | ||
/// <param name="base64DeflateCompressedFile">The Base64 and deflate compressed file.</param> | ||
public AltCoverEmbeddedFileReader(string base64DeflateCompressedFile) | ||
{ | ||
if (base64DeflateCompressedFile == null) | ||
{ | ||
throw new ArgumentNullException(nameof(base64DeflateCompressedFile)); | ||
} | ||
|
||
this.base64DeflateCompressedFile = base64DeflateCompressedFile; | ||
} | ||
|
||
/// <summary> | ||
/// Loads the file with the given path. | ||
/// </summary> | ||
/// <param name="path">The path.</param> | ||
/// <param name="error">Error message if file reading failed, otherwise <code>null</code>.</param> | ||
/// <returns><code>null</code> if an error occurs, otherwise the lines of the file.</returns> | ||
public string[] LoadFile(string path, out string error) | ||
{ | ||
try | ||
{ | ||
byte[] base64Decoded = Convert.FromBase64String(this.base64DeflateCompressedFile); | ||
byte[] decompressed = this.Decompress(base64Decoded); | ||
string content = Encoding.UTF8.GetString(decompressed); | ||
|
||
string[] lines = content.Split(LineEndings, StringSplitOptions.None); | ||
|
||
error = null; | ||
return lines; | ||
} | ||
catch (Exception ex) | ||
{ | ||
error = string.Format(CultureInfo.InvariantCulture, Resources.ErrorDuringReadingFile, path, ex.GetExceptionMessageForDisplay()); | ||
return null; | ||
} | ||
} | ||
|
||
private byte[] Decompress(byte[] data) | ||
{ | ||
byte[] decompressedArray = null; | ||
using (MemoryStream decompressedStream = new MemoryStream()) | ||
{ | ||
using (MemoryStream compressStream = new MemoryStream(data)) | ||
{ | ||
using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress)) | ||
{ | ||
deflateStream.CopyTo(decompressedStream); | ||
} | ||
} | ||
|
||
decompressedArray = decompressedStream.ToArray(); | ||
} | ||
|
||
return decompressedArray; | ||
} | ||
} | ||
} |
Oops, something went wrong.