123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.IO;
- using System.IO.Compression;
- using System.Net;
- using System.Text;
- namespace FNZCM.ConHost.Ver2
- {
- internal static class ExtensionMethods
- {
- public static string SecondsToHumanRead(this int seconds)
- {
- var s = TimeSpan.FromSeconds(seconds);
- if (s.TotalHours < 1) return $"{ Math.Floor(s.TotalMinutes):00}:{s.Seconds:00}";
- return $"{ Math.Floor(s.TotalHours):00}:{ s.Minutes:00}:{s.Seconds:00}";
- }
- public static void WriteM3U8Header(this StringBuilder me)
- {
- me.AppendLine("#EXTM3U");
- me.AppendLine("#EXTENC: UTF-8");
- }
- public static string UrlEscape(this string input)
- {
- if (input == null) return null;
- return input
- .Replace("[", "%5B")
- .Replace("]", "%5D")
- .Replace("'", "%27")
- .Replace(" ", "%20")
- .Replace("#", "%23")
- ;
- }
- public static string FormatDuration(this int second)
- {
- var sbd = new StringBuilder();
- var ts = TimeSpan.FromSeconds(second);
- if (ts.TotalHours > 1) sbd.Append($"{ts.TotalHours:00}:");
- sbd.Append($"{ts.Minutes:00}:{ts.Seconds:00}");
- return sbd.ToString();
- }
- public static string FormatFileSize(this long length)
- {
- string[] sizes = { "B", "KB", "MB", "GB", "TB" };
- double len = length;
- int order = 0;
- while (len >= 1024 && order < sizes.Length - 1)
- {
- order++;
- len = len / 1024;
- }
- // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
- // show a single decimal place, and no space.
- string result = $"{len:000.00} {sizes[order]}";
- return result;
- }
- public static void WriteTextUtf8(this HttpListenerContext context, string content, string contentType = Const.TextHtml)
- {
- var bytes = Encoding.UTF8.GetBytes(content);
- context.Response.ContentEncoding = Encoding.UTF8;
- context.Response.ContentType = contentType;
- if (true == context.Request.Headers["Accept-Encoding"]?.Contains("gzip"))
- {
- context.Response.AddHeader("Content-Encoding", "gzip");
- var memoryStream = new MemoryStream(bytes);
- var gZipStream = new GZipStream(context.Response.OutputStream, CompressionMode.Compress, false);
- memoryStream.CopyTo(gZipStream);
- gZipStream.Flush();
- }
- else
- {
- context.Response.OutputStream.Write(bytes);
- }
- }
- }
- }
|