コード例 #1
0
ファイル: SearchAutomation.cs プロジェクト: ADeltaX/MSEdgeBot
        private static async Task UploadAndSendMessage(EdgeFile fileElement, string captionHtml)
        {
            string file = Path.Combine(DownloadFolder, fileElement.FileName);

            if (!File.Exists(file))
            {
                SharedDBcmd.TraceError($"Internal error: File is not downloaded => " + file);
                Console.WriteLine("File is not downloaded => " + file);
                return;
            }

            await UploadFileHelper.UploadFileAsync(file, captionHtml);
        }
コード例 #2
0
ファイル: SearchAutomation.cs プロジェクト: ADeltaX/MSEdgeBot
        private static async Task Download(string name, string version, EdgeFile fileElement, int numTries = 0)
        {
            if (numTries >= _numMaxDLAttempts)
            {
                return;
            }

            try
            {
                string filename = Path.Combine(DownloadFolder, fileElement.FileName);

                if (File.Exists(filename))
                {
                    byte[] fileHash = GetSHA1HashFile(filename);

                    //We already have this update, no need to download it again
                    if (fileHash.SequenceEqual(fileElement.Sha1))
                    {
                        Console.WriteLine($"[{DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")}] --- The file is already downloaded and valid, using this...");
                        return;
                    }
                    else
                    {
                        File.Delete(filename);
                    }
                }

                using (WebClient wb = new WebClient())
                {
                    await wb.DownloadFileTaskAsync(fileElement.Url, filename);

                    byte[] fileHash = GetSHA1HashFile(filename);

                    if (!fileHash.SequenceEqual(fileElement.Sha1))
                    {
                        if (File.Exists(filename))
                        {
                            File.Delete(filename);
                        }

                        throw new DataMisalignedException("Rip SHA1 hash");
                    }
                }
            }
            catch (Exception ex)
            {
                var errorMessageIter = ex.Message;

                var currException = ex;

                while ((currException = currException.InnerException) != null)
                {
                    errorMessageIter += "\n\nINNER EXCEPTION: " + currException.Message;
                }

                SharedDBcmd.TraceError($"Internal error: {errorMessageIter}");

                Console.WriteLine("[ERROR] Failed downloading... retrying...");

                //Generate a new link.... just in case
                var res = await _edgeUpdate.GetFiles(name, version);

                if (res.Success)
                {
                    var edgeFileFull = res.Value.EdgeFile.Where(file => file.EdgeFileUpdateType == EdgeFileUpdateType.Full).First();
                    await Download(name, version, edgeFileFull, ++numTries);
                }
                else
                {
                    await Download(name, version, fileElement, ++numTries);
                }
            }
        }