Change ICompressor implement Compress for a Stream

This commit is contained in:
Denis-Cosmin Nutiu 2022-04-03 18:12:49 +03:00
parent 2131edb4c8
commit 364c0fda02
6 changed files with 75 additions and 13 deletions

View file

@ -34,22 +34,19 @@ namespace ImageCore.Tests
} }
[Fact] [Fact]
public void TestNullCompressor_Compress() public void TestLosslessCompressor_Compress_Stream()
{ {
ICompressor compressor = new NullCompressor(); ICompressor compressor = new LosslessCompressor();
var sourceFileName = Path.Combine(_testsProjectDirectory, "test_pictures/IMG_0138.HEIC"); var sourceFileName = Path.Combine(_testsProjectDirectory, "test_pictures/IMG_0138.HEIC");
var destinationFileName = Path.GetTempFileName(); var destinationFileName = Path.GetTempFileName();
File.Copy(sourceFileName, destinationFileName, true); File.Copy(sourceFileName, destinationFileName, true);
compressor.Compress(destinationFileName);
var originalFile = File.Open(sourceFileName, FileMode.Open); var destinationFileHandle = File.Open(destinationFileName, FileMode.Open);
var compressedFile = File.Open(destinationFileName, FileMode.Open); var lengthBeforeCompression = destinationFileHandle.Length;
Assert.True(compressedFile.Length == originalFile.Length); compressor.Compress(destinationFileHandle);
originalFile.Close(); Assert.True(destinationFileHandle.Length < lengthBeforeCompression);
compressedFile.Close();
File.Delete(destinationFileName);
} }
} }
} }

View file

@ -0,0 +1,37 @@
using System;
using System.IO;
using Image.Core;
using Xunit;
namespace ImageCore.Tests
{
public class TestNullCompressor
{
private readonly string _testsProjectDirectory;
public TestNullCompressor()
{
_testsProjectDirectory = Environment.GetEnvironmentVariable("IMAGE_CORE_TESTS");
}
[Fact]
public void TestNullCompressor_Compress()
{
ICompressor compressor = new NullCompressor();
var sourceFileName = Path.Combine(_testsProjectDirectory, "test_pictures/IMG_0138.HEIC");
var destinationFileName = Path.GetTempFileName();
File.Copy(sourceFileName, destinationFileName, true);
compressor.Compress(destinationFileName);
var originalFile = File.Open(sourceFileName, FileMode.Open);
var compressedFile = File.Open(destinationFileName, FileMode.Open);
Assert.True(compressedFile.Length == originalFile.Length);
originalFile.Close();
compressedFile.Close();
File.Delete(destinationFileName);
}
}
}

View file

@ -1,4 +1,6 @@
namespace Image.Core using System.IO;
namespace Image.Core
{ {
/// <summary> /// <summary>
/// ICompressor is an interface for implementing image compressors. /// ICompressor is an interface for implementing image compressors.
@ -10,5 +12,11 @@
/// </summary> /// </summary>
/// <param name="fileName">The file name of the image to be compressed.</param> /// <param name="fileName">The file name of the image to be compressed.</param>
void Compress(string fileName); void Compress(string fileName);
/// <summary>
/// The method compresses an image in place.
/// </summary>
/// <param name="stream">The stream of the image to be compressed.</param>
void Compress(Stream stream);
} }
} }

View file

@ -1,4 +1,5 @@
using ImageMagick; using System.IO;
using ImageMagick;
namespace Image.Core namespace Image.Core
{ {
@ -22,5 +23,13 @@ namespace Image.Core
{ {
_imageOptimizer.LosslessCompress(fileName); _imageOptimizer.LosslessCompress(fileName);
} }
/// <summary>
/// <inheritdoc />
/// </summary>
public void Compress(Stream stream)
{
_imageOptimizer.LosslessCompress(stream);
}
} }
} }

View file

@ -1,4 +1,6 @@
namespace Image.Core using System.IO;
namespace Image.Core
{ {
/// <summary> /// <summary>
/// Does nothing. Using this Compressor will have no effect. /// Does nothing. Using this Compressor will have no effect.
@ -13,5 +15,9 @@
public void Compress(string fileName) public void Compress(string fileName)
{ {
} }
public void Compress(Stream stream)
{
}
} }
} }

View file

@ -6,5 +6,10 @@
&lt;/AssemblyExplorer&gt;</s:String> &lt;/AssemblyExplorer&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=761234b5_002D46ba_002D4312_002Dab60_002Dd15d5d83a61a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="TestGetFilenamesFromPath" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD; <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=761234b5_002D46ba_002D4312_002Dab60_002Dd15d5d83a61a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="TestGetFilenamesFromPath" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Or&gt;&#xD;
&lt;Project Location="C:\Users\nutiu\RiderProjects\ImgMetadataRemover\ImageCore.Tests" Presentation="&amp;lt;ImageCore.Tests&amp;gt;" /&gt;&#xD; &lt;Project Location="C:\Users\nutiu\RiderProjects\ImgMetadataRemover\ImageCore.Tests" Presentation="&amp;lt;ImageCore.Tests&amp;gt;" /&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::B915AC83-B6E9-4E0A-BA88-915F629F57C8::net6.0::ConsoleInterface.Tests.TestSimpleOutputSink&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/Or&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary> &lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>