-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from dabutvin/threshold
configuration option for compression threshold (fixes #562)
- Loading branch information
Showing
8 changed files
with
174 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Linq; | ||
using Common; | ||
|
||
namespace CompressImagesFunction | ||
{ | ||
public class Threshold | ||
{ | ||
/// <summary> | ||
/// Using the compressionResults and the repoConfiguration determine whether | ||
/// the optimization warrants a PR at this time. | ||
/// </summary> | ||
/// <returns>True when the images are compressed enough to warrant a PR.</returns> | ||
public static bool MeetsThreshold(RepoConfiguration repoConfiguration, CompressionResult[] compressionResults) | ||
{ | ||
if (repoConfiguration.MinKBReduced == null || repoConfiguration.MinKBReduced <= 0) | ||
{ | ||
// no threshold specified - let's continue | ||
return true; | ||
} | ||
|
||
// determine total KB reduced | ||
var totalKBReduced = compressionResults.Sum(x => x.SizeBefore - x.SizeAfter); | ||
return repoConfiguration.MinKBReduced <= totalKBReduced; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
You can set a space saved threshold using the `.imgbotconfig` file. | ||
|
||
- This configuration is optional and is only required if you want to change the default threshold | ||
- Default setting is 10KB | ||
- Accepts only numbers as input (e.g. `"minKBReduced": 500` for a 500 KB threshold) | ||
- Can be used to limit the frequency of PRs Imgbot will open over time | ||
|
||
`.imgbotconfig` | ||
|
||
Setting 500 KB threshold | ||
|
||
``` | ||
{ | ||
"minKBReduced": 500 | ||
} | ||
``` | ||
|
||
To disable this threshold and always open a PR no matter how much size is reduced unset the default | ||
``` | ||
{ | ||
"minKBReduced": null | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
# ImgBot | ||
# Imgbot | ||
|
||
ImgBot crawls all your image files in GitHub and submits pull requests after applying a lossless compression. | ||
Imgbot crawls all your image files in GitHub and submits pull requests after applying a lossless compression. | ||
This will make the file size go down, but leave the dimensions and quality just as good. | ||
|
||
![screenshot](https://imgbot.net/images/screen.png?cache=2) | ||
|
||
## Configuration | ||
|
||
ImgBot supports optional configuration through a `.imgbotconfig` json file. | ||
This is not a required step to using ImgBot and is only for more advanced scenarios. | ||
Imgbot supports optional configuration through a `.imgbotconfig` json file. | ||
This is not a required step to using Imgbot and is only for more advanced scenarios. | ||
This file should be placed in the root of the repository and set to your liking. | ||
|
||
``` | ||
|
@@ -20,7 +20,8 @@ This file should be placed in the root of the repository and set to your liking. | |
"public/special_images/*", // by folderpath | ||
], | ||
"aggressiveCompression": "true", // true|false | ||
"compressWiki": "true" // true|false | ||
"compressWiki": "true", // true|false | ||
"minKBReduced": 500 // set reduction threshold (default to 10) | ||
} | ||
``` | ||
|
||
|
@@ -33,14 +34,14 @@ to [email protected] | |
|
||
- optional | ||
- Accepts: daily|weekly|monthly | ||
- Limits the PRs from ImgBot to once a day, once a week, or once a month respectively | ||
- The default behavior is to receive ImgBot PRs as images require optimization | ||
- Limits the PRs from Imgbot to once a day, once a week, or once a month respectively | ||
- The default behavior is to receive Imgbot PRs as images require optimization | ||
|
||
**ignoredFiles** | ||
|
||
- optional | ||
- Accepts the syntax for searchPattern on [Directory.EnumerateFiles()](https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles) | ||
- Limits the images optimized by ImgBot by esentially ignoring them | ||
- Limits the images optimized by Imgbot by esentially ignoring them | ||
- When ignoring by filename no path is necessary, when ignoring by foldername full path from root is necessary | ||
|
||
**aggressiveCompression** | ||
|
@@ -58,10 +59,18 @@ to [email protected] | |
- Example: `https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.wiki.git` | ||
- The default behavior is opt out | ||
|
||
|
||
**minKBReduced** | ||
|
||
- optional | ||
- Accepts only numbers as input (e.g. `"minKBReduced": 500` for a 500 KB threshold) | ||
- Can be used to limit the frequency of PRs Imgbot will open over time | ||
- The default setting is 10 | ||
|
||
Find out more: https://imgbot.net/docs | ||
|
||
## Contributing | ||
|
||
All the code for ImgBot is available on GitHub. We will gladly accept contributions for the service, the website, and the documentation. This is where you can find out how to get set up to run locally as well as detailed information on exactly how ImgBot works. | ||
All the code for Imgbot is available on GitHub. We will gladly accept contributions for the service, the website, and the documentation. This is where you can find out how to get set up to run locally as well as detailed information on exactly how Imgbot works. | ||
|
||
https://imgbot.net/docs#contributing |
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,92 @@ | ||
using Common; | ||
using CompressImagesFunction; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Test | ||
{ | ||
[TestClass] | ||
public class ThresholdTests | ||
{ | ||
/// We have a default threshold set so it won't meet it by default | ||
[TestMethod] | ||
public void GivenDefaultConfiguration_ShouldNotOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] { }; | ||
var configuration = new RepoConfiguration(); | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsFalse(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void GivenDisabledConfiguration_ShouldOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] { }; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = null | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsTrue(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void Given0_ShouldOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] { }; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = 0 | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsTrue(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void GivenBelowThreshold_ShouldOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] | ||
{ | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4000, | ||
}, | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4999, | ||
}, | ||
}; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = 500 | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsTrue(shouldOptimize); | ||
} | ||
|
||
[TestMethod] | ||
public void GivenAboveThreshold_ShouldNotOptimizeImages() | ||
{ | ||
var compressionResults = new CompressionResult[] | ||
{ | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4900, | ||
}, | ||
new CompressionResult | ||
{ | ||
SizeBefore = 5000, | ||
SizeAfter = 4999, | ||
}, | ||
}; | ||
var configuration = new RepoConfiguration | ||
{ | ||
MinKBReduced = 500 | ||
}; | ||
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults); | ||
Assert.IsFalse(shouldOptimize); | ||
} | ||
} | ||
} |
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