Пример #1
0
        public static async Task DownloadMostRecentFirmware(JsonReps.FirmwareListing firmwareListing, string basePathToFolder, bool deleteOldFiles)
        {
            //leave if no firmware is found
            if (firmwareListing.firmwares.Count == 0)
            {
                ++_totalDone;
                Console.Write($"{firmwareListing.name} has no firmware for download");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"                      {(_totalDone / _totalCount) * 100}% complete");
                Console.ForegroundColor = ConsoleColor.Gray;
                return;
            }

            //firmware[0] is always the newest firmware available
            var res = await Client.GetAsync($"https://api.ipsw.me/v4/ipsw/download/{firmwareListing.firmwares[0].identifier}/{firmwareListing.firmwares[0].buildid}");

            var urlToDownload = res.Headers.Location;

            Console.WriteLine($"Beginning to download {firmwareListing.name} {firmwareListing.firmwares[0].version}");

            //If file has already been downloaded, skip
            if (File.Exists(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw")))
            {
                ++_totalDone;
                Console.Write($"{firmwareListing.name} {firmwareListing.firmwares[0].version} already exists. Skipping download");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"                      {(_totalDone / _totalCount) * 100}% complete");
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }

            //If set to delete all old firmware and there are existing files, delete all
            if (deleteOldFiles &&
                Directory.Exists(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/")) &&
                Directory.GetFiles(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/")).Length != 0)
            {
                Console.WriteLine($"{firmwareListing.name} has existing files. Deleting...");

                //Deletes all files in directory
                foreach (var file in Directory.GetFiles(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/")))
                {
                    File.Delete(file);
                }

                Console.WriteLine("Finished deleting files. Resuming download...");
            }

            Stream dlStream = null;

            //If apples api errors, skip. This likely means the IPSW is no longer provided.
            try
            {
                dlStream = await Client.GetStreamAsync(urlToDownload);
            }
            catch (HttpRequestException)
            {
                ++_totalDone;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write($"{firmwareListing.name} {firmwareListing.firmwares[0].version} erred out on apples side. This likely means this IPSW is deprecated.");
                Console.WriteLine($"                      {(_totalDone / _totalCount) * 100}% complete");
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }

            //Create file
            Directory.CreateDirectory(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/"));
            await using var fileStream = File.Create(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw"));

            using var cts = new CancellationTokenSource();

            //Delete currently downloading files if program is cancelled
            void DeleteCorruptFileCallback(object sender, ConsoleCancelEventArgs args)
            {
                cts.Cancel();
                fileStream.Dispose();
                dlStream.Dispose();

                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Stop Command recived. Deleting corrupt downloads...");
                Console.ForegroundColor = ConsoleColor.Gray;
                try
                {
                    File.Delete(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw"));
                    Console.WriteLine($"Deleted {Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw")}");
                }
                catch (Exception)
                {
                    Console.WriteLine($"There was an error deleting {Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw")}");
                }
            }

            Console.CancelKeyPress += DeleteCorruptFileCallback;

            //download the file
            try
            {
                await dlStream.CopyToAsync(fileStream, cts.Token);
            }
            catch (TaskCanceledException)
            {
                //ignore, handled by the delegate above.
            }

            Console.CancelKeyPress -= DeleteCorruptFileCallback;
            await dlStream.DisposeAsync();

            ++_totalDone;
            Console.Write($"Finished downloading {firmwareListing.name} {firmwareListing.firmwares[0].version}");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"                      {(_totalDone/_totalCount) * 100}% complete");
            Console.ForegroundColor = ConsoleColor.Gray;
        }
Пример #2
0
        public static async Task DownloadMostRecentFirmware(JsonReps.FirmwareListing firmwareListing, string basePathToFolder)
        {
            //leave if no firmware is found
            if (firmwareListing.firmwares.Count == 0)
            {
                ++TotalDone;
                Console.Write($"{firmwareListing.name} has no firmware for download");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"                      {(TotalDone / TotalCount) * 100}% complete");
                Console.ForegroundColor = ConsoleColor.Gray;
                return;
            }

            //firmware[0] is always the newest
            var res = await Client.GetAsync($"https://api.ipsw.me/v4/ipsw/download/{firmwareListing.firmwares[0].identifier}/{firmwareListing.firmwares[0].buildid}");

            var urlToDownload = res.Headers.Location;

            Console.WriteLine($"Beginning to download {firmwareListing.name} {firmwareListing.firmwares[0].version}");

            //If file has already been downloaded, skip
            if (File.Exists(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw")))
            {
                ++TotalDone;
                Console.Write($"{firmwareListing.name} {firmwareListing.firmwares[0].version} already exists. Skipping download");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"                      {(TotalDone / TotalCount) * 100}% complete");
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }

            //Create file
            Directory.CreateDirectory(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/"));
            await using var fileStream = File.Create(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw"));
            await using var dlStream   = await Client.GetStreamAsync(urlToDownload);

            using var cts = new CancellationTokenSource();

            //Delete currently downloading files if program is cancelled
            void DeleteCorruptFile(object sender, ConsoleCancelEventArgs args)
            {
                cts.Cancel();
                fileStream.Dispose();
                dlStream.Dispose();

                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Stop Command recived. Deleting corrupt downloads...");
                Console.ForegroundColor = ConsoleColor.Gray;
                try
                {
                    File.Delete(Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw"));
                    Console.WriteLine($"Deleted {Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw")}");
                }
                catch (Exception)
                {
                    Console.WriteLine($"There was an error deleting {Path.Join(basePathToFolder, $@"/IPSW/{firmwareListing.name}/{firmwareListing.firmwares[0].version}.ipsw")}");
                }
            }

            Console.CancelKeyPress += DeleteCorruptFile;

            //download the file
            try
            {
                await dlStream.CopyToAsync(fileStream, cts.Token);
            }
            catch (TaskCanceledException)
            {
                //ignore, handled by the delegate above.
            }

            Console.CancelKeyPress -= DeleteCorruptFile;

            ++TotalDone;
            Console.Write($"Finished downloading {firmwareListing.name} {firmwareListing.firmwares[0].version}");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"                      {(TotalDone/TotalCount) * 100}% complete");
            Console.ForegroundColor = ConsoleColor.Gray;
        }