mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
assorted: move getbytes to parseutil and add tests (#14076)
This commit is contained in:
@@ -104,5 +104,43 @@ namespace Jackett.Common.Utils
|
||||
var imdbLen = ((int)imdbid > 9999999) ? "D8" : "D7";
|
||||
return "tt" + ((int)imdbid).ToString(imdbLen);
|
||||
}
|
||||
|
||||
// ex: " 3.5 gb " -> "3758096384" , "3,5GB" -> "3758096384" , "296,98 MB" -> "311406100.48" , "1.018,29 MB" -> "1067754455.04"
|
||||
// ex: "1.018.29mb" -> "1067754455.04" , "-" -> "0" , "---" -> "0"
|
||||
public static long GetBytes(string str)
|
||||
{
|
||||
var valStr = new string(str.Where(c => char.IsDigit(c) || c == '.' || c == ',').ToArray());
|
||||
valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", ".");
|
||||
if (valStr.Count(c => c == '.') > 1)
|
||||
{
|
||||
var lastOcc = valStr.LastIndexOf('.');
|
||||
valStr = valStr.Substring(0, lastOcc).Replace(".", string.Empty) + valStr.Substring(lastOcc);
|
||||
}
|
||||
var unit = new string(str.Where(char.IsLetter).ToArray());
|
||||
var val = CoerceFloat(valStr);
|
||||
return GetBytes(unit, val);
|
||||
}
|
||||
|
||||
public static long GetBytes(string unit, float value)
|
||||
{
|
||||
unit = unit.Replace("i", "").ToLowerInvariant();
|
||||
if (unit.Contains("kb"))
|
||||
return BytesFromKB(value);
|
||||
if (unit.Contains("mb"))
|
||||
return BytesFromMB(value);
|
||||
if (unit.Contains("gb"))
|
||||
return BytesFromGB(value);
|
||||
if (unit.Contains("tb"))
|
||||
return BytesFromTB(value);
|
||||
return (long)value;
|
||||
}
|
||||
|
||||
public static long BytesFromTB(float tb) => BytesFromGB(tb * 1024f);
|
||||
|
||||
public static long BytesFromGB(float gb) => BytesFromMB(gb * 1024f);
|
||||
|
||||
public static long BytesFromMB(float mb) => BytesFromKB(mb * 1024f);
|
||||
|
||||
public static long BytesFromKB(float kb) => (long)(kb * 1024f);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user