Quasar/Server/Core/Helper/Helper.cs

131 lines
4.0 KiB
C#
Raw Normal View History

2014-07-08 12:58:53 +00:00
using System;
using System.IO;
using System.Linq;
2015-03-21 18:14:00 +00:00
using System.Text;
using xServer.Core.Networking;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
namespace xServer.Core.Helper
2014-07-08 12:58:53 +00:00
{
2015-01-13 18:43:55 +00:00
public static class Helper
2014-07-08 12:58:53 +00:00
{
private const string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
2015-01-14 12:15:31 +00:00
private static readonly Random _rnd = new Random(Environment.TickCount);
2015-05-21 18:35:57 +00:00
private static readonly string[] _sizes = {"B", "KB", "MB", "GB"};
private static readonly char[] _illegalChars = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray();
public static bool CheckPathForIllegalChars(string path)
{
return path.Any(c => _illegalChars.Contains(c));
}
2014-07-08 12:58:53 +00:00
public static string GetRandomFilename(int length, string extension)
{
2015-03-21 18:14:00 +00:00
StringBuilder randomName = new StringBuilder(length);
2014-07-08 12:58:53 +00:00
for (int i = 0; i < length; i++)
2015-03-21 18:14:00 +00:00
randomName.Append(CHARS[_rnd.Next(CHARS.Length)]);
2014-07-08 12:58:53 +00:00
2015-03-21 18:14:00 +00:00
return string.Concat(randomName.ToString(), extension);
2014-07-08 12:58:53 +00:00
}
public static string GetRandomName(int length)
{
2015-03-21 18:14:00 +00:00
StringBuilder randomName = new StringBuilder(length);
2014-07-08 12:58:53 +00:00
for (int i = 0; i < length; i++)
2015-03-21 18:14:00 +00:00
randomName.Append(CHARS[_rnd.Next(CHARS.Length)]);
2014-07-08 12:58:53 +00:00
2015-03-21 18:14:00 +00:00
return randomName.ToString();
2014-07-08 12:58:53 +00:00
}
public static int GetNewTransferId(int o = 0)
{
return _rnd.Next(0, int.MaxValue) + o;
}
public static string GetDataSize(long size)
2014-07-08 12:58:53 +00:00
{
double len = size;
int order = 0;
2015-05-21 18:35:57 +00:00
while (len >= 1024 && order + 1 < _sizes.Length)
2014-07-08 12:58:53 +00:00
{
order++;
len = len/1024;
2014-07-08 12:58:53 +00:00
}
2015-05-21 18:35:57 +00:00
return string.Format("{0:0.##} {1}", len, _sizes[order]);
2014-07-08 12:58:53 +00:00
}
public static string GetWindowTitle(string title, Client c)
{
return string.Format("{0} - {1}@{2} [{3}:{4}]", title, c.Value.Username , c.Value.PCName, c.EndPoint.Address.ToString(), c.EndPoint.Port.ToString());
}
public static string GetWindowTitle(string title, int count)
{
return string.Format("{0} [Selected: {1}]", title, count);
}
2015-01-14 12:15:31 +00:00
public static int GetFileIcon(string extension)
2014-07-08 12:58:53 +00:00
{
2015-01-14 12:15:31 +00:00
if (string.IsNullOrEmpty(extension))
return 2;
switch (extension.ToLower())
2014-07-08 12:58:53 +00:00
{
2015-01-14 12:15:31 +00:00
default:
return 2;
case ".exe":
return 3;
case ".txt":
case ".log":
case ".conf":
case ".cfg":
case ".asc":
2015-01-14 12:15:31 +00:00
return 4;
case ".rar":
case ".zip":
case ".zipx":
case ".tar":
case ".tgz":
case ".gz":
2015-01-14 12:15:31 +00:00
case ".s7z":
case ".7z":
case ".bz2":
case ".cab":
case ".zz":
case ".apk":
2015-01-14 12:15:31 +00:00
return 5;
case ".doc":
case ".docx":
case ".odt":
return 6;
case ".pdf":
return 7;
case ".jpg":
case ".jpeg":
case ".png":
case ".bmp":
case ".gif":
case ".ico":
2015-01-14 12:15:31 +00:00
return 8;
case ".mp4":
case ".mov":
case ".avi":
case ".wmv":
case ".mkv":
case ".m4v":
case ".flv":
2015-01-14 12:15:31 +00:00
return 9;
case ".mp3":
case ".wav":
case ".pls":
case ".m3u":
case ".m4a":
2015-01-14 12:15:31 +00:00
return 10;
2014-07-08 12:58:53 +00:00
}
}
public static bool IsRunningOnMono()
{
return Type.GetType("Mono.Runtime") != null;
}
2014-07-08 12:58:53 +00:00
}
}