﻿using System;
using System.ComponentModel;
using System.IO;
using System.Net;

namespace TweetLib.Core.Utils{
    public static class WebUtils{
        private static bool HasMicrosoftBeenBroughtTo2008Yet;

        private static void EnsureTLS12(){
            if (!HasMicrosoftBeenBroughtTo2008Yet){
                ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
                ServicePointManager.SecurityProtocol &= ~(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11);
                HasMicrosoftBeenBroughtTo2008Yet = true;
            }
        }

        public static WebClient NewClient(string userAgent){
            EnsureTLS12();

            WebClient client = new WebClient{ Proxy = null };
            client.Headers[HttpRequestHeader.UserAgent] = userAgent;
            return client;
        }

        public static AsyncCompletedEventHandler FileDownloadCallback(string file, Action? onSuccess, Action<Exception>? onFailure){
            return (sender, args) => {
                if (args.Cancelled){
                    try{
                        File.Delete(file);
                    }catch{
                        // didn't want it deleted anyways
                    }
                }
                else if (args.Error != null){
                    onFailure?.Invoke(args.Error);
                }
                else{
                    onSuccess?.Invoke();
                }
            };
        }
    }
}

﻿using System;
using System.ComponentModel;
using System.IO;
using System.Net;

namespace TweetLib.Core.Utils{
    public static class WebUtils{
        private static bool HasMicrosoftBeenBroughtTo2008Yet;

        private static void EnsureTLS12(){
            if (!HasMicrosoftBeenBroughtTo2008Yet){
                ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
                ServicePointManager.SecurityProtocol &= ~(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11);
                HasMicrosoftBeenBroughtTo2008Yet = true;
            }
        }

        public static WebClient NewClient(string userAgent){
            EnsureTLS12();

            WebClient client = new WebClient{ Proxy = null };
            client.Headers[HttpRequestHeader.UserAgent] = userAgent;
            return client;
        }

        public static AsyncCompletedEventHandler FileDownloadCallback(string file, Action? onSuccess, Action<Exception>? onFailure){
            return (sender, args) => {
                if (args.Cancelled){
                    TryDeleteFile(file);
                }
                else if (args.Error != null){
                    TryDeleteFile(file);
                    onFailure?.Invoke(args.Error);
                }
                else{
                    onSuccess?.Invoke();
                }
            };
        }

        private static void TryDeleteFile(string file){
            try{
                File.Delete(file);
            }catch{
                // didn't want it deleted anyways
            }
        }
    }
}

Delete corrupted downloads after an error
Delete corrupted downloads after an error
