diff --git a/Apewer.Run/FileRenamer.cs b/Apewer.Run/FileRenamer.cs new file mode 100644 index 0000000..af87ce6 --- /dev/null +++ b/Apewer.Run/FileRenamer.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Apewer.Run +{ + + class FileRenamer + { + + public FileRenamer() + { + // RenameZQSV(); + + var paths = StorageUtility.GetSubFiles(@"Y:\telecast\芸汐传", true); + foreach (var path in paths) + { + var name = Path.GetFileName(path); + + // name = name.Replace(" ", ""); + name = name.Replace(".qsv.flv.mp4", ".mp4"); + name = name.Replace(".qsv.flv.mp4", ""); + name = name.Replace("【4K】", ""); + + var split = name.Split(' '); + split[1] = split[1].Replace("第", "").Replace("集", ""); + if (split[1].Length == 1) split[1] = "0" + split[1]; + split[1] = "第 " + split[1] + " 集"; + + name = string.Join(" - ", split) + ".mp4"; + + var newPath = Path.Combine(Path.GetDirectoryName(path), name); + if (newPath != path) + { + File.Move(path, newPath); + Console.WriteLine($"{path} -> {newPath}"); + } + // else Console.WriteLine(path); + } + } + + void RenameZQSV() + { + var paths = Apewer.StorageUtility.GetSubFiles("z:\\", true); + foreach (var path in paths) + { + RenameFile(path, " ", "_"); + RenameFile(path, "【蓝光1080P】", ""); + } + } + + void RenameFile(string path, string oldSub, string newSub) + { + if (path.Contains(oldSub)) + { + var newName = path.Replace(oldSub, newSub); + File.Move(path, newName); + Console.WriteLine($"{path} -> {newName}"); + } + else + { + Console.WriteLine(path); + } + } + + } + +} diff --git a/Apewer.Run/HashComputer.cs b/Apewer.Run/HashComputer.cs new file mode 100644 index 0000000..73c2e7d --- /dev/null +++ b/Apewer.Run/HashComputer.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Apewer.Run +{ + + class HashComputer + { + + public HashComputer() + { + var path = @"Y:\telecast\新白娘子传奇"; + var subs = StorageUtility.GetSubFiles(path, true); + subs.Sort(); + var array = Json.NewArray(); + foreach (var sub in subs) + { + var md5 = ""; + using (var file = StorageUtility.OpenFile(sub)) + { + var info = new FileInfo(sub); + var length = info.Length; + md5 = BinaryUtility.MD5(file, (p) => + { + Console.WriteLine($"{sub} total={length} progress={p}"); + }).ToX2(); + } + + Console.WriteLine($"{sub} md5={md5}"); + + var item = Json.NewObject(); + item["path"] = sub; + item["md5"] = md5; + array.AddItem(item); + } + + StorageUtility.WriteFile(path + ".json", array.ToString(true).ToBinary()); + } + + } + +} diff --git a/Apewer.Run/_Program.cs b/Apewer.Run/_Program.cs index 86e3f3c..d7f348a 100644 --- a/Apewer.Run/_Program.cs +++ b/Apewer.Run/_Program.cs @@ -1,6 +1,7 @@ using Apewer; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Reflection; namespace Apewer.Run @@ -17,6 +18,9 @@ namespace Apewer.Run // RunPublicClass(args); + // new FileRenamer(); + // new HashComputer(); + Console.WriteLine("ended"); Console.ReadKey(); } diff --git a/Apewer/Apewer.csproj b/Apewer/Apewer.csproj index 9bedd6f..3ab6c77 100644 --- a/Apewer/Apewer.csproj +++ b/Apewer/Apewer.csproj @@ -7,7 +7,7 @@ Apewer Apewer Apewer - 6.0.8 + 6.0.9 diff --git a/Apewer/Network/SslUtility.cs b/Apewer/Network/SslUtility.cs index c76c9ce..d9b7947 100644 --- a/Apewer/Network/SslUtility.cs +++ b/Apewer/Network/SslUtility.cs @@ -19,7 +19,7 @@ namespace Apewer.Network set { ServicePointManager.ServerCertificateValidationCallback = value; } } - /// 证书验证。 + /// 证书验证。忽略所有错误。 public static bool ApproveAll(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; @@ -39,7 +39,7 @@ namespace Apewer.Network return null; } - /// 证书验证。 + /// 证书验证。要求必须有正确的证书。 public static bool ApproveRight(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return (errors == SslPolicyErrors.None) ? true : false; diff --git a/Apewer/TextUtility.cs b/Apewer/TextUtility.cs index a73a6c9..f871f7e 100644 --- a/Apewer/TextUtility.cs +++ b/Apewer/TextUtility.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Data; using System.IO; +using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; @@ -20,22 +21,13 @@ namespace Apewer public const string EmptyString = Constant.EmptyString; /// 合并为字符串。 - public static string Merge(params object[] cells) - { - return Join(null, cells); - } + public static string Merge(params object[] cells) => Join(null, cells); /// 合并为字符串。 - public static string Merge(IEnumerable cells) - { - return Join(null, (IEnumerable)cells); - } + public static string Merge(IEnumerable cells) => Join(null, (IEnumerable)cells); /// 合并为字符串。 - public static string Join(string separator, params object[] cells) - { - return Join(separator, (IEnumerable)cells); - } + public static string Join(string separator, params object[] cells) => Join(separator, (IEnumerable)cells); /// 合并为字符串。 public static string Join(string separator, IEnumerable cells) @@ -62,22 +54,13 @@ namespace Apewer /// 重复指定子字符串,直到达到指定长度。 /// 子字符串。 /// 目标字符串的长度。 - public static string CopyChar(string cell, int length) - { - return TextGenerator.CopyChar(cell, length); - } + public static string CopyChar(string cell, int length) => TextGenerator.CopyChar(cell, length); /// 获取指定长的的空格。 - public static string Space(int length) - { - return TextGenerator.Space(length); - } + public static string Space(int length) => TextGenerator.Space(length); /// 将文本以转换为字节数组。默认 Encoding 为 UTF-8。 - public static byte[] ToBinary(string text, Encoding encoding = null) - { - return TextConverter.ToBinary(text, encoding ?? Encoding.UTF8); - } + public static byte[] ToBinary(string text, Encoding encoding = null) => TextConverter.ToBinary(text, encoding ?? Encoding.UTF8); /// 将字节数组转换为文本。默认 Encoding 为 UTF-8。 public static string FromBinary(byte[] bytes, Encoding encoding = null) @@ -93,22 +76,13 @@ namespace Apewer } /// 将明文文本以 UTF-8 转换为 Base64 文本。 - public static string ToBase64(string plain) - { - return TextConverter.ToBase64(plain); - } + public static string ToBase64(string plain) => TextConverter.ToBase64(plain); /// 将 Base64 文本以 UTF-8 转换为明文文本。 - public static string FromBase64(string cipher) - { - return TextConverter.FromBase64(cipher); - } + public static string FromBase64(string cipher) => TextConverter.FromBase64(cipher); /// byte -> plain - public static string EncodeByte(byte @byte) - { - return TextConverter.EncodeByte(@byte); - } + public static string EncodeByte(byte @byte) => TextConverter.EncodeByte(@byte); /// binary -> hex plain public static string EncodeBinary(byte[] bytes) @@ -117,126 +91,66 @@ namespace Apewer } /// hex text -> plain - public static string EncodeText(string text, bool delimiter = false) - { - return TextConverter.EncodeText(text, delimiter); - } + public static string EncodeText(string text, bool delimiter = false) => TextConverter.EncodeText(text, delimiter); /// hex plain -> binary - public static byte[] DecodeBinary(string plain) - { - return TextConverter.DecodeBinary(plain); - } + public static byte[] DecodeBinary(string plain) => TextConverter.DecodeBinary(plain); /// hex plain -> text - public static string DecodeText(string plain) - { - return TextConverter.DecodeText(plain); - } + public static string DecodeText(string plain) => TextConverter.DecodeText(plain); /// 将字节数组格式化为字符串。 - public static string FormatX2(params byte[] bytes) - { - return TextConverter.FormatX2(bytes); - } + public static string FormatX2(params byte[] bytes) => TextConverter.FormatX2(bytes); /// 获取单精度浮点对象。 - public static Single GetFloat(char origin) - { - return TextConverter.GetSingle(origin.ToString()); - } + public static Single GetFloat(char origin) => TextConverter.GetSingle(origin.ToString()); /// 获取单精度浮点对象。 - public static Single GetFloat(string origin) - { - return TextConverter.GetSingle(origin); - } + public static Single GetFloat(string origin) => TextConverter.GetSingle(origin); /// 获取单精度浮点对象。 - public static Single GetSingle(char origin) - { - return TextConverter.GetSingle(origin.ToString()); - } + public static Single GetSingle(char origin) => TextConverter.GetSingle(origin.ToString()); /// 获取单精度浮点对象。 - public static Single GetSingle(string origin) - { - return TextConverter.GetSingle(origin); - } + public static Single GetSingle(string origin) => TextConverter.GetSingle(origin); /// 获取双精度浮点对象。 - public static Double GetDouble(char origin) - { - return TextConverter.GetDouble(origin.ToString()); - } + public static Double GetDouble(char origin) => TextConverter.GetDouble(origin.ToString()); /// 获取双精度浮点对象。 - public static Double GetDouble(string origin) - { - return TextConverter.GetDouble(origin); - } + public static Double GetDouble(string origin) => TextConverter.GetDouble(origin); /// 获取 Decimal 对象。 - public static decimal GetDecimal(char origin) - { - return TextConverter.GetDecimal(origin.ToString()); - } + public static decimal GetDecimal(char origin) => TextConverter.GetDecimal(origin.ToString()); /// 获取 Decimal 对象。 - public static decimal GetDecimal(string origin) - { - return TextConverter.GetDecimal(origin); - } + public static decimal GetDecimal(string origin) => TextConverter.GetDecimal(origin); /// 获取 Int16 对象。 - public static Int16 GetInt16(char origin) - { - return TextConverter.GetInt16(origin.ToString()); - } + public static Int16 GetInt16(char origin) => TextConverter.GetInt16(origin.ToString()); /// 获取 Int16 对象。 - public static Int16 GetInt16(string origin) - { - return TextConverter.GetInt16(origin); - } + public static Int16 GetInt16(string origin) => TextConverter.GetInt16(origin); /// 获取 Int32 对象。 - public static Int32 GetInt32(char origin) - { - return TextConverter.GetInt32(origin.ToString()); - } + public static Int32 GetInt32(char origin) => TextConverter.GetInt32(origin.ToString()); /// 获取 Int32 对象。 - public static Int32 GetInt32(string origin) - { - return TextConverter.GetInt32(origin); - } + public static Int32 GetInt32(string origin) => TextConverter.GetInt32(origin); /// 获取 Int64 对象。 - public static Int64 GetInt64(char origin) - { - return TextConverter.GetInt64(origin.ToString()); - } + public static Int64 GetInt64(char origin) => TextConverter.GetInt64(origin.ToString()); /// 获取 Int64 对象。 - public static Int64 GetInt64(string origin) - { - return TextConverter.GetInt64(origin); - } + public static Int64 GetInt64(string origin) => TextConverter.GetInt64(origin); /// 为字符串前添加字符“0”。 /// 原字符串,内容应为整数、小数或十六进制数,若格式不符则返回原字符串。 /// 新字符串的长度,若大于原数字长度,则不添加额外的“0”。 - public static string PreZero(string origin, int length = 0) - { - return TextModifier.PreZero(origin, length); - } + public static string PreZero(string origin, int length = 0) => TextModifier.PreZero(origin, length); /// 删除字符串前额外的字符“0”。 - public static string RemoveZero(string argValue) - { - return TextModifier.RemoveZero(argValue); - } + public static string RemoveZero(string text) => TextModifier.RemoveZero(text); /// 替换父字符串中的子字符串。 /// 父字符串。 @@ -244,19 +158,13 @@ namespace Apewer /// 原子字符串。 /// 查找时是否忽略父字符串和原子字符串大小写。 /// 替换后的父字符串。 - public static string Replace(string parent, string old, string @new, bool ignoreCase = false) - { - return TextModifier.Replace(parent, old, @new, ignoreCase); - } + public static string Replace(string parent, string old, string @new, bool ignoreCase = false) => TextModifier.Replace(parent, old, @new, ignoreCase); /// 修复文本后缀。默认用于修复 Windows 目录路径。 /// 原文本。 /// True:追加指定后缀;False:去除指定后缀。 /// 后缀文本。 - public static string AssureEnds(string origin, bool include = true, string foot = "\\") - { - return TextModifier.AssureEnds(origin, include, foot); - } + public static string AssureEnds(string origin, bool include = true, string foot = "\\") => TextModifier.AssureEnds(origin, include, foot); /// 用单字符作为分隔符拆分文本。 public static string[] Split(string text, char separator) @@ -357,67 +265,40 @@ namespace Apewer /// 移除字符串前后的空白。 /// 原始字符串。 /// 所有情况,全角空格将被去除。 - public static string Trim(string origin, bool allCases) - { - return TextModifier.Trim(origin, allCases); - } + public static string Trim(string origin, bool allCases) => TextModifier.Trim(origin, allCases); /// 防注入处理,去除会引发代码注入的字符。可限定字符串长度。 - public static string AntiInject(string text, int length, params char[] blacklist) - { - return TextModifier.AntiInject(text, -1, blacklist); - } + public static string AntiInject(string text, int length, params char[] blacklist) => TextModifier.AntiInject(text, length, blacklist); /// 防注入处理,去除会引发代码注入的字符。可限定字符串长度。 - public static string AntiInject(string text, int length, IEnumerable blacklist) - { - return TextModifier.AntiInject(text, -1, blacklist); - } + public static string AntiInject(string text, int length, IEnumerable blacklist) => TextModifier.AntiInject(text, length, blacklist); /// 防注入处理,去除会引发代码注入的字符。可限定字符串长度。 public static string AntiInject(string text, int length = -1, string blacklist = Constant.InjectDefaultBlackList) { - return TextModifier.AntiInject(text, -1, blacklist == null ? null : blacklist.ToCharArray()); + return TextModifier.AntiInject(text, length, blacklist == null ? null : blacklist.ToCharArray()); } /// 剪取文本内容,若指定头部为空则从原文本首部起,若指定尾部为空则至原文本末尾。 - public static string Cut(string origin, string head = null, string foot = null) - { - return TextModifier.Cut(origin, head, foot); - } + public static string Cut(string origin, string head = null, string foot = null) => TextModifier.Cut(origin, head, foot); /// 比较两个字符串的相似度。返回值大于 0,小于等于 1。 /// /// /// - public static double Similarity(string arg1, string arg2) - { - return Levenshtein.Compute(arg1, arg2).Rate; - } + public static double Similarity(string arg1, string arg2) => Levenshtein.Compute(arg1, arg2).Rate; /// 判断对象为 Null、空字符串或空白字符串。 - public static bool IsEmpty(string text) - { - return TextVerifier.IsEmpty(text); - } + public static bool IsEmpty(string text) => TextVerifier.IsEmpty(text); /// 判断对象为含有内容的字符串。 - public static bool NotEmpty(string text) - { - return !TextVerifier.IsEmpty(text); - } + public static bool NotEmpty(string text) => !TextVerifier.IsEmpty(text); /// 判断对象为 Null、空字符串或无实际内容的字符串。 - public static bool IsBlank(string text, bool allCases = false) - { - return TextVerifier.IsBlank(text, allCases); - } + public static bool IsBlank(string text, bool allCases = false) => TextVerifier.IsBlank(text, allCases); /// 判断对象为含有实际内容的字符串。 - public static bool NotBlank(string text, bool allCases = false) - { - return !TextVerifier.IsBlank(text, allCases); - } + public static bool NotBlank(string text, bool allCases = false) => !TextVerifier.IsBlank(text, allCases); /// 生成新的 GUID(不带连字符、小写)。 public static string NewGuid(bool hyphenation = false, bool lower = true) @@ -435,75 +316,60 @@ namespace Apewer /// 生成随机字符串,出现的字符由字符池指定,默认池包含数字和字母。 /// 字符池,字符池中每个字符在随机字符串中出现的概率约等。 /// 随机字符串的长度。 - public static string Random(int length, string pool = "0123456789abcdefghijklmnopqrstuvwxyz") - { - return RandomHelper.RandomCustom(pool, length); - } + public static string Random(int length, string pool = "0123456789abcdefghijklmnopqrstuvwxyz") => RandomHelper.RandomCustom(pool, length); /// 对字符串列表去重。指定 valid 参数时将去除 NULL、空字符串和空白字符串。 - public static List Distinct(IEnumerable origin, bool valid = false) - { - return TextModifier.Distinct(origin, valid); - } + public static List Distinct(IEnumerable origin, bool valid = false) => TextModifier.Distinct(origin, valid); /// 约束字符串长度范围,超出的部分将被截取去除。 - public static string RestrictLength(string origin, int length) - { - return TextModifier.RestrictLength(origin, length); - } + public static string RestrictLength(string origin, int length) => TextModifier.RestrictLength(origin, length); /// 约束字符串长度为 32,超出的部分将被截取去除。 - public static string Restrict32(string origin) - { - return TextModifier.RestrictLength(origin, 32); - } + public static string Restrict32(string origin) => TextModifier.RestrictLength(origin, 32); /// 约束字符串长度为 255,超出的部分将被截取去除。 - public static string Restrict255(string origin) - { - return TextModifier.RestrictLength(origin, 255); - } + public static string Restrict255(string origin) => TextModifier.RestrictLength(origin, 255); /// 约束字符串长度为 2000,超出的部分将被截取去除。 - public static string Restrict2000(string origin) - { - return TextModifier.RestrictLength(origin, 2000); - } + public static string Restrict2000(string origin) => TextModifier.RestrictLength(origin, 2000); /// 约束字符串中的字符,不允许的字符将被去除。 - public static string RestrictCharacters(string origin, params char[] allowable) - { - return TextModifier.RestrictCharacters(origin, new string(allowable)); - } + public static string RestrictCharacters(string origin, params char[] allowable) => TextModifier.RestrictCharacters(origin, new string(allowable)); /// 约束字符串中的字符,不允许的字符将被去除。 - public static string RestrictCharacters(string origin, string allowable) - { - return TextModifier.RestrictCharacters(origin, allowable); - } + public static string RestrictCharacters(string origin, string allowable) => TextModifier.RestrictCharacters(origin, allowable); /// 约束字符串中的字符,只保留字母。 - public static string RestrictLetters(string origin) - { - return TextModifier.RestrictCharacters(origin, Constant.LetterCollection); - } + public static string RestrictLetters(string origin) => TextModifier.RestrictCharacters(origin, Constant.LetterCollection); /// 约束字符串中的字符,只保留数字。 - public static string RestrictNumeric(string origin) - { - return TextModifier.RestrictCharacters(origin, Constant.NumberCollection); - } + public static string RestrictNumeric(string origin) => TextModifier.RestrictCharacters(origin, Constant.NumberCollection); /// 约束字符串,只保留 GUID 可能出现的字符,根据连字符限定长度为 32 或 36。 - public static string RestrictGuid(string origin) + public static string RestrictGuid(string origin) => TextModifier.RestrictGuid(origin); + + /// 返回此字符串的安全键副本,只保留数据记录主键中可能出现的字符,默认限制长度为 255 字符。 + public static string SafeKey(string text, int maxLength = 255) { - return TextModifier.RestrictGuid(origin); + if (string.IsNullOrEmpty(text)) return Constant.EmptyString; + var input = ToLower(text); + var max = maxLength > 0 ? maxLength : input.Length; + var sb = new StringBuilder(); + for (var i = 0; i < max; i++) + { + var c = input[i]; + if (Constant.HexCollection.IndexOf(c) < 0) continue; + sb.Append(c); + } + var result = sb.ToString(); + return result; } /// 追加字符串。 - public static void Append(StringBuilder builder, params object[] cells) + public static StringBuilder Append(StringBuilder builder, params object[] cells) { if (builder != null) builder.Append(Join(null, cells)); + return builder; } /// 对 URL 编码。 diff --git a/Apewer/_ChangeLog.md b/Apewer/_ChangeLog.md index f7647f0..1ce335d 100644 --- a/Apewer/_ChangeLog.md +++ b/Apewer/_ChangeLog.md @@ -5,6 +5,9 @@ ### 最新提交 +### 6.0.9 +- 调整了 TextUtility.SafeKey 的长度限制。 + ### 6.0.8 - Query.SimpleColumn 不再去重,提升性能。 diff --git a/Apewer/_Extensions.cs b/Apewer/_Extensions.cs index ad5bf5b..047c2c9 100644 --- a/Apewer/_Extensions.cs +++ b/Apewer/_Extensions.cs @@ -192,7 +192,7 @@ public static class Extensions public static string[] Split(this string @this, params char[] separators) => TextUtility.Split(@this, separators); /// 返回此字符串的安全键副本。 - public static string SafeKey(this string @this) => TextUtility.RestrictGuid(@this); + public static string SafeKey(this string @this, int maxLength = 255) => TextUtility.SafeKey(@this, maxLength); /// 移除字符串前后的空白。 public static string SafeTrim(this string @this) => TextUtility.Trim(@this);