Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed Jan 19, 2023
2 parents dc54606 + fbb3af4 commit 9db5558
Show file tree
Hide file tree
Showing 20 changed files with 952 additions and 1,164 deletions.
454 changes: 143 additions & 311 deletions OpenTween.Tests/MediaSelectorTest.cs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions OpenTween/Api/TwitterApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class TwitterApiException : WebApiException
public TwitterErrorItem[] Errors
=> this.ErrorResponse != null ? this.ErrorResponse.Errors : Array.Empty<TwitterErrorItem>();

public string[] LongMessages
=> this.Errors.Select(x => x.Message).ToArray();

public TwitterApiException()
{
}
Expand Down
7 changes: 5 additions & 2 deletions OpenTween/AppendSettingDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTween.Api;
using OpenTween.Connection;
using OpenTween.Setting.Panel;
using OpenTween.Thumbnail;
Expand Down Expand Up @@ -208,9 +209,11 @@ private async void StartAuthButton_Click(object sender, EventArgs e)
"Authenticate",
MessageBoxButtons.OK);
}
catch (WebApiException ex)
catch (TwitterApiException ex)
{
var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine +
string.Join(Environment.NewLine, ex.LongMessages);

MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
}
}
Expand Down
20 changes: 10 additions & 10 deletions OpenTween/Connection/TwitterApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task<T> GetAsync<T>(Uri uri, IDictionary<string, string>? param, st
if (endpointName != null)
MyCommon.TwitterApiInfo.UpdateFromHeader(response.Headers, endpointName);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);

using var content = response.Content;
Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task<Stream> GetStreamingStreamAsync(Uri uri, IDictionary<string, s
var response = await this.HttpStreaming.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);

return await response.Content.ReadAsStreamAsync()
Expand Down Expand Up @@ -220,7 +220,7 @@ public async Task<LazyJson<T>> PostLazyAsync<T>(Uri uri, IDictionary<string, str
response = await this.Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);

var result = new LazyJson<T>(response);
Expand Down Expand Up @@ -267,7 +267,7 @@ public async Task<LazyJson<T>> PostLazyAsync<T>(Uri uri, IDictionary<string, str
response = await this.HttpUpload.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);

var result = new LazyJson<T>(response);
Expand Down Expand Up @@ -313,7 +313,7 @@ public async Task PostAsync(Uri uri, IDictionary<string, string>? param, IDictio
using var response = await this.HttpUpload.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);
}
catch (HttpRequestException ex)
Expand Down Expand Up @@ -345,7 +345,7 @@ public async Task<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json)
response = await this.Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);

var result = new LazyJson<T>(response);
Expand Down Expand Up @@ -377,7 +377,7 @@ public async Task DeleteAsync(Uri uri)
using var response = await this.Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

await this.CheckStatusCode(response)
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);
}
catch (HttpRequestException ex)
Expand All @@ -390,7 +390,7 @@ await this.CheckStatusCode(response)
}
}

protected async Task CheckStatusCode(HttpResponseMessage response)
protected static async Task CheckStatusCode(HttpResponseMessage response)
{
var statusCode = response.StatusCode;

Expand Down Expand Up @@ -546,8 +546,8 @@ private static async Task<IDictionary<string, string>> GetOAuthTokenAsync(
var responseText = await content.ReadAsStringAsync()
.ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
throw new TwitterApiException(response.StatusCode, responseText);
await TwitterApiConnection.CheckStatusCode(response)
.ConfigureAwait(false);

var responseParams = HttpUtility.ParseQueryString(responseText);

Expand Down
13 changes: 13 additions & 0 deletions OpenTween/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ public static int GetCodepointCount(this string s, int start, int end)
return count;
}

public static bool TryInvoke(this Control control, Action action)
{
if (control.IsDisposed)
return false;

if (control.InvokeRequired)
control.Invoke(action);
else
action();

return true;
}

public static Task ForEachAsync<T>(this IObservable<T> observable, Action<T> subscriber)
{
return ForEachAsync(observable, value =>
Expand Down
64 changes: 26 additions & 38 deletions OpenTween/MediaItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@

namespace OpenTween
{
public interface IMediaItem
public interface IMediaItem : IDisposable
{
/// <summary>
/// メディアのID
/// </summary>
Guid Id { get; }

/// <summary>
/// メディアが既に破棄されているかを示す真偽値
/// </summary>
bool IsDisposed { get; }

/// <summary>
/// メディアへの絶対パス
/// </summary>
Expand All @@ -57,11 +67,6 @@ public interface IMediaItem
/// </summary>
long Size { get; }

/// <summary>
/// メディアが画像であるかどうかを示す真偽値
/// </summary>
bool IsImage { get; }

/// <summary>
/// 代替テキスト (アップロード先が対応している必要がある)
/// </summary>
Expand Down Expand Up @@ -106,6 +111,10 @@ public FileMediaItem(FileInfo fileInfo)
{
}

public Guid Id { get; } = Guid.NewGuid();

public bool IsDisposed { get; private set; } = false;

public string Path
=> this.FileInfo.FullName;

Expand All @@ -121,34 +130,6 @@ public bool Exists
public long Size
=> this.FileInfo.Length;

public bool IsImage
{
get
{
if (this.isImage == null)
{
try
{
// MemoryImage が生成できるかを検証する
using (var image = this.CreateImage())
{
}

this.isImage = true;
}
catch (InvalidImageException)
{
this.isImage = false;
}
}

return this.isImage.Value;
}
}

/// <summary>IsImage の検証結果をキャッシュする。未検証なら null</summary>
private bool? isImage = null;

public MemoryImage CreateImage()
{
using var fs = this.FileInfo.OpenRead();
Expand All @@ -163,6 +144,14 @@ public void CopyTo(Stream stream)
using var fs = this.FileInfo.OpenRead();
fs.CopyTo(stream);
}

public void Dispose()
{
if (this.IsDisposed)
return;

this.IsDisposed = true;
}
}

/// <summary>
Expand All @@ -171,7 +160,7 @@ public void CopyTo(Stream stream)
/// <remarks>
/// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
/// </remarks>
public class MemoryImageMediaItem : IMediaItem, IDisposable
public class MemoryImageMediaItem : IMediaItem
{
public const string PathPrefix = "<>MemoryImage://";
private static int fileNumber = 0;
Expand All @@ -187,6 +176,8 @@ public MemoryImageMediaItem(MemoryImage image)
this.Path = PathPrefix + num + this.image.ImageFormatExt;
}

public Guid Id { get; } = Guid.NewGuid();

public string Path { get; }

public string? AltText { get; set; }
Expand All @@ -203,9 +194,6 @@ public bool Exists
public long Size
=> this.image.Stream.Length;

public bool IsImage
=> true;

public MemoryImage CreateImage()
=> this.image.Clone();

Expand Down
Loading

0 comments on commit 9db5558

Please sign in to comment.