1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-26 17:47:08 +00:00

Actually fixed size parsing this time

This commit is contained in:
Mark McDowall 2013-07-17 00:33:54 -07:00
parent 8921c45a96
commit a9dd2d2f04
3 changed files with 14 additions and 6 deletions

View file

@ -26,7 +26,7 @@ public void parse_releaseGroup(string title, string expected)
[TestCase("7,162.1MB", 7510006170)] [TestCase("7,162.1MB", 7510006170)]
[TestCase("162.1MB", 169974170)] [TestCase("162.1MB", 169974170)]
[TestCase("398.62 MB", 417983365)] [TestCase("398.62 MB", 417983365)]
[TestCase("845 MB", 1073741824)] [TestCase("845 MB", 886046720)]
public void parse_size(string sizeString, long expectedSize) public void parse_size(string sizeString, long expectedSize)
{ {
var result = BasicRssParser.GetReportSize(sizeString); var result = BasicRssParser.GetReportSize(sizeString);

View file

@ -26,12 +26,12 @@ public static string WithDefault(this string actual, object defaultValue)
public static Int64 Megabytes(this int megabytes) public static Int64 Megabytes(this int megabytes)
{ {
return megabytes * 1048576L; return Convert.ToInt64(megabytes * 1024L *1024L);
} }
public static Int64 Gigabytes(this int gigabytes) public static Int64 Gigabytes(this int gigabytes)
{ {
return gigabytes * 1073741824L; return Convert.ToInt64(gigabytes * 1024L * 1024L * 1024L);
} }
public static string ToBestDateString(this DateTime dateTime) public static string ToBestDateString(this DateTime dateTime)

View file

@ -133,7 +133,7 @@ public static string ParseHeader(string header)
return header; return header;
} }
private static readonly Regex ReportSizeRegex = new Regex(@"(?<value>\d+\.\d{1,2}|\d+)\W?(?<unit>GB|MB|GiB|MiB)", private static readonly Regex ReportSizeRegex = new Regex(@"(?<value>\d+\.\d{1,2}|\d+\,\d+\.\d{1,2}|\d+)\W?(?<unit>GB|MB|GiB|MiB)",
RegexOptions.IgnoreCase | RegexOptions.Compiled); RegexOptions.IgnoreCase | RegexOptions.Compiled);
@ -151,16 +151,24 @@ public static long GetReportSize(string sizeString)
if (unit.Equals("MB", StringComparison.InvariantCultureIgnoreCase) || if (unit.Equals("MB", StringComparison.InvariantCultureIgnoreCase) ||
unit.Equals("MiB", StringComparison.InvariantCultureIgnoreCase)) unit.Equals("MiB", StringComparison.InvariantCultureIgnoreCase))
{ {
return Convert.ToInt32(value).Megabytes(); return ConvertToBytes(Convert.ToDouble(value), 2);
} }
if (unit.Equals("GB", StringComparison.InvariantCultureIgnoreCase) || if (unit.Equals("GB", StringComparison.InvariantCultureIgnoreCase) ||
unit.Equals("GiB", StringComparison.InvariantCultureIgnoreCase)) unit.Equals("GiB", StringComparison.InvariantCultureIgnoreCase))
{ {
return Convert.ToInt32(value).Gigabytes(); return ConvertToBytes(Convert.ToDouble(value), 3);
} }
} }
return 0; return 0;
} }
private static long ConvertToBytes(double value, int power)
{
var multiplier = Math.Pow(1024, power);
var result = value*multiplier;
return Convert.ToInt64(result);
}
} }
} }