diff --git a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs index 5aa457ba..33194edd 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs @@ -18,6 +18,12 @@ internal sealed class HutaoException : Exception public HutaoExceptionKind Kind { get; private set; } + [DoesNotReturn] + public static HutaoException Throw(HutaoExceptionKind kind, string message, Exception? innerException = default) + { + throw new HutaoException(kind, message, innerException); + } + public static void ThrowIf(bool condition, HutaoExceptionKind kind, string message, Exception? innerException = default) { if (condition) @@ -25,4 +31,10 @@ internal sealed class HutaoException : Exception throw new HutaoException(kind, message, innerException); } } + + public static HutaoException ServiceTypeCastFailed(string name, Exception? innerException = default) + { + string message = $"This instance of '{typeof(TFrom).FullName}' '{name}' doesn't implement '{typeof(TTo).FullName}'"; + throw new HutaoException(HutaoExceptionKind.ServiceTypeCastFailed, message, innerException); + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoExceptionKind.cs b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoExceptionKind.cs index 6cf43a30..7b975757 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoExceptionKind.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoExceptionKind.cs @@ -6,4 +6,5 @@ namespace Snap.Hutao.Core.ExceptionService; internal enum HutaoExceptionKind { None, + ServiceTypeCastFailed, } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 8572ee15..558b8e75 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -1691,6 +1691,9 @@ 完成 + + 响应内容不是有效的文件字节流 + 等待中 diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/Guide/DownloadSummary.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/Guide/DownloadSummary.cs index 7069167f..9942256e 100644 --- a/src/Snap.Hutao/Snap.Hutao/ViewModel/Guide/DownloadSummary.cs +++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/Guide/DownloadSummary.cs @@ -5,6 +5,7 @@ using CommunityToolkit.Common; using CommunityToolkit.Mvvm.ComponentModel; using Snap.Hutao.Core; using Snap.Hutao.Core.Caching; +using Snap.Hutao.Core.ExceptionService; using Snap.Hutao.Core.IO; using System.IO; using System.IO.Compression; @@ -74,6 +75,14 @@ internal sealed class DownloadSummary : ObservableObject try { HttpResponseMessage response = await httpClient.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); + + if (response.Content.Headers.ContentType?.MediaType is not "application/octet-stream") + { + logger.LogWarning("Download Static Zip failed, Content-Type is {Type}", response.Content.Headers.ContentType); + Description = SH.ViewModelWelcomeDownloadSummaryContentTypeNotMatch; + return false; + } + long contentLength = response.Content.Headers.ContentLength ?? 0; logger.LogInformation("Begin download, length: {length}", contentLength); using (Stream content = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) @@ -95,7 +104,7 @@ internal sealed class DownloadSummary : ObservableObject logger.LogError(ex, "Download Static Zip failed"); await taskContext.SwitchToMainThreadAsync(); Description = ex is HttpRequestException httpRequestException - ? $"{SH.ViewModelWelcomeDownloadSummaryException} - HTTP {httpRequestException.HttpRequestError} {httpRequestException.StatusCode:D}" + ? $"{SH.ViewModelWelcomeDownloadSummaryException} - [HTTP '{httpRequestException.StatusCode:D}'] [Error '{httpRequestException.HttpRequestError}']" : ex.Message; return false; } @@ -109,8 +118,10 @@ internal sealed class DownloadSummary : ObservableObject private void ExtractFiles(Stream stream) { - IImageCacheFilePathOperation? imageCacheFilePathOperation = imageCache.As(); - ArgumentNullException.ThrowIfNull(imageCacheFilePathOperation); + if (imageCache is not IImageCacheFilePathOperation imageCacheFilePathOperation) + { + throw HutaoException.ServiceTypeCastFailed(nameof(imageCache)); + } using (ZipArchive archive = new(stream)) {