Change ICompressor implement Compress for a Stream
This commit is contained in:
parent
2131edb4c8
commit
364c0fda02
6 changed files with 75 additions and 13 deletions
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
37
ImageCore.Tests/TestNullCompressor.cs
Normal file
37
ImageCore.Tests/TestNullCompressor.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,5 +6,10 @@
|
||||||
</AssemblyExplorer></s:String>
|
</AssemblyExplorer></s:String>
|
||||||
|
|
||||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=761234b5_002D46ba_002D4312_002Dab60_002Dd15d5d83a61a/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="TestGetFilenamesFromPath" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=761234b5_002D46ba_002D4312_002Dab60_002Dd15d5d83a61a/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="TestGetFilenamesFromPath" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||||
|
<Or>
|
||||||
<Project Location="C:\Users\nutiu\RiderProjects\ImgMetadataRemover\ImageCore.Tests" Presentation="&lt;ImageCore.Tests&gt;" />
|
<Project Location="C:\Users\nutiu\RiderProjects\ImgMetadataRemover\ImageCore.Tests" Presentation="&lt;ImageCore.Tests&gt;" />
|
||||||
|
<TestAncestor>
|
||||||
|
<TestId>xUnit::B915AC83-B6E9-4E0A-BA88-915F629F57C8::net6.0::ConsoleInterface.Tests.TestSimpleOutputSink</TestId>
|
||||||
|
</TestAncestor>
|
||||||
|
</Or>
|
||||||
</SessionState></s:String></wpf:ResourceDictionary>
|
</SessionState></s:String></wpf:ResourceDictionary>
|
Loading…
Reference in a new issue