diff --git a/Framework/Utils/RandomUtils.cs b/Framework/Utils/RandomUtils.cs index f3792123..9f127167 100644 --- a/Framework/Utils/RandomUtils.cs +++ b/Framework/Utils/RandomUtils.cs @@ -47,13 +47,14 @@ var result = ""; while (result.Length < requiredLength) { - var len = Math.Min(1024, requiredLength - result.Length); + var remaining = requiredLength - result.Length; + var len = Math.Min(1024, remaining); var bytes = new byte[len]; random.NextBytes(bytes); result += string.Join("", bytes.Select(b => b.ToString())); } - return result; + return result.Substring(0, Convert.ToInt32(requiredLength)); } } } diff --git a/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs b/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs index 8dadc97b..216ab7f0 100644 --- a/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs +++ b/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs @@ -15,6 +15,8 @@ namespace BiblioTech.CodexChecking Task CouldNotDownloadCid(); Task GiveCidToUser(string cid); Task GiveDataFileToUser(string fileContent); + + Task ToAdminChannel(string msg); } public class CodexTwoWayChecker @@ -91,9 +93,9 @@ namespace BiblioTech.CodexChecking return; } - if (IsManifestLengthCompatible(check, manifest)) + if (await IsManifestLengthCompatible(handler, check, manifest)) { - if (IsContentCorrect(check, receivedCid)) + if (await IsContentCorrect(handler, check, receivedCid)) { CheckNowCompleted(handler, check, userId); return; @@ -148,34 +150,38 @@ namespace BiblioTech.CodexChecking } } - private bool IsManifestLengthCompatible(TransferCheck check, Manifest manifest) + private async Task IsManifestLengthCompatible(ICheckResponseHandler handler, TransferCheck check, Manifest manifest) { var dataLength = check.UniqueData.Length; var manifestLength = manifest.OriginalBytes.SizeInBytes; + await handler.ToAdminChannel($"Debug:dataLength={dataLength},manifestLength={manifestLength}"); + return manifestLength > (dataLength - 1) && manifestLength < (dataLength + 1); } - private bool IsContentCorrect(TransferCheck check, string receivedCid) + private async Task IsContentCorrect(ICheckResponseHandler handler, TransferCheck check, string receivedCid) { try { - return codexWrapper.OnCodex(node => + var content = codexWrapper.OnCodex(node => { var file = node.DownloadContent(new ContentId(receivedCid)); - if (file == null) return false; + if (file == null) return string.Empty; try { - var content = File.ReadAllText(file.Filename).Trim(); - return content == check.UniqueData; + return File.ReadAllText(file.Filename).Trim(); } finally { if (File.Exists(file.Filename)) File.Delete(file.Filename); } }); + + await handler.ToAdminChannel($"Debug:content=`{content}`,check=`{check.UniqueData}`"); + return content == check.UniqueData; } catch { diff --git a/Tools/BiblioTech/Commands/CheckResponseHandler.cs b/Tools/BiblioTech/Commands/CheckResponseHandler.cs index 9a32a7c3..8777003e 100644 --- a/Tools/BiblioTech/Commands/CheckResponseHandler.cs +++ b/Tools/BiblioTech/Commands/CheckResponseHandler.cs @@ -60,5 +60,10 @@ namespace BiblioTech.Commands { await context.Followup("Successfully completed the check!"); } + + public async Task ToAdminChannel(string msg) + { + await Program.AdminChecker.SendInAdminChannel(msg); + } } } diff --git a/Tools/BiblioTech/Options/StringOption.cs b/Tools/BiblioTech/Options/StringOption.cs index 15e04c9a..047bf15a 100644 --- a/Tools/BiblioTech/Options/StringOption.cs +++ b/Tools/BiblioTech/Options/StringOption.cs @@ -12,11 +12,12 @@ namespace BiblioTech.Options public async Task Parse(CommandContext context) { var strData = context.Options.SingleOrDefault(o => o.Name == Name); - if (strData == null) + if (strData == null && IsRequired) { await context.Followup("String option not received."); return null; } + if (strData == null) return null; return strData.Value as string; } }