@@ -21,34 +21,53 @@ public static void Init(AndrealConfig config)
2121 JsonConvert . DeserializeObject < ResponseRoot > ( await ( await _client ! . SendAsync ( new ( HttpMethod . Get , url ) ) )
2222 . EnsureSuccessStatusCode ( ) . Content . ReadAsStringAsync ( ) ) ;
2323
24- private static async Task GetStream ( string url , Path filename )
24+ private static async Task GetImage ( string url , Path filename )
2525 {
2626 FileStream ? fileStream = null ;
27+ var message = ( await _client ! . GetAsync ( url ) ) . EnsureSuccessStatusCode ( ) ;
28+
29+ if ( message . Content . Headers . ContentType ? . MediaType ? . StartsWith ( "image/" ) != true )
30+ throw new ArgumentException ( JsonConvert . DeserializeObject < ResponseRoot > ( await message . Content
31+ . ReadAsStringAsync ( ) ) !
32+ . Message ) ;
33+
34+ var exflag = false ;
2735
2836 try
2937 {
3038 fileStream = new ( filename , FileMode . Create , FileAccess . ReadWrite , FileShare . ReadWrite ) ;
31- await ( await _client ! . GetAsync ( url ) ) . EnsureSuccessStatusCode ( ) . Content . CopyToAsync ( fileStream ) ;
39+ await message . Content . CopyToAsync ( fileStream ) ;
3240 }
3341 catch
42+ {
43+ exflag = true ;
44+ throw ;
45+ }
46+ finally
3447 {
3548 try
3649 {
37- File . Delete ( filename ) ;
50+ if ( fileStream is not null )
51+ {
52+ fileStream . Close ( ) ;
53+ await fileStream . DisposeAsync ( ) ;
54+ }
3855 }
3956 catch
4057 {
4158 // ignore
4259 }
43-
44- throw ;
45- }
46- finally
47- {
48- if ( fileStream is not null )
60+
61+ try
4962 {
50- fileStream . Close ( ) ;
51- await fileStream . DisposeAsync ( ) ;
63+ if ( exflag && File . Exists ( filename ) )
64+ {
65+ File . Delete ( filename ) ;
66+ }
67+ }
68+ catch
69+ {
70+ // ignore
5271 }
5372 }
5473 }
@@ -69,16 +88,16 @@ private static async Task GetStream(string url, Path filename)
6988 internal static async Task < ResponseRoot ? > SongList ( ) => await GetString ( "song/list" ) ;
7089
7190 internal static async Task SongAssets ( string sid , int difficulty , Path pth ) =>
72- await GetStream ( $ "assets/song?songid={ sid } &difficulty={ difficulty } ", pth ) ;
91+ await GetImage ( $ "assets/song?songid={ sid } &difficulty={ difficulty } ", pth ) ;
7392
7493 internal static async Task CharAssets ( int partner , bool awakened , Path pth ) =>
75- await GetStream ( $ "assets/char?partner={ partner } &awakened={ ( awakened ? "true" : "false" ) } ", pth ) ;
94+ await GetImage ( $ "assets/char?partner={ partner } &awakened={ ( awakened ? "true" : "false" ) } ", pth ) ;
7695
7796 internal static async Task IconAssets ( int partner , bool awakened , Path pth ) =>
78- await GetStream ( $ "assets/icon?partner={ partner } &awakened={ ( awakened ? "true" : "false" ) } ", pth ) ;
97+ await GetImage ( $ "assets/icon?partner={ partner } &awakened={ ( awakened ? "true" : "false" ) } ", pth ) ;
7998
8099 internal static async Task PreviewAssets ( string sid , int difficulty , Path pth ) =>
81- await GetStream ( $ "assets/preview?songid={ sid } &difficulty={ difficulty } ", pth ) ;
100+ await GetImage ( $ "assets/preview?songid={ sid } &difficulty={ difficulty } ", pth ) ;
82101
83102 internal static TextMessage GetErrorMessage ( RobotReply info , int status , string message )
84103 {
0 commit comments