Implement IDisposable

This commit is contained in:
Jaex 2022-12-07 17:26:58 +03:00
parent 4ae7cc56a9
commit 09500ebaa8
3 changed files with 49 additions and 32 deletions

View file

@ -3051,6 +3051,11 @@ private async void oauth2YouTube_OpenButtonClicked()
{ {
Config.YouTubeOAuth2Info = listener.OAuth.AuthInfo; Config.YouTubeOAuth2Info = listener.OAuth.AuthInfo;
} }
else
{
Config.YouTubeOAuth2Info = null;
}
this.ForceActivate();
ConfigureOAuthStatus(oauth2YouTube, result); ConfigureOAuthStatus(oauth2YouTube, result);
} }

View file

@ -33,17 +33,31 @@
namespace ShareX.UploadersLib namespace ShareX.UploadersLib
{ {
public class OAuthListener public class OAuthListener : IDisposable
{ {
public IOAuth2Loopback OAuth { get; private set; } public IOAuth2Loopback OAuth { get; private set; }
public string Code { get; private set; }
private HttpListener listener;
public OAuthListener(IOAuth2Loopback oauth) public OAuthListener(IOAuth2Loopback oauth)
{ {
OAuth = oauth; OAuth = oauth;
} }
public void Dispose()
{
listener?.Close();
listener = null;
}
public async Task<bool> ConnectAsync() public async Task<bool> ConnectAsync()
{ {
try
{
Dispose();
Code = null;
IPAddress ip = IPAddress.Loopback; IPAddress ip = IPAddress.Loopback;
int port = URLHelpers.GetRandomUnusedPort(); int port = URLHelpers.GetRandomUnusedPort();
string redirectURI = string.Format($"http://{ip}:{port}/"); string redirectURI = string.Format($"http://{ip}:{port}/");
@ -64,19 +78,18 @@ public async Task<bool> ConnectAsync()
try try
{ {
using (HttpListener listener = new HttpListener()) listener = new HttpListener();
{
listener.Prefixes.Add(redirectURI); listener.Prefixes.Add(redirectURI);
listener.Start(); listener.Start();
HttpListenerContext context = await listener.GetContextAsync(); HttpListenerContext context = await listener.GetContextAsync();
string code = context.Request.QueryString.Get("code"); Code = context.Request.QueryString.Get("code");
using (HttpListenerResponse response = context.Response) using (HttpListenerResponse response = context.Response)
{ {
string status; string status;
if (!string.IsNullOrEmpty(code)) if (!string.IsNullOrEmpty(Code))
{ {
status = "Authorization completed successfully."; status = "Authorization completed successfully.";
} }
@ -96,16 +109,21 @@ public async Task<bool> ConnectAsync()
await responseOutput.FlushAsync(); await responseOutput.FlushAsync();
} }
} }
if (!string.IsNullOrEmpty(code))
{
return await Task.Run(() => OAuth.GetAccessToken(code));
} }
finally
{
Dispose();
}
if (!string.IsNullOrEmpty(Code))
{
return await Task.Run(() => OAuth.GetAccessToken(Code));
} }
} }
catch (Exception e) catch (Exception e)
{ {
DebugHelper.WriteException(e); DebugHelper.WriteException(e);
e.ShowError();
} }
return false; return false;

View file

@ -37,12 +37,6 @@
<p>{0}</p> <p>{0}</p>
<p>You can now close this page.</p> <p>You can now close this page.</p>
</div> </div>
<script>
setTimeout(function() {
window.close();
}, 30000);
</script>
</body> </body>
</html> </html>